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: 3836103 • 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 left to lower right 123 123 12 345 456 34 789 56 Minor Diagonal: runs from upper right to lower left 123 123 12 345 456 34 789 56

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.may;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

/**
*
* @author Sam
*/
public class Matrix {
    static int[][] generateMatrix(int rows, int cols, int min, int max) {
        int[][] mat = new int[rows][cols];
        for (int i = 0; i<rows; i++)
            for (int j = 0; j < cols; j++)
                mat[i][j] = (int) (Math.round(Math.random()*(max-min)) + min);
        return mat;
    }
  
    static int[][] readMatrix(File file) throws FileNotFoundException{
        Scanner sc = new Scanner(file);
        int rows = sc.nextInt();
        int cols = sc.nextInt();
        int[][] mat = new int[rows][cols];
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                mat[i][j] = sc.nextInt();
        return mat;
    }
  
    static int sum(int[][] matrix) {
        int sum = 0;
        for (int[] matrix1 : matrix) {
            for (int i : matrix1) {
                sum += i;
            }
        }
        return sum;
    }
  
    static double avg(int[][] matrix) {
        int sum = sum(matrix);
        return (double)sum /(matrix.length * matrix[0].length);
    }
  
    static int[] colSum(int[][] matrix) {
        int[] sum = new int[matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            sum[i] = 0;
            for (int j : matrix[i])
                sum[i] += j;
        }
        return sum;
    }
  
    static double[] colAvg(int[][] matrix) {
        int[] sum = colSum(matrix);
        double[] avg = new double[matrix.length];
        for (int i = 0; i < sum.length; i++)
            avg[i] = (double)sum[i]/matrix[0].length;
        return avg;
    }
  
    static int[] rowSum(int[][] matrix) {
        int[] sum = new int[matrix[0].length];
        for (int i = 0; i < matrix[0].length; i++) {
            sum[i] = 0;
            for (int[] matrix1 : matrix) {
                sum[i] += matrix1[i];
            }
        }
        return sum;
    }
  
    static double[] rowAvg(int[][] matrix) {
        int[] sum = rowSum(matrix);
        double[] avg = new double[matrix[0].length];
        for (int i = 0; i < sum.length; i++)
            avg[i] = (double)sum[i]/matrix.length;
        return avg;
    }
  
    static int sumMajor(int[][] matrix) {
        int sum = 0;
        for(int i = 0; i < Math.min(matrix.length, matrix[0].length); i++)
            sum += matrix[i][i];
        return sum;
    }
  
    static double avgMajor(int[][] matrix){
        return (double)sumMajor(matrix)/Math.min(matrix.length, matrix[0].length);
    }
  
    static int sumMinor(int[][] matrix) {
        int sum = 0;
        int row = 0;
        int col = matrix[0].length;
        for(int i = 0; i < Math.min(matrix.length, matrix[0].length); i++)
            sum += matrix[row++][col--];
        return sum;
    }
  
    static double avgMinor(int[][] matrix){
        return (double)sumMinor(matrix)/Math.min(matrix.length, matrix[0].length);
    }
  
    static void printMaxRowCol(int[][] matrix) {
        int max = 0;
        int[] sum = rowSum(matrix);
        for (int i = 0; i < sum.length; i++)
            if (sum[i] > sum[max])
                max = i;
        System.out.println("Max row: ");
        for (int i = 0; i < matrix[0].length; i++)
            System.out.print(matrix[max][i]+" ");
        System.out.println("");
      
        sum = rowSum(matrix);
        for (int i = 0; i < sum.length; i++)
            if (sum[i] > sum[max])
                max = i;
        System.out.println("Max col: ");
        for (int i = 0; i < matrix.length; i++)
            System.out.print(matrix[i][max]+" ");
        System.out.println("");
    }
  
    static void printMinRowCol(int[][] matrix) {
        int min = 0;
        int[] sum = rowSum(matrix);
        for (int i = 0; i < sum.length; i++)
            if (sum[i] < sum[min])
                min = i;
        System.out.println("Max row: ");
        for (int i = 0; i < matrix[0].length; i++)
            System.out.print(matrix[min][i]+" ");
        System.out.println("");
      
        sum = rowSum(matrix);
        for (int i = 0; i < sum.length; i++)
            if (sum[i] < sum[min])
                min = i;
        System.out.println("Max col: ");
        for (int i = 0; i < matrix.length; i++)
            System.out.print(matrix[i][min]+" ");
        System.out.println("");
    }
  
    public static void main(String[] args) throws FileNotFoundException {
        int[][] matrix = null;
        Scanner sc = new Scanner(System.in);
        System.out.println("1. Generate random matrix");
        System.out.println("2. Read matrix from file");
        switch (sc.nextInt()) {
            case 1:
                System.out.println("Enter number of rows:");
                int rows = sc.nextInt();
                System.out.println("Enter number of cols:");
                int cols = sc.nextInt();
                System.out.println("Enter minimum:");
                int min = sc.nextInt();
                System.out.println("Enter maximum:");
                int max = sc.nextInt();
                matrix = generateMatrix(rows, cols, min, max);
                break;
            case 2:
                System.out.println("Enter file name");
                matrix = readMatrix(new File(sc.next()));
        }
        System.out.println("Matrix sum: " + sum(matrix));
        System.out.println("Matrix avg: " + avg(matrix));
        System.out.println("Matrix row sum: " + Arrays.toString(rowSum(matrix)));
        System.out.println("Matrix row avg: " + Arrays.toString(rowAvg(matrix)));
        System.out.println("Matrix col sum: " + Arrays.toString(colSum(matrix)));
        System.out.println("Matrix col avg: " + Arrays.toString(colAvg(matrix)));
        System.out.println("Matrix Major sum: " + sumMajor(matrix));
        System.out.println("Matrix Major avg: " + avgMajor(matrix));
        System.out.println("Matrix Major sum: " + sumMinor(matrix));
        System.out.println("Matrix Major avg: " + avgMinor(matrix));
        printMaxRowCol(matrix);
        printMinRowCol(matrix);
    }
}

I kept the the code very simple, hence I havent comented the code. If you need the code to be commented, please feel free to comment below. I shall be glad to help you with the code;

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