Question & Answer: Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The p…..

Write a program with an array that is initialized with test data. 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 value in the array.

getLowest. This method should accept a one-dimensional array as its argument and return the lowest value in the array.

package name operations

Expert Answer

 

public class ArrayTotalAverageHighestLowest {

// Method for getting the highest value

public static int getHighest(int[] arr){

int highestValue = arr[0];

for(int i=1;i < arr.length;i++){

if(arr[i] > highestValue){

highestValue = arr[i];

}

}

return highestValue;

}

 

// Method for getting the lowest value

public static int getLowest(int[] arr){

int lowestValue = arr[0];

for(int i=1;i<arr.length;i++){

if(arr[i] < lowestValue){

lowestValue = arr[i];

}

}

return lowestValue;

}

 

// Method for getting the average value

public static double getAverage(int[] arr){

int sum=0;

for(int i=0;i<arr.length;i++){

sum +=arr[i];

}

double averageValue = (double)sum/arr.length;

return averageValue;

}

 

// Method for getting the total value

public static int getTotal(int[] arr){

int total=0;

for(int i=0;i<arr.length;i++){

total +=arr[i];

}

return total;

}

public static void main(String[] args) {

// Array of int type initialized with test data

int[] arr = new int[]{12, 45, 98, 33, 54};

// Calling getLowest() method for getting total

int total = getTotal(arr);

System.out.println(“Total of given vales in array: “+total);

// Calling getLowest() method for getting average

double average = getAverage(arr);

System.out.println(“Average of given vales in array: “+average);

 

// Calling getHighest() method for getting highest value

int highest = getHighest(arr);

System.out.println(“Highest Value in given array is: “+highest);

// Calling getLowest() method for getting lowest value

int lowest = getLowest(arr);

System.out.println(“Lowest Value in given array is: “+lowest);

}

}

Ouput:

Still stressed from student homework?
Get quality assistance from academic writers!