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 {
- //Inserting the scanner
- static Scanner input = new Scanner(System.in);
- //main method
- public static void main(String[] args)
- {
- int number = 0;
- int integer = userMenu(number);
- //printing the method
- menuChoices(integer);
- }
- //method for usermenu and user input
- 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;
- }
- //method for the menu choices
- public static void menuChoices(int integer)
- {
- //inserting a switch for the options
- switch(integer)
- {
- case 1:
- //printing the array and results
- 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];
- }
- }
- //average formulas
- averageOfArray = (sumOfArray / (sizeRow * sizeColumn));
- averageOfMajor = (sumOfMajor / (sizeRow));
- averageOfMinor = (sumOfMinor / (sizeRow));
- //printing all the results
- 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));
- }
- //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;
- }
- }
- //method for rounding the numbers
- public static double roundingNumbers(double rounded)
- {
- rounded = Math.round(rounded * 100);
- rounded = rounded/100;
- return rounded;
- }
- //creating the method for asking for user input of array parameters
- public static int[][] creatingRandomArray()
- {
- float sumOfArray = 0, sumOfMajor = 0, sumOfMinor = 0;
- double averageOfMinor = 0, averageOfArray = 0, averageOfMajor = 0;
- 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];
- //loops to input random numberes into the array
- 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;
- sumOfArray += arr[row][column];
- }
- sumOfMajor += arr[row][row];
- }
- for(int row = rows1 - 1; row != 0; row--)
- {
- sumOfMinor += arr[row][row];
- }
- //average formoulas
- averageOfMajor = (sumOfMajor / (rows1));
- averageOfMinor = (sumOfMinor / (rows1));
- averageOfArray = (sumOfArray / (rows1 * columns1));
- //printing out the results
- System.out.println("The total sum of the array is " + sumOfArray);
- System.out.println("The total average of the array is " + roundingNumbers(averageOfArray));
- sumOfRows1(arr, rows1, columns1);
- sumOfColumns1(arr, rows1, columns1);
- 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));
- return arr;
- }
- //method for the rows number 2!!!
- public static void sumOfRows1(int[][] arr, int rows1, int columns1)
- {
- double sum = 0;
- for(int row = 0; row < rows1; row++)
- {
- sum = 0;
- for(int column = 0; column < columns1; column++)
- {
- sum += arr[row][column];
- }
- System.out.println("The sum of row " + (row + 1) + " is "+ sum + " and the average is " + avgOfRows(arr, rows1, sum));
- }
- System.out.println("The max average of rows is " + maxOfRows(arr, rows1, sum));
- }
- //method for the columns number 2!!!!
- public static void sumOfColumns1(int[][] arr, int rows1, int columns1)
- {
- float sum1 = 0;
- for(int column = 0; column < columns1; column++)
- {
- sum1 = 0;
- for(int row = 0; row < rows1; row++)
- {
- sum1 += arr[row][column];
- }
- System.out.println("The sum of column " + (column + 1) + " is "+ sum1 + " and the average is " + avgOfColumns(arr, columns1, sum1));
- }
- System.out.println("The max average of columns is " + maxOfColumns(arr, columns1, sum1));
- }
- //printing the random array
- 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();
- }
- }
- //finding the sum of the numbers and average
- 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));
- }
- //finding the sum of the columns and average
- 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));
- }
- //method for average of columns
- 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 max;
- }
- //method for average of rows
- 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;
- }
- //method to find the max within the columns
- 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;
- }
- //method for finding max within rows
- public static double maxOfRows(int[][] arr, int sizeRows, double sum)
- {
- double avg = 0;
- double max = 0;
- for(int row = 0; row < sizeRows; row++)
- {
- avg = (sum / sizeRows);
- avg = roundingNumbers(avg);
- }
- if(avg > max)
- {
- max = avg;
- }
- return max;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement