I need help writing this in JAVA, please keep it as simple as possible. Use some
ID: 3840579 • Letter: I
Question
I need help writing this in JAVA, please keep it as simple as possible. Use some dummy files for the J file chooser option. Also, my professor wants the commands for the 2d array to be in their own methods. For example (public static int SumArray(int[][] arr), public static int SumOfEachColumns(int[][] arr), public static int SumOfEachRows(int[][] arr), public static int AvgOfEachRow(int[][] arr), public static int AvgOfEachColumn(int[][] arr), etc. Please do not use ArrayList or any other advanced methods. Thank you in advance.
Problem 01: 2D Array Practice (20 points)
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. (Seperate results, highest avg for a row and highest avg for a col)
Display the row and col with the lowest average. (Seperate results, lowest avg for a row and lowest avg for a col)
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
Any questions please feel free to ask any questions.
Explanation / Answer
//Please comment if need any clearification
import java.util.Random;
import java.util.Scanner;
public class MatrixCalculation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = 3;
Matrix mat = null;
//Printing options
System.out.println("[1].Randomly Generate a 2D array.");
System.out.println("[2].Populate an array using File.");
System.out.print(" Enter Choice:");
int ch = sc.nextInt(); //reading choice
if(ch==1){
System.out.print(" Enter Size of Array:");
size = sc.nextInt(); // reading size of array
int arr[][] = getRandArray(size); // creating random array of sixe*size
mat = new Matrix(size, size, arr); // Instantiate matrix objcet
}
else if(ch==2){
System.out.print(" Enter file name : ");
String filename = sc.next(); // reading file name
mat = new Matrix(filename); // instantiate matrix object using file
size = mat.getColumns();
}
else{ // if invalid choice then program will exit
System.out.print(" Invalid input ");
System.exit(0);
}
System.out.println("Matrix : "+mat);// printing Matrix
//Printing Matrix sum and average by calling appropriate method
System.out.println("Matrix Sum : "+mat.getSum()+", Average : "+mat.getAverage());
//Printing row sum and average by calling appropriate method
for(int i=0;i<size;i++){
System.out.println("Row["+i+"] Sum : "+mat.getRowSum(i)+", Average : "+mat.getRowAverage(i));
}
System.out.println();
//Printing Column's sum and average by calling appropriate method
for(int i=0;i<size;i++){
System.out.println("Column["+i+"] Sum : "+mat.getColumnSum(i)+" Average : "+mat.getColumnAverage(i));
}
//Printing major and minor digonal sum and average
System.out.println("Mator Digonal Sum : "+mat.getMajorDigonalSum()+", Average : "+mat.getMajorDigonalAverage());
System.out.println("Minor Digonal Sum : "+mat.getMinorDigonalSum()+", Average : "+mat.getMinorDigonalAverage());
mat.displayHighestAvgRow(); // Printing highest average and row
mat.displayLowestAvgRow(); // Printing lowest average and row
mat.displayHighestAvgCol(); // Printing highest average and column
mat.displayLowestAvgCol(); // Printing lowest average and column
}
public static int[][] getRandArray(int n){
Random rand = new Random();
int[][] array = new int[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
array[i][j] = Math.abs(rand.nextInt()%10);
}
}
return array;
}
}
=====================================================================
public class Matrix {
private int rows;
private int columns;
private int array[][];
public Matrix(int rows ,int columns, int[][] array) {
this.setRows(rows);
this.setColumns(columns);
this.array =array;
}
public Matrix(String filename) {
Scanner sc;
try{
sc = new Scanner(new File(filename));
this.setRows(sc.nextInt());
this.setColumns(sc.nextInt());
for(int i=0; i<rows; i++){
for(int j=0; j<columns; j++){
array[i][j] = sc.nextInt();
}
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getColumns() {
return columns;
}
public void setColumns(int columns) {
this.columns = columns;
}
public int[][] getArray() {
return array;
}
public int getSum(){
int sum =0;
for(int i=0; i<rows; i++){
for(int j=0; j<columns; j++){
sum += array[i][j];
}
}
return sum;
}
public double getAverage(){
double avg = (double)getSum()/(double)(rows*columns);
return avg;
}
public int getRowSum(int row){
int sum= 0;
for(int j=0; j<columns; j++){
sum += array[row][j];
}
return sum;
}
public double getRowAverage(int row){
double avg= (double)getRowSum(row)/(double)columns;
return avg;
}
public int getColumnSum(int col){
int sum= 0;
for(int i=0; i<rows; i++){
sum += array[i][col];
}
return sum;
}
public double getColumnAverage(int col){
double avg= (double)getColumnSum(col)/(double)rows;
return avg;
}
public int getMajorDigonalSum(){
int sum= 0;
for(int i=0,j=0; i<rows&&j<columns; i++,j++){
sum += array[i][j];
}
return sum;
}
public double getMajorDigonalAverage(){
int s = rows<columns ? rows :columns;
double avg= (double)getMajorDigonalSum()/(double)s;
return avg;
}
public int getMinorDigonalSum(){
int sum= 0;
for(int i=rows-1,j=0; i>=0&&j<columns; i--,j++){
sum += array[i][j];
}
return sum;
}
public double getMinorDigonalAverage(){
int s = rows<columns ? rows :columns;
double avg= (double)getMinorDigonalSum()/(double)s;
return avg;
}
public void displayHighestAvgCol(){
int index=0;
double maxAvg=0.0;
for(int j=0; j<columns; j++){
if(getColumnAverage(j)>=maxAvg){
maxAvg = getColumnAverage(j);
index = j;
}
}
System.out.println("Column :");
for(int j=0; j<rows; j++){
System.out.println(" "+array[j][index]);
}
System.out.println("Highest Average : "+maxAvg);
}
public void displayLowestAvgCol(){
int index=0;
double avg=Double.MAX_VALUE;
for(int j=0; j<columns; j++){
if(getColumnAverage(j)<avg){
avg = getColumnAverage(j);
index = j;
}
}
System.out.println("Column :");
for(int j=0; j<rows; j++){
System.out.println(" "+array[j][index]);
}
System.out.println("Lowest Average : "+avg);
}
public void displayLowestAvgRow(){
int index=0;
double avg=Double.MAX_VALUE;
for(int j=0; j<rows; j++){
if(getRowAverage(j)<avg){
avg = getRowAverage(j);
index = j;
}
}
System.out.println("Row :");
for(int j=0; j<rows; j++){
System.out.print(" "+array[index][j]);
}
System.out.println();
System.out.println("Lowest Average : "+avg);
}
public void displayHighestAvgRow(){
int index=0;
double avg=0.0;
for(int j=0; j<rows; j++){
if(getRowAverage(j)<avg){
avg = getRowAverage(j);
index = j;
}
}
System.out.println("Row :");
for(int j=0; j<rows; j++){
System.out.print(" "+array[index][j]);
}
System.out.println();
System.out.println("Highest Average : "+avg);
}
public boolean isEqual(Matrix mat){
if(mat.getColumns()!=columns || mat.getRows() != rows)
return false;
int[][] arr = mat.getArray();
for(int i=0; i<rows; i++){
for(int j=0; j<columns; j++){
if(arr[i][j] != array[i][j])
return false;
}
}
return true;
}
public String toString(){
String str = "";
for(int i=0; i<rows; i++){
for(int j=0; j<columns; j++){
str += ""+array[i][j]+" ";
}
str += " ";
}
str += " ";
return str;
}
public void multiply(int k){
for(int i=0; i<rows; i++){
for(int j=0; j<columns; j++){
array[i][j] *= k;
}
}
}
public Matrix multiply(Matrix mat) throws Exception{
if(columns != mat.getRows())
throw new Exception("Not compatable");
int arr[][] = new int[rows][mat.getColumns()];
int array2[][] = mat.getArray();
for(int i=0; i<rows; i++){
for(int j=0; j<mat.getColumns(); j++){
for(int k=0; k<columns; k++){
arr[i][j] = arr[i][j] + array[i][k] * array2[k][j];
}
}
}
return new Matrix(rows, mat.getColumns(), arr);
}
}