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 Option 1:

ID: 3834272 • Letter: W

Question

Write a program which does the following:

Give the user a menu choice

Option 1: Randomly Generate a 2D array. This 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 min and max of the random number formula.) The random numbers should be integers.

Option 2: Populate an array using File 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 file 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 2D 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.

Major Diagonal: runs from upper                    Minor Diagonal: runs from upper right
left to lower right                                              to lower left

1 2 3       1 2 3        1 2                                  1 2 3       1 2 3        1 2
3 4 5       4 5 6      3 4                                  3 4 5       4 5 6         3 4
7 8 9                         5 6                                  7 8 9                         5 6

Write a program which does the following: Give the user a menu choice o Option 1: Randomly Generate a 2D array This 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 min and max of the random number formula.) he random numbers should be integers. o Option 2: Populate an array using File 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 file 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 o Display the array in table format. (Print the 2D array) o Calculate and display the sum and average of the entire array o Calculate and display the sum and average of each row. o Calculate and display the sum and average of each column. o Calculate and display the sum and average of the major and minor diagonals see below. o Display the row and col with the highest average. o Display the row and col with the lowest average Be sure to use appropriate methods or the program will be worth no credit Major Diagonal: runs from upper Minor Diagonal: runs from upper right left to lower right to lower left 123 1 2 3 2 1 2 1 2 3 12 345 456 34 3 4 5 456 34 789 56 789 56

Explanation / Answer

Hi,

I could complete only below code in the time given-

import java.util.*;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class HelloWorld{

public static void main(String[] args) {
int rows,cols,sum=0,rowsum=0,colsum=0;
double avg=0.0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of rows");
rows=in.nextInt();
System.out.println("Enter the number of columns");
cols=in.nextInt();
int values[][] = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
values[i][j] = ((int) (Math.random() * 10));
System.out.print(values[i][j]);
}
// add a new line
System.out.println();
}
System.out.println("Done");
  
try {
           File file = new File("input.txt");
           FileReader fileReader = new FileReader(file);
           BufferedReader bufferedReader = new BufferedReader(fileReader);
           StringBuffer stringBuffer = new StringBuffer();
           String line;
           while ((line = bufferedReader.readLine()) != null) {
               stringBuffer.append(line);
               stringBuffer.append(" ");
           }
           fileReader.close();
           System.out.println("Contents of file:");
           System.out.println(stringBuffer.toString());
       } catch (IOException e) {
           e.printStackTrace();
       }
       try { File file = new File("input.txt");
FileReader fr = new FileReader(file);
char temparr[] = new char[(int) file.length()];
fr.read(temparr,0,(int) file.length());
String [] tempstring = (new String(temparr)).split(" ");
String array2d[][] = new String [tempstring.length][];
for(int i=0 ; i<tempstring.length; i++)
{
array2d[i]=tempstring[i].split(" ");   
}
       }
       catch (IOException e) {
           e.printStackTrace();
       }
      
       for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
  
sum+=values[i][j];
}
}
System.out.println("Sum of 2D array"+sum);
avg=(sum/(rows*cols));
System.out.println("Avg of 2D array"+avg);
for(int i = 0; i<cols;i++) {
for( int j = 0; j<rows; j++) {
rowsum = rowsum + values[i][j];
}
System.out.println("Sum of row " + rowsum);
rowsum=0;
}
int col = 0;
for(int i=0; i<rows;i++) {
for(int j=0; j<cols; j++) {
colsum = colsum + values[i][j];
}
System.out.println("Sum of column " +" is " + colsum);
colsum=0;
}
}
}

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