From 9baabad4d498d26830f24b70435a924715cbe747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Fri, 17 Mar 2023 15:31:57 +0100 Subject: [PATCH 1/2] Completed reading in the CSV (#1) * Created Matrix class Created Matrix class with an empty main method * Created Matrix class Created Matrix class with an empty main method * Created importCSV method Created the empty method importCSV * Completed importCSV method Added BufferedReader with FileReader surrounded by a try/catch * Added file parameter Added it so I don't have a hardcoded file location * Removed line variable It just wasn't needed * Created TestMatrix class For now only has one test for the importCSV method * Created empty printCSV method gonna use it to print out the array which contains the array * ReAdded line variable It was needed after all * Made result a global variable So it can be used by other methods * Array now initialized with correct size I still don't know how to put the csv into the array tough * done don't ask how --- src/Matrix.java | 49 +++++++++++++++++++++++++++++++++++++++++++++ src/TestMatrix.java | 11 ++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/Matrix.java create mode 100644 src/TestMatrix.java diff --git a/src/Matrix.java b/src/Matrix.java new file mode 100644 index 0000000..ed3a668 --- /dev/null +++ b/src/Matrix.java @@ -0,0 +1,49 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.FileReader; +import java.io.FileNotFoundException; + +public class Matrix { + private int[][] matrix; + + public static void main(String[] args) {} + + public Matrix(String file) { + matrix = this.readCSV(file); + } + + public int[][] readCSV(String file){ + int[][] intMatrix = null; + try (BufferedReader br = new BufferedReader(new FileReader(file))) { + String line = br.readLine(); + int rowCount = line.trim().split(";").length; + int columnCount = rowCount; + String[] lineArray = null; + + intMatrix = new int[rowCount][columnCount]; + + for(int columnIndex = 0; line != null && columnIndex < intMatrix.length; columnIndex++, line = br.readLine()) { + lineArray = line.trim().split(";"); + for(int rowIndex=0; rowIndex < intMatrix[columnIndex].length; rowIndex++) { + intMatrix[columnIndex][rowIndex] = Integer.parseInt(lineArray[rowIndex]); + } + } + br.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return intMatrix; + } + + public void print() { + for(int columnIndex=0; columnIndex < matrix.length; columnIndex++) { + for(int rowIndex=0; rowIndex < matrix[columnIndex].length; rowIndex++) { + System.out.print(matrix[columnIndex][rowIndex]); + } + System.out.println(); + } + } + +} diff --git a/src/TestMatrix.java b/src/TestMatrix.java new file mode 100644 index 0000000..7db94b0 --- /dev/null +++ b/src/TestMatrix.java @@ -0,0 +1,11 @@ +public class TestMatrix { + public static void main(String[] args) { + test1(""); + } + + public static void test1(String file) { + Matrix matrix = new Matrix(file); + + matrix.print(); + } +} -- 2.45.2 From 99932e227be4241f41f7903fc3045dd546cfc046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fuhry?= Date: Fri, 17 Mar 2023 15:35:53 +0100 Subject: [PATCH 2/2] Done, don't ask how --- src/Matrix.java | 64 +++++++++++++++++++++++++++++++++++++-------- src/TestMatrix.java | 6 +++++ 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/Matrix.java b/src/Matrix.java index ed3a668..3ad92a3 100644 --- a/src/Matrix.java +++ b/src/Matrix.java @@ -8,10 +8,61 @@ public class Matrix { public static void main(String[] args) {} + public Matrix(int rowLength, int columnLength) { + matrix = new int[rowLength][columnLength]; + } + public Matrix(String file) { matrix = this.readCSV(file); } + public Matrix multiply( Matrix m) { + Matrix scalarProduct = null; + + if(this.columnLength() != m.rowLength()) { + return scalarProduct; + } + + scalarProduct = new Matrix(this.rowLength(), m.columnLength()); + + for(int columnIndex = 0; columnIndex < this.columnLength(); columnIndex++) { + for(int rowIndex = 0; rowIndex < m.rowLength(); rowIndex++) { + int sum = 0; + for(int k=0; k < this.rowLength(); k++) { + sum += this.getValue(columnIndex, k) * m.getValue(k, rowIndex); + } + scalarProduct.insert(columnIndex, rowIndex, sum); + } + } + + return scalarProduct; + } + + public int rowLength() { + return matrix.length; + } + + public int columnLength() { + return matrix[0].length; + } + + public int getValue(int columnIndex, int rowIndex) { + return matrix[columnIndex][rowIndex]; + } + + public void insert(int columnIndex, int rowIndex, int value) { + matrix[rowIndex][columnIndex] = value; + } + + public void print() { + for(int columnIndex=0; columnIndex < matrix.length; columnIndex++) { + for(int rowIndex=0; rowIndex < matrix[columnIndex].length; rowIndex++) { + System.out.print(matrix[columnIndex][rowIndex]); + } + System.out.println(); + } + } + public int[][] readCSV(String file){ int[][] intMatrix = null; try (BufferedReader br = new BufferedReader(new FileReader(file))) { @@ -19,7 +70,7 @@ public class Matrix { int rowCount = line.trim().split(";").length; int columnCount = rowCount; String[] lineArray = null; - + intMatrix = new int[rowCount][columnCount]; for(int columnIndex = 0; line != null && columnIndex < intMatrix.length; columnIndex++, line = br.readLine()) { @@ -37,13 +88,4 @@ public class Matrix { return intMatrix; } - public void print() { - for(int columnIndex=0; columnIndex < matrix.length; columnIndex++) { - for(int rowIndex=0; rowIndex < matrix[columnIndex].length; rowIndex++) { - System.out.print(matrix[columnIndex][rowIndex]); - } - System.out.println(); - } - } - -} +} \ No newline at end of file diff --git a/src/TestMatrix.java b/src/TestMatrix.java index 7db94b0..b7679b4 100644 --- a/src/TestMatrix.java +++ b/src/TestMatrix.java @@ -5,7 +5,13 @@ public class TestMatrix { public static void test1(String file) { Matrix matrix = new Matrix(file); + Matrix scalarProduct; matrix.print(); + System.out.println(); + + scalarProduct = matrix.multiply( matrix); + scalarProduct.print(); + } } -- 2.45.2