Re: addition of 2 matrix number
SamuelXiao wrote:
Acutally, I still struggle this program, by far, what i have is like
the following code:
I've made some changes.
Prints:
true
true
Hello
6.0 8.0
10.0 12.0
import java.io.File;
import java.util.Scanner;
public class lab1 {
public static void main(String[] args) throws Exception {
Matric m1, m2, m3;
// Menu
System.out.println("(1) Add two matrices");
System.out.println("(2) Multiply two matrices");
System.out.println("(3) Take transpose of a matrix");
System.out.println("(4) Display a maxtric");
System.out.println("(5) Exit");
System.out.println("\n");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
switch (choice) {
case 1: {
String filename1, filename2;
Scanner stdin = new Scanner(System.in);
filename1 = stdin.next();
filename2 = stdin.next();
stdin.nextLine();
//System.out.println(filename1 + " " + filename2);
File file1 = new File(filename1);
File file2 = new File(filename2);
System.out.println(file1.exists());
System.out.println(file2.exists());
m1 = Matric.readfile(file1);
m2 = Matric.readfile(file2);
if ((m1.getCols() == m2.getCols()) &&
(m1.getRows() == m2.getRows())) {
System.out.println("Hello");
m3 = Matric.add2matrices(m1, m2);
m3.display();
} else
System.out.println(
"This two files cannot operation for addition");
}
break;
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
class Matric {
final double[][] element;
final int rows;
final int cols;
public Matric(int rows, int cols, double[][] data) {
this.rows = rows;
this.cols = cols;
element = data;
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public double[][] getData() {
return element;
}
public static Matric add2matrices(Matric m1, Matric m2) {
double[][] data = null;
if (m1.getRows() == m2.getRows() && m1.getCols() == m2.getCols()) {
data = new double[m1.getRows()][m1.getCols()];
for (int i = 0; i < m1.getRows(); i++) {
for (int j = 0; j < m1.getCols(); j++) {
data[i][j] = m1.element[i][j] + m2.element[i][j];
}
}
return new Matric(m1.getRows(), m1.getCols(), data);
}
return null;
}
public void display() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(element[i][j] + " ");
}
System.out.println();
}
}
public static Matric readfile(File file) {
String fileString = null;
try {
FileReader fileReader = new FileReader(file);
CharBuffer charBuffer = CharBuffer.allocate((int) file.length());
try {
fileReader.read(charBuffer);
fileReader.close();
} catch (IOException e) {
// TODO
e.printStackTrace();
}
charBuffer.flip();
fileString = charBuffer.toString();
} catch (FileNotFoundException e) {
// TODO
e.printStackTrace();
}
List<String> fileLines = new ArrayList<String>();
StringTokenizer lineTokenizer =
new StringTokenizer(fileString, "\n");
while (lineTokenizer.hasMoreTokens()) {
fileLines.add(lineTokenizer.nextToken());
}
int rows = Integer.parseInt(fileLines.get(1).substring(
fileLines.get(1).indexOf("=") + 2));
int cols = Integer.parseInt(fileLines.get(2).substring(
fileLines.get(2).indexOf("=") + 2));
double[][] data = new double[rows][cols];
for (int idx = 3; idx < rows + 3; idx++) {
String[] fieldStrings = fileLines.get(idx).split(" ");
for (int iidx = 0; iidx < cols; iidx++) {
data[idx - 3][iidx] = Double.parseDouble(fieldStrings[iidx]);
}
}
if (data != null) {
return new Matric(rows, cols, data);
}
return null;
}
}