Question: MainProgram.java import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class MainProgram { public static SquareMatrix read(String location) throws IOException { File file = new
MainProgram.java
import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class MainProgram { public static SquareMatrix read(String location) throws IOException { File file = new File(location); Scanner input = new Scanner(file); int size = input.nextInt(); int[][] matrix = new int[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { matrix[i][j] = input.nextInt(); } } input.close(); return new SquareMatrix(matrix); } public static void write(String location, StringBuilder builder) throws IOException { File file = new File(location); PrintWriter writer = new PrintWriter(file); writer.println(builder); writer.close(); } public static void main(String[] args) throws IOException { String location = "data" + File.separator + "matrix.txt"; String otherLocation = "data" + File.separator + "other_matrix.txt"; String outputLocation = "data" + File.separator + "output.txt"; // Create object to append string output of matrix StringBuilder builder = new StringBuilder(" "); // TODO : Create matrices for testing // TODO : Test operations of Square Matrices // write to output file write(outputLocation, builder); } } -----------------------------------------------------
SquareMatrix.java
public class SquareMatrix { //TODO: Update Class @Override public String toString() { StringBuilder result = new StringBuilder(" "); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { result.append(matrix[i][j]).append("\t"); } result.append(" "); } return result.toString(); } } ----------------------------------------------------------------------------------
matrix.txt
3 4 3 6 5 4 3 8 5 4
----------------------------------------------------------
other_matrix.txt
3 1 2 1 2 1 2 1 2 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
