In: Computer Science
JAVA Problem 1: [15 marks] Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an array of integer values with the array size of 5 and invoke the first method to get and display the average of the first array, and then create an array of double values with the array size of 6 and invoke the second method to get and display the average of the second array. You can generate the array values using the Math.random() method with the range from 0 to 9. Keep two decimals for the average values.
Problem 2: [15 marks] Find the largest value of each row of a 2D array (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if the 2D array passed to the method is: 8 5 5 6 6 3 6 5 4 5 2 5 4 5 2 8 8 5 1 6 The method returns an array of integers which are the largest values from each row as follows: 8 6 5 8 In the main method, create a 2D array of integer values with the array size 4 by 5 and invoke the method to find and display the largest values from each row of the 2D array. You can generate the values of the 2D array using the Math.random() method with the range from 0 to 9.
Question 1:
Java class FindAverage.java :
//Java class with name FindAverage
public class FindAverage {
// main() method
public static void main(String[] args) {
// creating array of integers with
size 5
int[] integerArray = new
int[5];
// using for loop to randomly
generate values
for (int i = 0; i <
integerArray.length; i++) {
// generate
random number and store in the array
integerArray[i]
=(int) (Math.random()*9);
}
// display random numbers
for (int i = 0; i <
integerArray.length; i++) {
System.out.print(integerArray[i] + " ");// print numbers
}
// call method and print average of
integer array
System.out.print("\nAverage of
integer array :");
System.out.printf("%.2f",
average(integerArray));
// creating array of doubles with
size 6
int[] doubleArray = new
int[6];
// using for loop to randomly
generate values
for (int i = 0; i <
doubleArray.length; i++) {
// generate
random number and store in the array
doubleArray[i]
=(int)(Math.random() *9);
}
System.out.println();//used for new
line
// display random numbers
for (int i = 0; i <
doubleArray.length; i++) {
System.out.print(doubleArray[i] + " ");// print numbers
}
// call method and print average of
integer array
System.out.print("\nAverage of
double array :");
System.out.printf("%.2f",
average(doubleArray));
}
public static double average(int[] num) {
// variable used to store sum
int sum = 0;
// using for loop
for (int i = 0; i < num.length;
i++) {
sum = sum +
num[i];// add each element from the array into variable sum
}
// calculate and return
average
return ((double) sum / (double)
num.length);
}
public static double average(double[] num) {
// variable used to store sum
double sum = 0;
// using for loop
for (int i = 0; i < num.length;
i++) {
sum = sum +
num[i];// add each element from the array into variable sum
}
// calculate and return
average
return sum / num.length;
}
}
===========================================
Output :