I need help with some code I have started but can\'t finish. In my main() method
ID: 3541546 • Letter: I
Question
I need help with some code I have started but can't finish.
In my main() method I need to read in two files matrixA.txt and matrixB.txt. Each text file is three rows of 3 integers all separated with a space. I have the 2 files saved on my computer.
-I need a method called readMatrix() to read in the values form the file and store them in a 2-dimensional array (3*3) - Another method called displayMatrix(int [][]arr) to display these matrices in tabular form. -Then I need to prompt the user to select from a menu method public static int menu() 1.) Addition 2.) Multiply 3.) EXIT -Once the user responds with their input it should call the right method and perform the matrix arithmetic and display the result using the displayMatrix() method. - public static void add(int arr1[][], int arr2 [][]) - publuc static void mult(int arr1[][], int arr2[][]) Within these methods I need to call writeMatrix() method from within these two method to write the values from the 2-dimensional array to the file. Other method to use- public static void displayMatrix(int arr[][]) // displays matrix in tabular form public static void readMatrix(String fileName, int [][] arr) // read values from files into 2-dimensioanl array public static void writeMethod(String fileName, int [][]arr) // writes values from 2 dimensional array to filename specified "sum.txt" and "mult.txt" will be the names of the files to be written to My CODE is below. Thanks for the help.
java.IO.File;
java.IO.IOException;
public class matrix
{
public static void main(String [] args) throws FileNotFoundException
{
boolean go = true;
int choice;
while(go)
{
displayMenu();
choice = menu();
switch(choice)
{
case 1: add();
break;
case 2: mult();
break;
case 3: System.exit(0);
break;
}
}
System.out.println("Exit Program");
}
public static void displayMenu()
{
System.out.print(" 1. Addition ");
System.out.print(" 2. Multiply ");
System.out.print(" 3. EXIT");
}
public static int menu()
{
Scanner in = new Scanner(System.in);
System.out.println(" Please enter your choice :");
return in.nextInt();
}
public static void add( int arr1[][], int arr2[][])
{
int i;
int j;
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
sum[i][j]= arr1[i][j] + arr2[i][j];
}
}
}
public static void mult(int arr1[][], int arr2[][])
{
int i;
int j;
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
mult[i][j]=0;
for(int k=0; k<l; k++)
{
mult[i][j]+= arr1[i][k]*arr2[k][j];
}
}
}
}
public static void readMatrix(String filename, int[][]arr1) // This method is done wrong. Not sure how to do this.
{
Scanner matrixA = new Scanner(new filename ("sum.txt"));
filename =
int [][] arr1 = new arr1[3][3];
int [][] arr2 = new arr2[3][3];
readMatrix(matrixA.txt);
readMatrix(matrixB.txt);
}
}
Explanation / Answer
import java.io.*;
import java.util.*;
public class matrix
{
public static void main(String [] args) throws IOException
{
boolean go = true;
int choice;
int[][] matrixA = new int[3][3];
int[][] matrixB = new int[3][3];
readMatrix("matrixA.txt",matrixA);
readMatrix("matrixB.txt",matrixB);
while(go)
{
displayMenu();
choice = menu();
switch(choice)
{
case 1: add(matrixA,matrixB);
break;
case 2: mult(matrixA,matrixB);
break;
case 3: System.exit(0);
break;
}
}
System.out.println("Exit Program");
}
public static void displayMenu()
{
System.out.print(" 1. Addition ");
System.out.print(" 2. Multiply ");
System.out.print(" 3. EXIT");
}
public static int menu()
{
Scanner in = new Scanner(System.in);
System.out.println(" Please enter your choice :");
return in.nextInt();
}
public static void add( int arr1[][], int arr2[][]) throws IOException
{
int[][] sum = new int[3][3];
for(int i=0; i<arr1.length; i++)
{
for(int j=0; j<arr1[i].length; j++)
{
sum[i][j]= arr1[i][j] + arr2[i][j];
}
}
writeMethod("sum.txt",sum);
}
public static void mult(int arr1[][], int arr2[][]) throws IOException
{
int[][] mult = new int[3][3];
for(int i=0; i<arr1.length; i++)
{
for(int j=0; j<arr1[i].length; j++)
{
mult[i][j]=0;
for(int k=0; k<3; k++)
{
mult[i][j]+= arr1[i][k]*arr2[k][j];
}
}
}
writeMethod("mult.txt",mult);
}
public static void readMatrix(String filename, int[][] arr1) throws IOException// This method is done wrong. Not sure how to do this.
{
Scanner matrixA = new Scanner(new File(filename));
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
arr1[i][j] = matrixA.nextInt();
}
public static void writeMethod(String fileName, int [][]arr) throws IOException
{
PrintWriter pw = new PrintWriter(new File(fileName));
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
pw.write(arr[i][j] + " ");
}
pw.write(" ");
}
pw.close();
}
}