This is a homework assignment for my csci 1302 class. The assignment is for the program to read a matrix of numbers from an input file (provided by the user), count up the number of rows and columns in the matrix, store the matrix into an array, go through the array and check for any errors (an error is defined as any number that differs by more than 1 from all of its neighbors), correct any errors (corrections made by taking the average of all its neighbors), and after correcting all errors it goes through the corrected matrix and changes each number to a character in order to print an image based on the matrix. Both the matrix class and the driver program have been completely debugged. When I execute the driver program, the only thing that runs is the constructor. None of the methods printed afterward will run. PLEASE HELP!
This is the input file:
1 2 1 4 5 3 6 8 0
2 4 3 6 2 6 4 8 3
5 1 3 6 8 1 4 3 5
6 8 4 5 9 7 3 1 2
8 6 5 4 9 7 3 1 2
5 6 8 4 3 2 1 8 9
5 8 4 3 1 2 1 4 5
8 5 6 4 7 4 5 6 4
9 8 4 5 3 2 4 5 7
This is the matrix class:
import java.util.*; import java.io.*; public class MatrixFile { private int[][] matrix = new int[30][30]; private String[][] newMatrix = new String[30][30]; private int i, j, numRows, numColumns; private String rowLine; public MatrixFile() throws IOException { Scanner fileScan = new Scanner(new File("file1.txt")); int numRows = 0, numColumns = 0; String rowLine; System.out.println("reading from the file and counting the rows and columns"); while (fileScan.hasNext()) { rowLine = (fileScan.nextLine()); numColumns = (rowLine.length()/2+1); numRows++; } System.out.println("The number of columns is: " + numColumns); System.out.println("The number of rows is: " + numRows); /******************************************************************/ Scanner fileScan2 = new Scanner(new File("file1.txt")); for (i=0; i<numRows; i++) { for (j=0; j<numColumns; j++) { matrix[i][j] = fileScan2.nextInt(); System.out.print(matrix[i][j]); } System.out.println(); } } public void detectErrors() { for (i=0; i<numRows; i++) { for (j=0; j<numColumns; j++) { if ((i==0)&&(j==0)) checkUpperLeft(); else if ((i==0)&&(j==(numColumns-1))) checkUpperRight(); else if ((i<(numRows-1))&&(j==0)) checkLeft(); else if ((i<(numRows-1))&&(j==(numColumns-1))) checkRight(); else if ((i==(numRows-1))&&(j==0)) checkLowerLeft(); else if ((i==(numRows-1))&&(j==(numColumns-1))) checkLowerRight(); else if ((i==0)&&(j<(numColumns-1))) checkUpperRow(); else if ((i<(numRows-1))&&(j<(numColumns-1))) checkMiddle(); else if ((i==(numRows-1))&&(j<(numColumns-1))) checkLowerRow(); System.out.print(matrix[i][j]); } System.out.println(); } } private void checkUpperLeft() { if ((Math.abs(matrix[i][j]-matrix[i][j+1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j+1])>1) &&(Math.abs(matrix[i][j]-matrix[i+1][j])>1)) { correctUpperLeft(); } } private void checkUpperRight() { if ((Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j-1])>1) &&(Math.abs(matrix[i][j]-matrix[i+1][j])>1)) { correctUpperRight(); } } private void checkLeft() { if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j+1])>1) &&((Math.abs(matrix[i][j]-matrix[i][j+1]))>1)&&((Math.abs(matrix[i][j]-matrix[i+1][j+1])>1) &&(Math.abs(matrix[i][j]-matrix[i+1][j])>1))) { correctLeft(); } } private void checkRight() { if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j-1])>1) &&(Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j-1])>1) &&(Math.abs(matrix[i][j]-matrix[i+1][j])>1)) { correctRight(); } } private void checkLowerLeft() { if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j+1])>1) &&(Math.abs(matrix[i][j]-matrix[i][j+1])>1)) { correctLowerLeft(); } } private void checkLowerRight() { if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j-1])>1) &&(Math.abs(matrix[i][j]-matrix[i][j-1])>1)) { correctLowerRight(); } } private void checkMiddle() { if ((Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j+1])>1) &&(Math.abs(matrix[i][j]-matrix[i][j+1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j+1])>1) &&(Math.abs(matrix[i][j]-matrix[i+1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j-1])>1) &&(Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j-1])>1)) { correctMiddle(); } } private void checkUpperRow() { if ((Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j-1])>1) &&(Math.abs(matrix[i][j]-matrix[i+1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i+1][j+1])>1) &&(Math.abs(matrix[i][j]-matrix[i][j+1])>1)) { correctUpperRow(); } } private void checkLowerRow() { if ((Math.abs(matrix[i][j]-matrix[i][j-1])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j-1])>1) &&(Math.abs(matrix[i][j]-matrix[i-1][j])>1)&&(Math.abs(matrix[i][j]-matrix[i-1][j+1])>1) &&(Math.abs(matrix[i][j]-matrix[i][j+1])>1)); { correctLowerRow(); } } private void correctUpperLeft() { matrix[i][j]=((matrix[i][j+1]+matrix[i+1][j+1]+matrix[i+1][j])/3); } private void correctUpperRight() { matrix[i][j]=((matrix[i][j-1]+matrix[i+1][j-1]+matrix[i-1][j])/3); } private void correctLeft() { matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1]+matrix[i+1][j+1]+matrix[i+1][j])/5); } private void correctRight() { matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j-1]+matrix[i][j-1]+matrix[i+1][j-1]+matrix[i+1][j])/5); } private void correctLowerLeft() { matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1])/3); } private void correctLowerRight() { matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j-1]+matrix[i][j-1])/3); } private void correctMiddle() { matrix[i][j]=((matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1]+matrix[i+1][j+1]+matrix[i+1][j]+matrix[i+1][j-1] +matrix[i][j-1]+matrix[i-1][j-1])/8); } private void correctUpperRow() { matrix[i][j]=((matrix[i][j-1]+matrix[i+1][j-1]+matrix[i+1][j]+matrix[i+1][j+1]+matrix[i][j+1])/5); } private void correctLowerRow() { matrix[i][j]=((matrix[i][j-1]+matrix[i-1][j-1]+matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1])/5); } public void createImage() { for(i=0; i<numRows; i++) { for (j=0; j<numColumns; j++) { if ((matrix[i][j]>=0)&&(matrix[i][j]<3)) newMatrix[i][j]=(" "); else if ((matrix[i][j]>=3)&&(matrix[i][j]<6)) newMatrix[i][j]=("."); else if ((matrix[i][j]>=6)&&(matrix[i][j]<9)) newMatrix[i][j]=("+"); else if ((matrix[i][j]==9)) newMatrix[i][j]=("#"); } } } public String toString() { String image; image=""; for(i=0; i<numRows; i++) { for (j=0; j<numColumns; j++) { image += (newMatrix[i][j] + " "); } image += ("\n"); } return image; } }
This is the driver program:
import java.io.*; public class MatrixFileTester { public static void main(String[] args) throws IOException { MatrixFile myMatrix = new MatrixFile(); System.out.println(); myMatrix.detectErrors(); myMatrix.createImage(); System.out.print(myMatrix); } }
And here is my output:
----jGRASP exec: java MatrixFileTester
reading from the file and counting the rows and columns
The number of columns is: 9
The number of rows is: 9
121453680
243626483
513681435
684597312
865497312
568432189
584312145
856474564
984532457
----jGRASP: operation complete.