Write a JAVA program that prompts a user to enter data for an array. Use any pri
ID: 3778887 • Letter: W
Question
Write a JAVA program that prompts a user to enter data for an array. Use any primitive data type of your choice. The program should also have the following methods:
• getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array.
• GetAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array.
• GetHighest. This method should accept a one-dimensional array as its argument and return the highest of the values in the array.
• GetLowest. This method should accept a one-dimensional array as its argument and return the lowest of the values in the array.
EXAMPLE OUTPUT
Enter Integers: (Enter -999 to stop)
Enter Integer: 2
Enter Integer: 3
Enter Integer: 4
Enter Integer: 5
Enter Integer: -999
Processing the int array.
Total : 14
Average : 4
Highest value : 5
Lowest value : 2
Explanation / Answer
import java.util.Scanner;
public class Simple77 {
static int getTotal(int a[],int l)
{
int ans=0;
for(int i=0;i<l;i++)
{
ans+=a[i];
}
return ans;
}
static int getAverage(int a[],int l)
{
return (getTotal(a,l))/l;
}
static int getHighest(int a[],int l)
{
int max=a[0];
for(int i=1;i<l;i++)
{
if(max<a[i])
{
max=a[i];
}
}
return max;
}
static int getLowest(int a[],int l)
{
int min=a[0];
for(int i=1;i<l;i++)
{
if(min>a[i])
{
min=a[i];
}
}
return min;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int r=0,l=0;
int a[] = new int[10000];
System.out.println("Enter Integers: (Enter -999 to stop)");
while(true)
{
System.out.print("Enter Integers:");
r = scan.nextInt();
if(r==-999)
{
break;
}
a[l]=r;
l++;
}
System.out.println("Processing the int array. ");
System.out.print("Total : ");
System.out.println(getTotal(a, l));
System.out.print("Average : ");
System.out.println(getAverage(a, l));
System.out.print("Highest value : ");
System.out.println(getHighest(a, l));
System.out.print("Lowest value : ");
System.out.println(getLowest(a, l));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.