Question: THE ASSIGNMENT Write a program that accepts an n x n matrix . The program should also: Total and display the sum of each row
THE ASSIGNMENT Write a program that accepts an n x n matrix. The program should also:
Total and display the sum of each row and each column
Find the product of each row and column
Find the highest and lowest value within the array
Constraints
The number of rows and columns should be the same
Use nested for loops and methods
Requirements
Build your matrix based upon user input (e.g., ask how many rows and columns - they must be the same)
Use a method to display the matrix (use tabs between each element)
Create methods to display the following:
o Method 1: The sum of each row o Method 2: The sum of each column o Method 3: The product of each row o Method 4: The product of each column o Method 5: The highest value in the matrix o Method 6: The lowest value in the matrix
Loop the program to run it until the user wishes to exit
In the example below, the numbers ARE NOT hard coded. Your program should ask the user for input.
This is what I have: MY SOLUTION
//Imports import java.util.Scanner; import java.io.*;
//Begin Main Method public static void main(String[] args) { Scanner sc = new Scanner(System.in); Console con=System.console();
//Variables int i; String answer; //do loop System.out.println("Welcome! This program that accepts an n x n matrix," + " totals the rows and columns & finds the product of each" + " row and column."); //do loop beginning do{ System.out.println("Enter number of rows & columns (they will be the same):"); i=Integer.parseInt(con.readLine());//check int array[][]=new int[i][i]; for(int j=0;j
} System.out.println(); } rowSum(array,i); columnSum(array,i); rowProduct(array,i); columnProduct(array,i); max(array,i); min(array,i); //end program(?) System.out.print("Would you like to run the program again? (Y for Yes, N for No): "); answer = sc.next(); } while(answer.equalsIgnoreCase("Y")); //end loop System.out.println("Thank you for using the program! Goodbye!");
} //End Main Method //Other methods //add intro public static void rowSum(int array[][], int i){ int sum = 0; //asume sum is 0 at first System.out.println("Row Totals *****************"); for(int j=0;jmax) { max=array[j][k]; } } } System.out.println("The greatest value in the matrix is : "+ max); } //add intro public static void min(int array[][],int i){ int min=array[0][0];//start at 0 for(int j=0;j
THE PROBLEM
It compiles correctly but when I go to run the program I get:
run: Enter number of rows & columns (they will be the same): Exception in thread "main" java.lang.NullPointerException
I can't figure out where I messed up. Any help would be appreciated.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
