Use a scanner for the input and printStream for the output C
*Use a scanner for the input and printStream for the output*
Complete this in Java. Your program will read data into each array (4 in all, A & B for addition and subtraction, and C & D for multiplication) from an input file: in2.txt and send all output to: out2.txt. The input file will contain all the numbers required to fill 4 arrays (first two sets of numbers are for the A & B arrays, the remaining two sets of numbers are for the C & D arrays). Each array set of numbers will be preceded by 2 integers; the first for the row dimension and the second for the column dimension as illustrated below (all data will be integers):
3 3
16 -32 1
25 8 12
-5 0 -16
3 3
4 2 -1
11 44 13
25 -8 0
2 3
3 4 -2
-1 0 -2
3 2
3 5
-3 2
1 0
First 2 arrays added should produce a 3 X 3 matrix =
20 -30 0
36 52 25
20 -8 -16
Subtraction of the first minus the second should produce a 3 X 3 matrix =
12 -34 2
14 -36 -1
-30 8 -16
Multiplying the remaining two arrays produces a 2 X 2 matrix =
-5 23
-5 -5
The 3 array operations (static methods) should be contained in their own class (ArrayMath) in a separate file named ArrayMath.java:
// All proper class documentation goes here
public class ArrayMath {
// all static methods (matrix math operations) should go here including their correct documentation.
}
The main method of class Assign_2 will access the ArrayMath methods by referring to the class in which they are defined (Note the period between the class name and method name):
int[ ][ ] addResult = ArrayMath.addArrays(a, b); // adds arrays a & b and returns the resulting array, etc.
The main method of class Assign_2 will:
create the arrays (most likely 7 in all- input A, B, C, D, & results for addition, subtraction, and multiplication)
open the files for I/O
populate the 4 arrays with data from the input file “in2.txt”.
call the methods of ArrayMath to perform the matrix math and return the resulting array
produce clear, meaningful output (out2.txt) to display:
the arrays on which operations are being performed, i.e. A, B, C, & D
the operations resulting arrays
You should name your class Assign_2 (file name will be Assign_2.java). You should also create at least 2 helper methods:
one that is passed the input file Scanner and returns a 2D array(see “2D Array Info.pdf” for example).
one that displays the arrays to an output file.
You should NOT use throws FileNotFoundException. Open your files with try/catch blocks as follows:
// Multiline comments, imports, javadoc, class header, etc. goes here.
public static void main(String[] theArgs) { Scanner input = null; PrintStream output = null; String inFileName = \"in2a.txt\"; String outFileName = \"out.txt\"; boolean filesOk = false; // Indicates if the files are accessable.
// other variables such as arrays, should also be declared here.
try { input = new Scanner(new File(inFileName)); output = new PrintStream(new File(outFileName)); filesOk = true; } catch (FileNotFoundException e) { System.out.println(\"Can\'t open file - \" + e); } if (filesOk) {
... // This block is where the rest of the processing should
// occur, such as, input of the arrays, matrix math operations and
// results stored, and output of results.
}
} // End of main.
// Helper methods, such as input and output, go here
// and don’t forget the closing brace for the class
Document your program throughout based on the Documentation Requirements we discussed and is listed on Canvas (Continue to do so on ALL programming assignments).
Be sure to save both Assign_2.java and ArrayMath.java in the same folder. This way when you compile Assign_2.java, all calls or references to the methods of ArrayMath class will also be compiled.
You will note, I have included 2 sample input files for you to use at will (in2a.txt & in2b.txt). Create your own with perhaps much larger arrays if you like.
Because you will have 2 files to submit, you HAVE TO compress both files into a folder and submit this single compressed folder (Programming2.zip) to Canvas. To properly zip your files, see Compress and uncompress files.pdf under the Programming Assignment 2.
Upload Programming2.zip on Canvas by the due date/time.
For full credit, your output should (at very least) be formatted to display the input arrays (with appropriate labelling) and the resulting arrays (also labelled), e.g.:
Array A:
16 -32 1
25 8 12
-5 0 -16
Array B:
4 2 -1
11 44 13
25 -8 0
Array C:
3 4 -2
-1 0 -2
Array D:
3 5
-3 2
1 0
A + B =
20 -30 0
36 52 25
20 -8 -16
A – B =
12 -34 2
14 -36 -1
-30 8 -16
C X D =
-5 23
-5 -5
Solution
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
public class Assign_2 {
public static void main(String[] theArgs) {
Scanner input = null;
PrintStream output = null;
String inFileName = \"D:/ravi/Cheg/in2a.txt\";
String outFileName = \"D:/ravi/Cheg/out.txt\";
boolean filesOk = false; // Indicates if the files are accessable.
// other variables such as arrays, should also be declared here.
try {
input = new Scanner(new File(inFileName));
output = new PrintStream(new File(outFileName));
filesOk = true;
} catch (FileNotFoundException e) {
System.out.println(\"Can\'t open file - \" + e);
}
if (filesOk) {
//read 4 arrays from file
int[][] arrayA = null;
int[][] arrayB = null;
int[][] arrayC = null;
int[][] arrayD = null;
for(int i = 0; i < 4; i++) {
//first line contains 2 integers specifying matrix size [row x column]
int rows = input.nextInt();
int cols = input.nextInt();
int [][] array = new int[rows][cols];
for(int m = 0; m < rows; m++)
for(int n = 0; n < cols; n++) {
array[m][n] = input.nextInt();
}
if(i==0)
arrayA = array;
if( i ==1)
arrayB = array;
if( i ==2)
arrayC = array;
if( i ==3)
arrayD = array;
}
//Addition
int[][] addAB = ArrayMath.addArrays(arrayA, arrayB);
//subtraction
int[][] subAB = ArrayMath.subtractArrays(arrayA, arrayB);
//multiplication
int[][] mulCD = ArrayMath.multiplyArrays(arrayC, arrayD);
try {
//write data to output file
String str = \"Array A:\ \";
output.println(str);
str = Assign_2.getArrayString(arrayA);
output.println(str);
str = \"Array B:\ \";
output.println(str);
str = Assign_2.getArrayString(arrayB);
output.println(str);
str = \"Array C:\ \";
output.println(str);
str = Assign_2.getArrayString(arrayC);
output.println(str);
str = \"Array D:\ \";
output.println(str);
str = Assign_2.getArrayString(arrayD);
output.println(str);
str = \"A + B = \ \";
output.println(str);
str = Assign_2.getArrayString(addAB);
output.println(str);
str = \"A - B = \ \";
output.println(str);
str = Assign_2.getArrayString(subAB);
output.println(str);
str = \" C X D = \ \";
output.println(str);
str = Assign_2.getArrayString(mulCD);
output.println(str);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(\"Failed to write output :\"+e);
}
}
} // End of main.
static String getArrayString(int[][] array) {
StringBuilder sb = new StringBuilder();
int rows = array.length;
int cols = array[0].length;
for(int m = 0; m < rows; m++) {
for(int n = 0; n < cols; n++) {
sb.append(array[m][n]+\" \");
}
sb.append(\"\ \ \");
}
return sb.toString();
}
}
class ArrayMath {
public static int[ ][ ] addArrays(int[][] a, int[][] b) {
if(a != null && b != null) {
if(a.length == b.length && a[0].length == b[0].length) {
int rows = a.length;
int cols = a[0].length;
int[][] array = new int[rows][cols];
for(int m = 0; m < rows; m++) {
for(int n = 0; n < cols; n++) {
array[m][n] = a[m][n] + b[m][n];
}
}
return array;
} else
return null;
}
return null;
}
public static int[ ][ ] subtractArrays(int[][] a, int[][] b) {
if(a != null && b != null) {
if(a.length == b.length && a[0].length == b[0].length) {
int rows = a.length;
int cols = a[0].length;
int[][] array = new int[rows][cols];
for(int m = 0; m < rows; m++) {
for(int n = 0; n < cols; n++) {
array[m][n] = a[m][n] - b[m][n];
}
}
return array;
} else
return null;
}
return null;
}
public static int[ ][ ] multiplyArrays(int[][] a, int[][] b) {
if(a != null && b != null) {
//columns of a must be equal to rows of b
if(a[0].length == b.length) {
int rows = a.length;
int cols = b[0].length;
int[][] array = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
for (int k = 0; k < a[0].length; k++) {
array[i][j] = array[i][j] + a[i][k] * b[k][j];
}
}
}
return array;
} else
return null;
}
return null;
}
}
-----------------output---------------
Array A:
16 -32 1
25 8 12
-5 0 -16
Array B:
4 2 -1
11 44 13
25 -8 0
Array C:
3 4 -2
-1 0 -2
Array D:
3 5
-3 2
1 0
A + B =
20 -30 0
36 52 25
20 -8 -16
A - B =
12 -34 2
14 -36 -1
-30 8 -16
C X D =
-5 23
-5 -5