diff --git a/Week9/Assignment9/bin/ArrayUtility.class b/Week9/Assignment9/bin/ArrayUtility.class new file mode 100644 index 0000000..67fde9f Binary files /dev/null and b/Week9/Assignment9/bin/ArrayUtility.class differ diff --git a/Week9/Assignment9/bin/ArrayUtilityTest.class b/Week9/Assignment9/bin/ArrayUtilityTest.class new file mode 100644 index 0000000..d8b8022 Binary files /dev/null and b/Week9/Assignment9/bin/ArrayUtilityTest.class differ diff --git a/Week9/Assignment9/bin/CSVUtility.class b/Week9/Assignment9/bin/CSVUtility.class new file mode 100644 index 0000000..102ee10 Binary files /dev/null and b/Week9/Assignment9/bin/CSVUtility.class differ diff --git a/Week9/Assignment9/bin/CSVUtilityGiven.class b/Week9/Assignment9/bin/CSVUtilityGiven.class new file mode 100644 index 0000000..49e9f9a Binary files /dev/null and b/Week9/Assignment9/bin/CSVUtilityGiven.class differ diff --git a/Week9/Assignment9/bin/CSVUtilityTest.class b/Week9/Assignment9/bin/CSVUtilityTest.class new file mode 100644 index 0000000..608b8af Binary files /dev/null and b/Week9/Assignment9/bin/CSVUtilityTest.class differ diff --git a/Week9/Assignment9/bin/module-info.class b/Week9/Assignment9/bin/module-info.class deleted file mode 100644 index d6ac9a1..0000000 Binary files a/Week9/Assignment9/bin/module-info.class and /dev/null differ diff --git a/Week9/Assignment9/data.txt b/Week9/Assignment9/data.txt new file mode 100644 index 0000000..5ca2b25 --- /dev/null +++ b/Week9/Assignment9/data.txt @@ -0,0 +1,7 @@ +1st line +2nd line +3rd line +4th line +5th line +6th line +7th line \ No newline at end of file diff --git a/Week9/Assignment9/golf-score-1.csv b/Week9/Assignment9/golf-score-1.csv new file mode 100644 index 0000000..3d6a808 --- /dev/null +++ b/Week9/Assignment9/golf-score-1.csv @@ -0,0 +1,11 @@ +course-02 +Player01,4,4,6,3,4,5,4,6,6,6,3,5,4,6,4,4,3,6 +Player02,4,5,6,4,5,4,5,4,8,9,4,5,5,6,5,4,4,5 +Player03,4,5,5,4,6,5,5,5,7,6,3,6,4,6,5,5,4,5 +Player04,5,6,4,3,5,4,4,4,6,5,2,7,4,7,5,7,4,6 +Player05,4,3,4,4,4,5,4,4,5,5,4,5,5,5,5,4,5,4 +Player06,3,4,4,3,3,4,4,4,5,5,4,5,3,5,3,4,3,6 +Player07,6,4,6,4,5,5,5,3,6,4,4,5,4,5,5,5,3,4 +Player08,4,6,5,3,4,6,4,3,4,4,2,5,4,5,4,4,3,4 +Player09,5,5,8,4,5,4,4,3,5,5,4,5,3,6,6,5,4,5 +Player10,5,4,5,3,4,4,4,2,5,5,3,6,4,5,4,6,5,5 diff --git a/Week9/Assignment9/golf-score-2.csv b/Week9/Assignment9/golf-score-2.csv new file mode 100644 index 0000000..7837094 --- /dev/null +++ b/Week9/Assignment9/golf-score-2.csv @@ -0,0 +1,25 @@ +course-04 +Player01,4,4,6,3,4,5,4,6,6,6,3,5,4,6,4,4,3,6 +Player02,4,5,6,4,5,4,5,4,8,9,4,5,5,6,5,4,4,5 +Player03,4,5,5,4,6,5,5,5,7,6,3,6,4,6,5,5,4,5 +Player04,5,6,4,3,5,4,4,4,6,5,2,7,4,7,5,7,4,6 +Player05,4,3,4,4,4,5,4,4,5,5,4,5,5,5,5,4,5,4 +Player06,3,4,4,3,3,4,4,4,5,5,4,5,3,5,3,4,3,6 +Player07,6,4,6,4,5,5,5,3,6,4,4,5,4,5,5,5,3,4 +Player08,4,6,5,3,4,6,4,3,4,4,2,5,4,5,4,4,3,4 +Player09,5,5,8,4,5,4,4,3,5,5,4,5,3,6,6,5,4,5 +Player10,5,4,5,3,4,4,4,2,5,5,3,6,4,5,4,6,5,5 +Player11,4,6,5,3,5,4,4,3,5,4,2,4,4,5,5,6,3,4 +Player12,4,5,5,3,4,5,4,5,4,4,3,6,4,6,4,4,4,4 +Player13,6,6,6,3,5,4,6,4,5,5,3,5,3,6,5,4,4,6 +Player14,4,6,5,3,5,4,4,4,5,4,4,6,4,6,7,4,4,5 +Player15,5,4,5,4,6,6,4,3,5,5,4,5,4,5,5,4,4,5 +Player16,5,4,5,3,5,4,5,4,7,8,3,5,4,5,5,4,4,4 +Player17,6,4,5,3,5,6,4,3,5,4,3,5,5,9,5,3,3,4 +Player18,5,6,5,4,5,6,4,4,5,5,2,5,4,6,7,5,3,7 +Player19,4,4,6,4,4,6,4,3,6,7,3,5,4,5,4,4,4,5 +Player20,5,4,6,3,4,4,4,3,5,5,3,5,5,6,5,6,4,5 +Player21,4,4,7,2,4,5,4,3,5,6,3,7,4,5,4,4,5,4 +Player22,6,4,7,4,5,6,5,4,5,5,4,7,4,4,5,5,4,5 +Player23,5,4,7,3,5,5,6,4,5,6,3,5,4,6,4,4,3,6 +Player24,6,4,6,4,4,4,4,3,5,4,5,5,3,6,5,3,5,5 \ No newline at end of file diff --git a/Week9/Assignment9/src/ArrayUtility.java b/Week9/Assignment9/src/ArrayUtility.java new file mode 100644 index 0000000..5a4f7e6 --- /dev/null +++ b/Week9/Assignment9/src/ArrayUtility.java @@ -0,0 +1,59 @@ +import java.util.Arrays; + +public class ArrayUtility { + + /** + * combines a given 2D array into 1D array + * for example, + * { {1, 2}, {3, 4, 5} } -> {1, 2, 3, 4, 5} + * @param d + * @return 1D array of combine all elements from 2D array + */ + public static int[] merge(int[][] d) { + int length = 0; + for(int i = 0;i < d.length;i++)length+=d[i].length; + int[] merged_array = new int[length]; + int current_index = 0; + for(int i = 0;i < d.length;i++)for(int j = 0;j < d[i].length;j++) { + merged_array[current_index] = d[i][j]; + current_index++; + } + return merged_array; + } + + /** + * splits a given 1D array into 2D array with n equal parts + * if the given array cannot be split into n equal part + * this method should return 2D array of size 0,0 (new int[0][0]) + * @param d + * @param n number of parts for the target, 0 < n <= d.length + * @return 2D array + */ + public static int[][] split(int[] d, int n) { + if(n<=0) return new int[0][0]; //Return to prevent ArithmeticException: / by zero and negative array index + if (d.length%n!=0)return new int[0][0]; //Return, not array is not splittable + int[][] splitted_array = new int[n][d.length/n]; //Create the splitted_array array and initialize it + for(int i = 0;i < n;i++)for(int j = 0;j= 0\n----------"); + ArrayUtility.show(ArrayUtility.split(c, -1)); + System.out.println("Expected: n must be >= 0\n----------"); + ArrayUtility.show(ArrayUtility.split(c, 7)); + System.out.println("Expected: n cannot be greater than array's size, 6\n----------"); + } + + // do not modify this method + private static void testMerge() { + int[][] a = { { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8, 9 }, { 10 } }; + ArrayUtility.show(ArrayUtility.merge(a)); + System.out.println("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - Expected"); + int[][] b = { { 1, 2, 3, 4 }, { 4, 5, 6, 7 }, { 7, 8, 9, 10 }, { 10, 11, 12, 13 } }; + ArrayUtility.show(ArrayUtility.merge(b)); + System.out.println("[1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 13] - Expected"); + } + +} diff --git a/Week9/Assignment9/src/CSVUtility.java b/Week9/Assignment9/src/CSVUtility.java new file mode 100644 index 0000000..ca2eac0 --- /dev/null +++ b/Week9/Assignment9/src/CSVUtility.java @@ -0,0 +1,42 @@ + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + +public class CSVUtility { + + /** + * reads all lines in given filename into an array of String + * @param filename + * @return array of String, each element in array is each line in file + * @throws FileNotFoundException + */ + public static String[] readFile(String filename) throws FileNotFoundException { + // ENTER CODE HERE + Scanner sc = new Scanner(new File(filename)); + int length = 0; + for(length = 0;sc.hasNext();length++)sc.nextLine(); + String[] filedata = new String[length]; + sc = new Scanner(new File(filename)); + sc = new Scanner(new File(filename)).useDelimiter(","); + for(int i = 0;i<=length;i++)filedata[i]=sc.next(); + return filedata; + } + + /** + * read given CSV file into 2D array of String + * @param filename CSV file + * @param header true if CSV file has a header line. The first line is header. The data start from second line. + * @return + * @throws FileNotFoundException + */ + public static String[][] readCSV(String filename, boolean header) throws FileNotFoundException { + // ENTER CODE HERE + return null; + } + + + +} diff --git a/Week9/Assignment9/src/CSVUtilityGiven.java b/Week9/Assignment9/src/CSVUtilityGiven.java new file mode 100644 index 0000000..bea40be --- /dev/null +++ b/Week9/Assignment9/src/CSVUtilityGiven.java @@ -0,0 +1,51 @@ +// +// Source code recreated from a .class file by SNLinter +// (powered by FernFlower decompiler) +// + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Scanner; + +public class CSVUtilityGiven { + public CSVUtilityGiven() { + } + + public static String[] readFile(String var0) throws FileNotFoundException { + Scanner var1 = new Scanner(new File(var0)); + ArrayList var2 = new ArrayList(); + + while(var1.hasNext()) { + var2.add(var1.nextLine()); + } + + var1.close(); + String[] var3 = new String[var2.size()]; + + for(int var4 = 0; var4 < var3.length; ++var4) { + var3[var4] = (String)var2.get(var4); + } + + return var3; + } + + public static String[][] readCSV(String var0, boolean var1) throws FileNotFoundException { + String[] var2 = readFile(var0); + int var3 = var1 ? 1 : 0; + String[][] var4 = new String[var2.length - var3][]; + int var5 = 0; + + for(int var6 = var3; var5 < var4.length; ++var6) { + var4[var5] = var2[var6].split(","); + ++var5; + } + + return var4; + } + + public static void main(String[] var0) throws FileNotFoundException { + ArrayUtility.show(readCSV("course-db.csv", false)); + ArrayUtility.show(readCSV("golf-score.csv", false)); + } +} diff --git a/Week9/Assignment9/src/CSVUtilityTest.java b/Week9/Assignment9/src/CSVUtilityTest.java new file mode 100644 index 0000000..d092883 --- /dev/null +++ b/Week9/Assignment9/src/CSVUtilityTest.java @@ -0,0 +1,31 @@ +import java.io.FileNotFoundException; + +public class CSVUtilityTest { + public static void main(String[] args) throws FileNotFoundException { + System.out.println("Expected:"); + ArrayUtility.show(CSVUtilityGiven.readFile("data.txt")); + System.out.println("Your result:"); + ArrayUtility.show(CSVUtility.readFile("data.txt")); + System.out.println("----------------------------------------"); + System.out.println("Expected:"); + ArrayUtility.show(CSVUtilityGiven.readCSV("data.txt", false)); + System.out.println("Your result:"); + ArrayUtility.show(CSVUtility.readCSV("data.txt", false)); + System.out.println("----------------------------------------"); + System.out.println("Expected:"); + ArrayUtility.show(CSVUtilityGiven.readCSV("data.txt", true)); + System.out.println("Your result:"); + ArrayUtility.show(CSVUtility.readCSV("data.txt", true)); + System.out.println("----------------------------------------"); + System.out.println("Expected:"); + ArrayUtility.show(CSVUtilityGiven.readCSV("course-db.csv", false)); + System.out.println("Your result:"); + ArrayUtility.show(CSVUtility.readCSV("course-db.csv", false)); + System.out.println("----------------------------------------"); + System.out.println("Expected:"); + ArrayUtility.show(CSVUtilityGiven.readCSV("course-db.csv", true)); + System.out.println("Your result:"); + ArrayUtility.show(CSVUtility.readCSV("course-db.csv", true)); + } + +} diff --git a/Week9/Assignment9/src/Java101Viewer.class b/Week9/Assignment9/src/Java101Viewer.class new file mode 100644 index 0000000..d180da7 Binary files /dev/null and b/Week9/Assignment9/src/Java101Viewer.class differ diff --git a/Week9/Assignment9/src/module-info.java b/Week9/Assignment9/src/module-info.java deleted file mode 100644 index 505265e..0000000 --- a/Week9/Assignment9/src/module-info.java +++ /dev/null @@ -1,2 +0,0 @@ -module Assignment9 { -} \ No newline at end of file