Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone please help me fix this code in java. There is two error in the division method and at the end when I return.

Can someone please help me fix this code in java. There is two error in the division method and at the end when I return. I commented out where are they. Thank you!

import java.util.Scanner; public class HW3part1 { public static void main(String[] args){ //start main Scanner keyboard = new Scanner(System.in); System.out.println(" Please enter the number of row for Matrix A: "); int row1 = keyboard.nextInt(); System.out.println(" Please enter the number of column for Matrix A: "); int column1 = keyboard.nextInt(); System.out.println(" Please enter the number of row for Matrix B: "); int row2 = keyboard.nextInt(); System.out.println(" Please enter the number of column for Matrix B: "); int column2 = keyboard.nextInt(); /* System.out.println(" Please enter the number of Matrix A from left to right and top to the bottom "); int rc1 = keyboard.nextInt(); System.out.println(" Please enter the number of Matrix B from left to right and top to the bottom "); int rc2 = keyboard.nextInt(); int A[][] = { {3, 4, 5}, {9, 8, 6} , {4, 7, 2}}; System.out.println(" Please enter the number of Matrix A from left to right and top to the bottom "); //array(A); //array2(B); int B[][] = { {1, 2, 4}, {4, 5, 6} , {7, 3, 9}}; System.out.println(" Please enter the number of Matrix B from left to right and top to the bottom "); input = keyboard.nextLine(); answer = input.charAt(0); System.out.println(answer); System.out.println(input.charAt(0)); */ int[][] A = new int[row1][column1]; System.out.println(" Please enter the number of Matrix A from left to right and top to the bottom "); readMatrix(A, row1, column1); int[][] B = new int[row2][column2]; System.out.println(" Please enter the number of Matrix B from left to right and top to the bottom "); readMatrix(B, row2, column2); System.out.println("Enter an operation to use of your choice"); System.out.println("1:Add , 2:subtract , 3:multiply , 4:divide "); int operation = keyboard.nextInt(); switch(operation) { case 1: AddMatrix(A, row1, column1, B, row2, column2); break; case 2: SubtractMatrix(A, row1, column1, B, row2, column2); break; case 3: MultiplyMatrix(A, row1, column1, B, row2, column2); break; case 4: DivideMatrix(A, row1, column1, B, row2, column2); break; default: System.out.println("invalid option"); } } // end main public static void AddMatrix(int[][] A, int row1, int column1, int[][]B, int row2, int column2){ // new method /*for( int row = 0 ; row < a.length; row++ ){ for ( int column = 0; column < a[row].length; column++ ) { System.out.print( a[row][column] + " "); } System.out.println(); */ if(row1 == row2 && column1 == column2){ int[][] result = new int[row1][column2]; for( int row = 0 ; row < row1; row++ ){ for ( int column = 0; column < column1; column++ ) { result [row][column] = A[row][column] + B[row][column]; } } System.out.println("result of matrix"); printMatrix(result, row1, column1); } // end of if else { System.out.println("not possible"); } } // end of array public static void SubtractMatrix(int[][] A, int row1, int column1, int[][]B, int row2, int column2) // new method 2 { /* for( int row = 0 ; row < b.length; row++ ){ for ( int column = 0; column < column1; column++ ) { System.out.print( b[row][column] + " "); } System.out.println(); */ if(row1 == row2 && column1 == column2){ int[][] result = new int[row1][column2]; for(int row= 0; row < row1; row++){ for(int column = 0; column < column2; column++){ result[row][column] = A[row][column] - B[row][column]; } } System.out.println("result is"); printMatrix(result, row1, column1); } // end if else { System.out.println("not possible"); } } // end of method 2 public static void MultiplyMatrix(int[][] A, int row1, int column1, int[][]B, int row2, int column2) //method 3 { if (column1 == row2){ int[][] result = new int[row1][row2]; for(int row=0; row< row1; row++){ for(int column = 0; column < column2; column++){ int sum = 0; for(int h=0;h < row2; h++){ sum += A[row][h] * B[h][column]; } result[row][column] = sum; } } System.out.println("result of matrix: "); printMatrix(result,row1, column1); } else { System.out.println("not possible"); } } // end of method public static void DivideMatrix(int[][] A, int row1, int column1, int[][]B, int row2, int column2) //method 4 { int[][]BInverse = reverseMatrix(B, row2, column2); //error // then multiply both. MultiplyMatrix(A, row1, column1, BInverse, column2, row2); } //end of method public static void readMatrix(int[][] A, int row1, int column1){ Scanner keyboard = new Scanner(System.in); for(int row=0; row< row1; row++){ for(int column=0; column < column1; column++){ A[row][column] = keyboard.nextInt(); } } } public static void printMatrix(int[][] A, int rrow, int col){ for(int row=0; rowout.println(A[row][column] + " "); } System.out.println(); } } public static int reverseMatrix(int[][] A, int rrow, int col){ int result[][] = new int[col][rrow]; for(int row=0;row                        

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

4th Edition

0619064625, 978-0619064624

More Books

Students also viewed these Databases questions

Question

2. To compare the costs of alternative training programs.

Answered: 1 week ago