Question

In: Computer Science

JAVA Problem 1: [15 marks] Find the average of an array of numbers (filename: FindAverage.java) Write...

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.

Solutions

Expert Solution

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 :


Related Solutions

Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
[15 marks] (GetPositiveNumbers.java) Write a method that receives an array of integers as a parameter. The...
[15 marks] (GetPositiveNumbers.java) Write a method that receives an array of integers as a parameter. The method finds and returns an array which contains the positive numbers. For example, if the method receives an array with the following elements: 0 3 -1 2 5 1 -5 2 -2 0 the method should return an array with the following elements: 3 2 5 1 2 In the main method, randomly generate an array of 10 random integers between -5 and 5,...
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
Write a java code to find the following. For numbers 501 to 999, how many numbers...
Write a java code to find the following. For numbers 501 to 999, how many numbers will have the sum of the digits equal to 10. 501, sum of digits=6 502, sum of digits=7 503, sum of digits=8 504, sum of digits=9 505, sum of digits=10 506, sum of digits=11
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
Write a procedure to calculate Average of numbers(integers) using Arrays. Send base address of array in...
Write a procedure to calculate Average of numbers(integers) using Arrays. Send base address of array in register $a1 and Array length in register $a2 to the procedure and return Average in register $v0 to main program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT