Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.util.Scanner;
- import javax.swing.JFileChooser;
- import javax.swing.JOptionPane;
- public class Hw11P01 {
- static Scanner input = new Scanner(System.in);
- public static void main(String[] args)
- {
- int number = 0, grabInt = 0, grabChoice = 0;
- int integer = userMenu(number);
- menuChoices(integer);
- }
- public static int userMenu(int number)
- {
- System.out.println("Option 1: Randomly Generate a 2D array");
- System.out.println("Option 2: Populate an array using File I/O");
- System.out.println("Option 3: Exit program");
- System.out.print("Enter your choice here: ");
- number = input.nextInt();
- return number;
- }
- public static void menuChoices(int integer)
- {
- switch(integer)
- {
- case 1:
- int[][] array = creatingRandomArray();
- printingRandomArray(array);
- break;
- case 2:
- //asking user to choose a file
- JOptionPane.showMessageDialog(null, "Choose a file to calculate from");
- //creating jfc as a variable
- JFileChooser jfc = new JFileChooser();
- //creating the array and counter variables
- int counter = 0;
- int x = jfc.showOpenDialog(null);
- int sizeRow = 0, sizeColumn = 0;
- //if user approves the file
- if(x == JFileChooser.APPROVE_OPTION)
- {
- //allowing for jfc to pick a file
- File newFile = jfc.getSelectedFile();
- try
- {
- Scanner in = new Scanner(newFile);
- //making the program grab data from the selected file
- sizeRow = Integer.parseInt(in.nextLine());
- sizeColumn = Integer.parseInt(in.nextLine());
- float sumOfArray = 0, sumOfMajor = 0, sumOfMinor = 0;
- double averageOfMinor = 0, averageOfArray = 0, averageOfMajor = 0;
- int array1[][] = new int[sizeRow][sizeColumn];
- //creating the loop to enter all the data while updating counter
- while(in.hasNext())
- {
- for(int row = 0; row < sizeRow; row++)
- {
- for(int column = 0; column < sizeColumn; column++)
- {
- //adding spaces in between the numbers
- array1[row][column] = Integer.parseInt(in.nextLine());
- sumOfArray += array1[row][column];
- //insert the methods that solve the problem in this loop
- }
- sumOfMajor += array1[row][row];
- }
- for(int row = sizeRow - 1; row != 0; row--)
- {
- sumOfMinor += array1[row][row];
- }
- }
- averageOfArray = (sumOfArray / (sizeRow * sizeColumn));
- averageOfMajor = (sumOfMajor / (sizeRow));
- averageOfMinor = (sumOfMinor / (sizeRow));
- printingRandomArray(array1);
- System.out.println();
- System.out.println("The total sum of the array is " + sumOfArray);
- System.out.println("The total average of the array is " + roundingNumbers(averageOfArray));
- sumOfRows(array1, sizeRow, sizeColumn);
- sumOfColumns(array1, sizeRow, sizeColumn);
- System.out.println("The total sum of the major diagonal is " + sumOfMajor);
- System.out.println("The total average of the major diagonal is " + roundingNumbers(averageOfMajor));
- System.out.println("The total sum of the minor diagonal is " + sumOfMinor);
- System.out.println("The total average of the minor diagonal is " + roundingNumbers(averageOfMinor));
- System.out.println();
- }
- //catches errors
- catch(IOException e)
- {
- System.err.println(e);
- System.exit(1);
- }
- }
- //cancels the jfc
- else if(x == JFileChooser.CANCEL_OPTION)
- {
- JOptionPane.showMessageDialog(null, "Cancelled");
- System.exit(1);
- }
- //tells the user when there was an error
- else if(x == JFileChooser.ERROR_OPTION)
- {
- JOptionPane.showMessageDialog(null, "You caused an error.");
- System.exit(1);
- }
- break;
- case 3:
- System.out.println("You have exited the program!");
- System.exit(1);
- default:
- System.out.print("The entered value is not an option!");
- break;
- }
- }
- public static double roundingNumbers(double rounded)
- {
- rounded = Math.round(rounded * 100);
- rounded = rounded/100;
- return rounded;
- }
- public static int[][] creatingRandomArray()
- {
- System.out.print("You Have Chosen to Randomly Generate a 2D array\n");
- System.out.print("How many rows would you like in this array?: ");
- int rows1 = input.nextInt();
- System.out.print("How many columns would you like in this array?: ");
- int columns1 = input.nextInt();
- System.out.print("What will be the minimum for the array?: ");
- int minimum = input.nextInt();
- System.out.print("What will be the maximum for the array?: ");
- int maximum = input.nextInt();
- int [][] arr = new int[rows1][columns1];
- for(int row = 0; row < arr.length; row++)
- {
- for(int column = 0; column < arr[row].length; column++)
- {
- //creating the random number generator
- int randomNumber = (int)(maximum * Math.random() + minimum);
- //adding spaces in between the numbers
- arr[row][column] = randomNumber;
- }
- }
- return arr;
- }
- public static void printingRandomArray(int[][] arr)
- {
- for(int row = 0; row < arr.length; row++)
- {
- for(int column = 0; column < arr[row].length; column++)
- {
- System.out.print(arr[row][column] + "\t");
- }
- System.out.println();
- }
- }
- public static void sumOfRows(int[][] arr, int sizeRow, int sizeColumn)
- {
- double sum = 0;
- for(int row = 0; row < sizeRow; row++)
- {
- sum = 0;
- for(int column = 0; column < sizeColumn; column++)
- {
- sum += arr[row][column];
- }
- System.out.println("The sum of row " + (row + 1) + " is "+ sum + " and the average is " + avgOfRows(arr, sizeRow, sum));
- }
- System.out.println("The max average of rows is " + maxOfRows(arr, sizeRow, sum));
- }
- public static void sumOfColumns(int[][] arr, int sizeRow, int sizeColumn)
- {
- float sum1 = 0;
- for(int column = 0; column < sizeColumn; column++)
- {
- sum1 = 0;
- for(int row = 0; row < sizeRow; row++)
- {
- sum1 += arr[row][column];
- }
- System.out.println("The sum of column " + (column + 1) + " is "+ sum1 + " and the average is " + avgOfColumns(arr, sizeColumn, sum1));
- }
- System.out.println("The max average of columns is " + maxOfColumns(arr, sizeColumn, sum1));
- }
- public static double avgOfColumns(int[][] arr, int sizeColumn, float sum1)
- {
- double avg = 0;
- double max = 0;
- for(int column = 0; column < sizeColumn; column++)
- {
- avg = (sum1 / sizeColumn);
- avg = roundingNumbers(avg);
- }
- if(max < avg)
- {
- max = avg;
- }
- return avg;
- }
- public static double maxOfColumns(int[][] arr, int sizeColumn, double sum)
- {
- double avg = 0;
- double max = 0;
- for(int column = 0; column < sizeColumn; column++)
- {
- avg = (sum / sizeColumn);
- avg = roundingNumbers(avg);
- }
- if(max < avg)
- {
- max = avg;
- }
- return max;
- }
- public static double maxOfRows(int[][] arr, int sizeRows, double sum)
- {
- double avg = 0;
- double max = 0;
- for(int column = 0; column < sizeRows; column++)
- {
- avg = (sum / sizeRows);
- avg = roundingNumbers(avg);
- }
- if(avg > max)
- {
- max = avg;
- }
- return max;
- }
- public static double avgOfRows(int[][] arr, int sizeRow, double sum)
- {
- double avg = 0;
- for(int row = 0; row < sizeRow; row++)
- {
- avg = (sum / sizeRow);
- avg = roundingNumbers(avg);
- }
- return avg;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement