From 0350952979d4795d53b90bbfd313db6ceb3aa48a Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 29 Oct 2020 14:03:11 +0700 Subject: [PATCH] Add some Manual UnitTest to ArrayUtil Test --- Week9/Assignment9/bin/ArrayUtilityTest.class | Bin 2010 -> 3281 bytes Week9/Assignment9/bin/CSVUtility.class | Bin 1464 -> 1498 bytes Week9/Assignment9/src/ArrayUtility.java | 4 +- Week9/Assignment9/src/ArrayUtilityTest.java | 39 +++++++++++++++++-- Week9/Assignment9/src/CSVUtility.java | 13 ++++--- 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/Week9/Assignment9/bin/ArrayUtilityTest.class b/Week9/Assignment9/bin/ArrayUtilityTest.class index d8b8022a19247c305ce9cf3517294219241f1b32..10aaa1f7b29fd09b248fd6b0ab41ee8a35e7b3fd 100644 GIT binary patch literal 3281 zcmbVOOK)367(J8NzBi6bZ`!7mKGNHk65Dl}#CDullQw-p9<58=28@%!b$Xk)#CBvm zEnxw>vP6glNL_%~@Y+x$AgTaWY>>JEv1AE9f*s<__;Ig;D-`0`Gv7BeXTCY#`R3lo ze?R;Yz!+ZF&?aC^l}gse^-69vSJ}8`mn#}P^xn4aSpBP3VYxq!XK+Bneu02HkN^(ikbt)=fd%wnW_mi5$xO@XhXXKigsA0};$89)q(3y@ zfnzwX;n^n6QXA!poe$tS^pdaQI-$C&N6HoZukbt-p31iJ<2phD0~g*{BgOX3F@I(- zVva=2Sj1#!NOgv*okf3NKGK5YHV#P z8q9L;P1_tbm#jjeSTVD`3)r@fws)FhI6uHE(Y*C zUSK`ts|Bg4nyHr4j4Y>FjqNmFT!8&|8PDmZL-> zkkWFZS;4#pga4jPy|}qMm*)aMo;IBMM0tr_do2#=`c`R%nrd4^|5Qw;g zXUI_*J-;e^wp-$V?f+;m(IhFcR7;M!&vo2XnU0uEDdAnC?#xjojV5K>5;!Ao)W-Z; zUeprTD!Gzh&Xi)?suy7ih^&A=Ra`GE*%x#2+1BlTYR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Ns z=1cN8JI?RODgH_FafjmPJCc_V+Jv2ACwW?#WT*LrJ;uM=()^*~NlOJ&MMl(lR>wIk z5{ZhChe7QW`kTWA!NH_GB9_QIaFOpQ23zJo6w_`aK73kz)Uv|{843*-39ggAfQkVO z(%*?gB<}g#~Ebk ze47Rjf8y$A^Zb>|bT71VUZ|0!Qq;GY=Wczy&~+~TRbQ~vngp^)VilvfhB55YyL(TS eeuUAvYk+!_#a*iPGTpTcH&)1EG*$^qSo;O*#-Tp| diff --git a/Week9/Assignment9/bin/CSVUtility.class b/Week9/Assignment9/bin/CSVUtility.class index 956b3383ac32e5b4322b2f869ca15e8195ab6e04..904cdf1adcbdcaf1039fe38e32678626c8e89818 100644 GIT binary patch delta 651 zcmYLG%Wl(95Itl2+KH1kCBaQYYc{CHX`O~Xa3MfjN&^H)g{WYGKoDBQL*XH{LhQ2X z7t{;H4kV;(VN*p2!io)_z!$&|u36bW5<9@jHx=A7F}{Y=?E_C9w3RIvQXej|>J z9K1A+!xX61=8N@*7K+ga*^V3`7I8tKKWn^ZtzWAZYZj`}H|?Dyqx*U>`3H&zxD##ZUSfP3 zqa$D8M_+Wm6(^;S{HMdF4bew?7z1#S#US!LBlI4IFopukIKnN4QRQu&{uW%^#!;l$ zHFGe7b>qwia18-U5aL|H1Uof;C7i${XDxEZ6!VsWX%=T;>>_U=C)O@TEzF3GBu;W2 zjX;1wm_x7t@fg$N!B_{g_2IX$j5Z8?P7e(?RBhw~M@?lu=6dw>L326U zc$mXH76itV=oPWGF!VwTi}APAdr8O74KMQ(ng_TYe>AFU{4*-?cca#=f8xx5&Dubg zwTNLHMF9><7{NGumA6wE#T?4$;21*o6^te5*+WiDpvK){EWyJhg!_x!@sVQnP$##6 zHhG(zDV!#@0JVow(LzzIJ)E#GB{njc#!UQHx!u|2v3Iak56Wm8fpP=gL&TW`jFh!wx@*2;9? zlgLwSfdWTS?^3mo8S0*==F3!khy5O=64xt8)4~0Jt_QbEbk5vym)Hhf=dp?_WH3m0 z3s)1@4vr`OH91e>8WYm|++=1ZnN(r^!4XEWFd_TMh)D5(YAg#C83e@AkwJ9AM*x3w F{V$%~RM7wc diff --git a/Week9/Assignment9/src/ArrayUtility.java b/Week9/Assignment9/src/ArrayUtility.java index b9ece20..fe43f18 100644 --- a/Week9/Assignment9/src/ArrayUtility.java +++ b/Week9/Assignment9/src/ArrayUtility.java @@ -14,9 +14,9 @@ public class ArrayUtility { int length = 0; //Initialize length to store the column as we will pass this to the array constructor for(int i = 0;i < d.length;i++)length+=d[i].length; //SUM of X(nY) = length int[] mergedArray = new int[length]; // Create a new array with size equal to length - int currentIndex = 0; //Create a new variable to store the 2D Array Index + int currentIndex = 0; //Create a new variable to store the 1D Array Index for(int i = 0;i < d.length;i++)for(int j = 0;j < d[i].length;j++) { //Loop through every Row and In each iteration of row, loop through every column - mergedArray[currentIndex] = d[i][j]; //Set the 2D array element CI to 3D's IJ + mergedArray[currentIndex] = d[i][j]; //Set the 1D array element CI to 2D's IJ currentIndex++; //Move the cursor to the next element } return mergedArray; diff --git a/Week9/Assignment9/src/ArrayUtilityTest.java b/Week9/Assignment9/src/ArrayUtilityTest.java index 1588002..b94f7d0 100644 --- a/Week9/Assignment9/src/ArrayUtilityTest.java +++ b/Week9/Assignment9/src/ArrayUtilityTest.java @@ -1,16 +1,49 @@ public class ArrayUtilityTest { public static void main(String[] args) { - testMerge(); - testSplit(); + // testMerge(); + // testSplit(); + myTestSplit(); + myTestMerge(); } - + public static void myTestSplit() { + int[] myArray = { 1, 8, 3, 1, 5, 3, 7, 5, 3 }; // add your code and some testcases the test split() similar to testSplit() + ArrayUtility.show(ArrayUtility.split(myArray, 1)); + System.out.println("Expected:\n[1, 8, 3, 1, 5, 3, 7, 5, 3]\n----------"); + ArrayUtility.show(ArrayUtility.split(myArray, 2)); + System.out.println("Expected: Array size 9 cannot be splitted into 2 parts\n----------"); + ArrayUtility.show(ArrayUtility.split(myArray, 3)); + System.out.println("Expected:\n[1, 1, 7]\n[8, 5, 5]\n[3, 3, 3]\n----------"); + ArrayUtility.show(ArrayUtility.split(myArray, 4)); + System.out.println("Expected: Array size 9 cannot be splitted into 4 parts\n----------"); + ArrayUtility.show(ArrayUtility.split(myArray, 5)); + System.out.println("Expected: Array size 9 cannot be splitted into 5 parts\n----------"); + ArrayUtility.show(ArrayUtility.split(myArray, 6)); + System.out.println("Expected: Array size 9 cannot be splitted into 6 parts\n----------"); + ArrayUtility.show(ArrayUtility.split(myArray, 7)); + System.out.println("Expected: Array size 9 cannot be splitted into 7 parts\n----------"); + ArrayUtility.show(ArrayUtility.split(myArray, 8)); + System.out.println("Expected: Array size 9 cannot be splitted into 8 parts\n----------"); + ArrayUtility.show(ArrayUtility.split(myArray, 10)); + System.out.println("Expected: n cannot be greater than array's size, 9\n----------"); + ArrayUtility.show(ArrayUtility.split(myArray, 0)); + System.out.println("Expected: n must be >= 0\n----------"); + ArrayUtility.show(ArrayUtility.split(myArray, -1)); + System.out.println("Expected: n must be >= 0\n----------"); + + } public static void myTestMerge() { // add your code and some testcases the test merge() similar to testMerge() + int[][] myArray = { { 6, 13, 56 }, { 92, 55 }, { 12, 32, 13, 92 }, { 10 } }; + ArrayUtility.show(ArrayUtility.merge(myArray)); + System.out.println("[6, 13, 56, 92, 55, 12, 32, 13, 92, 10]] - Expected"); + int[][] myArray2 = {{145},{134,423,42},{33,44,13}}; + ArrayUtility.show(ArrayUtility.merge(myArray2)); + System.out.println("[145, 134, 423, 42, 33, 44, 13] - Expected"); } // do no modify this method diff --git a/Week9/Assignment9/src/CSVUtility.java b/Week9/Assignment9/src/CSVUtility.java index 41931f9..a5fef88 100644 --- a/Week9/Assignment9/src/CSVUtility.java +++ b/Week9/Assignment9/src/CSVUtility.java @@ -1,3 +1,4 @@ + //This Code is written bt Siwat Sirichai import java.io.File; import java.io.FileNotFoundException; @@ -16,15 +17,17 @@ public class CSVUtility { */ 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++) + Scanner sc = new Scanner(new File(filename)); // Create a Scanner to take in the file at path filename + int length = 0; //Create a length variable and initialize it to zero + for (length = 0; sc.hasNext(); length++) sc.nextLine(); + sc.close(); //Close the Scanner to prevent resource leak String[] filedata = new String[length]; - sc = new Scanner(new File(filename)); + sc = new Scanner(new File(filename)); //Reinitialize the closed scanner for (int i = 0; i < length; i++) filedata[i] = sc.nextLine(); - return filedata; + sc.close(); //Close the Scanner to prevent resource leak + return filedata; //Return } /**