Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program which does the following: Give the user a menu choice. Randomly

ID: 3835464 • Letter: W

Question

Write a program which does the following: Give the user a menu choice. Randomly Generate a 2D array. The option will ask the user for the number of rows and columns in the array, as well as the range of numbers to generate. (i.e. The user can choose the mm and max of the random number formula.) The random numbers should be integers Populate an array using Fie I/O. This option will ask the user to enter the name of a file OR use JFileChooser to read data from an input file. The we will have the following format. The first two numbers will be the dimensions of the array (rows and columns). The rest of the numbers will be the data for the array. Once the array has been created using one of the above two options. Display the following results: Display the array in table format (Print the 20 array) Calculate and display the sum and average of the entire array Calculate and display the sum and average of each row. Calculate and display the sum and average of each column Calculate and display the sum and average of the major and minor diagonals "see below. Display the row and col with the highest average Display the row and col with the lowest average Be sure to use appropriate methods or the program will be worth no credit.

Explanation / Answer

import java.io.File;
import java.util.Random;
import java.util.Scanner;

public class MenuArray {
   public static Scanner sc = new Scanner(System.in);
   public static void main(String[] args) {
       int arr[][];
       int choice = getChoice();
       switch(choice){
       case 1:
           arr= getRandomArray();
           display(arr);
           calculate(arr);
           break;
       case 2:
           arr=getFileArray();
           display(arr);
           calculate(arr);
           break;
       case 3:
           System.exit(0);
       }
      
   }
   public static int[][] getRandomArray(){
       Random rand = new Random();
       System.out.print(" Enter Number of rows:");
       int r =sc.nextInt();
       System.out.print(" Enter Number of columns:");
       int c = sc.nextInt();
       System.out.print(" Enter range min:");
       int min = sc.nextInt();
       System.out.print(" Enter range max:");
       int max = sc.nextInt();
       int arr[][] = new int[r][c];
      
       for(int i=0; i<r; i++){
           for(int j=0; j<c; j++){
               arr[i][j] = Math.abs(rand.nextInt((max-min)))+min;
           }
       }
       return arr;
   }
  
   public static int[][] getFileArray(){
       int arr[][] = null;
       try{
           System.out.print("Enter file name:");
           String filename= sc.next();
           Scanner file = new Scanner(new File(filename));
           int r = file.nextInt();
           int c = file.nextInt();
           arr = new int[r][c];
           for(int i=0; i<r; i++){
               for(int j=0; j<c; j++){
                   arr[i][j] = file.nextInt();
               }
           }
       }
       catch(Exception e){
          
       }
       return arr;
   }
   public static void display(int arr[][]){
       for(int i=0; i<arr.length; i++){
           for(int j=0; j<arr[i].length; j++){
               System.out.print(" "+arr[i][j]);
           }
           System.out.println();
       }
       System.out.println();
   }
  
   public static void calculate(int arr[][]){
       int sum = 0;
       int[] rowSum = new int[arr.length];
       double rowAvgMax = 0.0;
       double rowAvgMin = Double.MAX_VALUE;
       int[] colSum = new int[arr[0].length];
       double colAvgMax = 0.0;
       double colAvgMin = Double.MAX_VALUE;
       for(int i=0; i<arr.length; i++){
          
           for(int j=0; j<arr[i].length; j++){
               sum += arr[i][j];
               rowSum[i] += arr[i][j];
               colSum[j] += arr[i][j];
           }
       }
       System.out.println();
       System.out.println("All sum :"+sum);
       for(int i=0; i<rowSum.length; i++){
           System.out.println("Row Sum["+i+"] :"+rowSum[i]+" Average: "+((float)rowSum[i]/(float)rowSum.length));
           if(rowAvgMax<(double)rowSum[i]/(double)rowSum.length){
               rowAvgMax = (double)rowSum[i]/(double)rowSum.length;
           }
           if(rowAvgMin>(double)rowSum[i]/(double)rowSum.length){
               rowAvgMin = (double)rowSum[i]/(double)rowSum.length;
           }
       }
       System.out.println("Row Max Average :"+rowAvgMax+" ");
       System.out.println("Row Min Average :"+rowAvgMin+" ");
      
       for(int i=0; i<colSum.length; i++){
           System.out.println("Column Sum["+i+"] :"+colSum[i]+" Average: "+((float)colSum[i]/(float)colSum.length));
           if(colAvgMax<(double)colSum[i]/(double)colSum.length){
               colAvgMax = (double)colSum[i]/(double)colSum.length;
           }
           if(colAvgMin>(double)colSum[i]/(double)colSum.length){
               colAvgMin = (double)colSum[i]/(double)colSum.length;
           }
       }
       System.out.println("Column Max Average :"+colAvgMax+" ");
       System.out.println("Column Min Average :"+colAvgMin+" ");
       System.out.println();
   }
   public static int getChoice(){
         
       System.out.println("[1] Randomly Generate 2D Array");
       System.out.println("[2] Poulate 2D Array using File");
       System.out.println("[3] Exit");
       System.out.print("Enter your Choice :");
       int ch = sc.nextInt();
       return ch;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote