diff --git a/Week8/EclipseIDE/Assignment8/.classpath b/Week8/EclipseIDE/Assignment8/.classpath new file mode 100644 index 0000000..1cd9d95 --- /dev/null +++ b/Week8/EclipseIDE/Assignment8/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/Week8/EclipseIDE/Assignment8/.project b/Week8/EclipseIDE/Assignment8/.project new file mode 100644 index 0000000..5c86e38 --- /dev/null +++ b/Week8/EclipseIDE/Assignment8/.project @@ -0,0 +1,17 @@ + + + Assignment8 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Week8/EclipseIDE/Assignment8/.settings/org.eclipse.jdt.core.prefs b/Week8/EclipseIDE/Assignment8/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..910a770 --- /dev/null +++ b/Week8/EclipseIDE/Assignment8/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=14 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=14 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=14 diff --git a/Week8/EclipseIDE/Assignment8/bin/module-info.class b/Week8/EclipseIDE/Assignment8/bin/module-info.class new file mode 100644 index 0000000..270cba6 Binary files /dev/null and b/Week8/EclipseIDE/Assignment8/bin/module-info.class differ diff --git a/Week8/EclipseIDE/Assignment8/bin/partA/IsReverse.class b/Week8/EclipseIDE/Assignment8/bin/partA/IsReverse.class new file mode 100644 index 0000000..545a9b6 Binary files /dev/null and b/Week8/EclipseIDE/Assignment8/bin/partA/IsReverse.class differ diff --git a/Week8/EclipseIDE/Assignment8/bin/partA/package-info.class b/Week8/EclipseIDE/Assignment8/bin/partA/package-info.class new file mode 100644 index 0000000..951f65a Binary files /dev/null and b/Week8/EclipseIDE/Assignment8/bin/partA/package-info.class differ diff --git a/Week8/EclipseIDE/Assignment8/bin/partB/Replace.class b/Week8/EclipseIDE/Assignment8/bin/partB/Replace.class new file mode 100644 index 0000000..0cb5f57 Binary files /dev/null and b/Week8/EclipseIDE/Assignment8/bin/partB/Replace.class differ diff --git a/Week8/EclipseIDE/Assignment8/bin/partB/package-info.class b/Week8/EclipseIDE/Assignment8/bin/partB/package-info.class new file mode 100644 index 0000000..51513e8 Binary files /dev/null and b/Week8/EclipseIDE/Assignment8/bin/partB/package-info.class differ diff --git a/Week8/EclipseIDE/Assignment8/bin/partC/CountNumbers.class b/Week8/EclipseIDE/Assignment8/bin/partC/CountNumbers.class new file mode 100644 index 0000000..df114d0 Binary files /dev/null and b/Week8/EclipseIDE/Assignment8/bin/partC/CountNumbers.class differ diff --git a/Week8/EclipseIDE/Assignment8/bin/partC/package-info.class b/Week8/EclipseIDE/Assignment8/bin/partC/package-info.class new file mode 100644 index 0000000..41af608 Binary files /dev/null and b/Week8/EclipseIDE/Assignment8/bin/partC/package-info.class differ diff --git a/Week8/EclipseIDE/Assignment8/bin/partD/InsertArray.class b/Week8/EclipseIDE/Assignment8/bin/partD/InsertArray.class new file mode 100644 index 0000000..7989394 Binary files /dev/null and b/Week8/EclipseIDE/Assignment8/bin/partD/InsertArray.class differ diff --git a/Week8/EclipseIDE/Assignment8/bin/partD/package-info.class b/Week8/EclipseIDE/Assignment8/bin/partD/package-info.class new file mode 100644 index 0000000..4271d2d Binary files /dev/null and b/Week8/EclipseIDE/Assignment8/bin/partD/package-info.class differ diff --git a/Week8/EclipseIDE/Assignment8/numbers.txt b/Week8/EclipseIDE/Assignment8/numbers.txt new file mode 100644 index 0000000..8250c09 --- /dev/null +++ b/Week8/EclipseIDE/Assignment8/numbers.txt @@ -0,0 +1,100 @@ +84 +44 +73 +52 +49 +23 +53 +66 +52 +94 +93 +32 +3 +94 +3 +77 +7 +64 +86 +13 +10 +24 +42 +73 +63 +27 +59 +1 +13 +10 +79 +62 +16 +92 +90 +83 +83 +34 +18 +47 +72 +29 +1 +90 +71 +0 +99 +30 +66 +9 +10 +12 +58 +19 +15 +92 +74 +14 +10 +62 +36 +71 +23 +7 +26 +39 +21 +8 +15 +79 +94 +73 +8 +37 +44 +47 +39 +6 +93 +72 +21 +0 +65 +84 +64 +43 +13 +74 +91 +81 +54 +79 +66 +66 +61 +48 +26 +6 +34 +95 \ No newline at end of file diff --git a/Week8/EclipseIDE/Assignment8/src/module-info.java b/Week8/EclipseIDE/Assignment8/src/module-info.java new file mode 100644 index 0000000..d38a490 --- /dev/null +++ b/Week8/EclipseIDE/Assignment8/src/module-info.java @@ -0,0 +1,2 @@ +module Assignment8 { +} \ No newline at end of file diff --git a/Week8/EclipseIDE/Assignment8/src/partA/IsReverse.java b/Week8/EclipseIDE/Assignment8/src/partA/IsReverse.java new file mode 100644 index 0000000..7874e28 --- /dev/null +++ b/Week8/EclipseIDE/Assignment8/src/partA/IsReverse.java @@ -0,0 +1,37 @@ +//This code is written by Siwat Sirichai +package partA; + +import java.util.Arrays; + +public class IsReverse { + + public static boolean isReverse(int[] a, int[] b) { + if (a==null&&b==null)return true; //Special Case #1: if a and b is null, result: true, return right away to prevent NullPointer exception. + if((a==null&&b!=null)||(a!=null&&b==null))return false; //Special Case #2: if only one of a or b is null, result: false, return right away to prevent NullPointer exception. + if (a.length != b.length)return false; //Special Case #3: if the length of a and b is not equal, result: false, return right away to prevent ArrayIndexoutofBound exception. + for(int i = 0;iv)count++;//This is called a for-each loop, it will loop for a.length iteration, + return count; + } + +} diff --git a/Week8/EclipseIDE/Assignment8/src/partC/package-info.java b/Week8/EclipseIDE/Assignment8/src/partC/package-info.java new file mode 100644 index 0000000..2cf5023 --- /dev/null +++ b/Week8/EclipseIDE/Assignment8/src/partC/package-info.java @@ -0,0 +1 @@ +package partC; \ No newline at end of file diff --git a/Week8/EclipseIDE/Assignment8/src/partD/InsertArray.java b/Week8/EclipseIDE/Assignment8/src/partD/InsertArray.java new file mode 100644 index 0000000..da69f25 --- /dev/null +++ b/Week8/EclipseIDE/Assignment8/src/partD/InsertArray.java @@ -0,0 +1,60 @@ +//This code is written by Siwat Sirichai +package partD; +import partB.Replace; +//Utilizing the method arraycopy(Array a,int a_start_pos, Array b,int b_start_pos, length) +//This is kinda complicated, diagram attached. +public class InsertArray { + public static void arraycopy(int[] a, int apos,int[] b, int bpos, int length) { + for(int i=0;i=a.length-1) { //If pos => a.length just put a before b + arraycopy(a, 0, result, 0, a.length); + arraycopy(b, 0, result, a.length, b.length); + } + else { + arraycopy(b, 0, result, pos, b.length); //put in b first + arraycopy(a, 0, result, 0, pos); //put the first part of a in (part before b) + arraycopy(a, pos, result, pos+b.length ,a.length-pos); //put the second part of a in (part after b) + } + return result; + } + + + public static void main(String[] args) { + // TODO Auto-generated method stub + int[] a = { 1, 2, 3, 4}; + int[] b = { 5, 6, 7}; + int[] c = { 8, 9, 10, 11, 12}; + + System.out.print("Printing array a: "); Replace.printArray(a); + System.out.print("Printing array b: "); Replace.printArray(b); + System.out.print("Printing array c: "); Replace.printArray(c); + + System.out.println("Now calling inserts!"); + System.out.println("------------------------"); + int[] d = insert(a,b,1); + int[] e = insert(a,b,4); //ERROR + int[] f = insert(b,a,0); + int[] g = insert(c,a,3); + int[] h = insert(c,a,-1); + int[] i = insert(c,a,7); //ERROR + + System.out.print("Printing array a, expecting {1,2,3,4} : "); Replace.printArray(a); + System.out.print("Printing array b, expecting {5,6,7} : "); Replace.printArray(b); + System.out.print("Printing array c, expecting {8,9,10,11,12} : "); Replace.printArray(c); + System.out.print("Printing array d, expecting {1,5,6,7,2,3,4} : "); Replace.printArray(d); + System.out.print("Printing array e, expecting {1,2,3,4,5,6,7} : "); Replace.printArray(e); + System.out.print("Printing array f, expecting {1,2,3,4,5,6,7}: "); Replace.printArray(f); + System.out.print("Printing array g, expecting {8,9,10,1,2,3,4,11,12} : "); Replace.printArray(g); + System.out.print("Printing array h, expecting {1,2,3,4,8,9,10,11,12} : "); Replace.printArray(h); + System.out.print("Printing array i, expecting {8,9,10,11,12,1,2,3,4} : "); Replace.printArray(i); + + } + +} diff --git a/Week8/EclipseIDE/Assignment8/src/partD/package-info.java b/Week8/EclipseIDE/Assignment8/src/partD/package-info.java new file mode 100644 index 0000000..420b6aa --- /dev/null +++ b/Week8/EclipseIDE/Assignment8/src/partD/package-info.java @@ -0,0 +1 @@ +package partD; \ No newline at end of file diff --git a/Week8/SublimeText/CountNumbers.class b/Week8/SublimeText/CountNumbers.class new file mode 100644 index 0000000..ae66ed5 Binary files /dev/null and b/Week8/SublimeText/CountNumbers.class differ diff --git a/Week8/SublimeText/CountNumbers.java b/Week8/SublimeText/CountNumbers.java new file mode 100644 index 0000000..bed8594 --- /dev/null +++ b/Week8/SublimeText/CountNumbers.java @@ -0,0 +1,30 @@ +//This code is written by Siwat Sirichai +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; +public class CountNumbers { + + public static void main(String[] args) { + try { //Since java.io.File throws FileNotFoundException, a try-catch block is neccessary. + Scanner sc = new Scanner(new File("numbers.txt")); //This create a scanner with file "number.txt" as an input + int length = 0; //define array length initialy at zero + for(length = 0;sc.hasNextInt();length++)sc.nextInt(); //Use File Scanner to find the number of elements in the file + int[] int_array = new int[length]; //Create an array with length number of elements + sc = new Scanner(new File("numbers.txt")); //Send the Scanner's cursor to the top of the file + for(int i = 0;iv)count++;//This is called a for-each loop, it will loop for a.length iteration, + return count; + } + +} diff --git a/Week8/SublimeText/InsertArray.class b/Week8/SublimeText/InsertArray.class new file mode 100644 index 0000000..481717f Binary files /dev/null and b/Week8/SublimeText/InsertArray.class differ diff --git a/Week8/SublimeText/InsertArray.java b/Week8/SublimeText/InsertArray.java new file mode 100644 index 0000000..9c8b3fc --- /dev/null +++ b/Week8/SublimeText/InsertArray.java @@ -0,0 +1,58 @@ +//This code is written by Siwat Sirichai +//Utilizing the method arraycopy(Array a,int a_start_pos, Array b,int b_start_pos, length) +//This is kinda complicated, diagram attached. +public class InsertArray { + public static void arraycopy(int[] a, int apos,int[] b, int bpos, int length) { + for(int i=0;i=a.length-1) { //If pos => a.length just put a before b + arraycopy(a, 0, result, 0, a.length); + arraycopy(b, 0, result, a.length, b.length); + } + else { + arraycopy(b, 0, result, pos, b.length); //put in b first + arraycopy(a, 0, result, 0, pos); //put the first part of a in (part before b) + arraycopy(a, pos, result, pos+b.length ,a.length-pos); //put the second part of a in (part after b) + } + return result; + } + + + public static void main(String[] args) { + // TODO Auto-generated method stub + int[] a = { 1, 2, 3, 4}; + int[] b = { 5, 6, 7}; + int[] c = { 8, 9, 10, 11, 12}; + + System.out.print("Printing array a: "); Replace.printArray(a); + System.out.print("Printing array b: "); Replace.printArray(b); + System.out.print("Printing array c: "); Replace.printArray(c); + + System.out.println("Now calling inserts!"); + System.out.println("------------------------"); + int[] d = insert(a,b,1); + int[] e = insert(a,b,4); //ERROR + int[] f = insert(b,a,0); + int[] g = insert(c,a,3); + int[] h = insert(c,a,-1); + int[] i = insert(c,a,7); //ERROR + + System.out.print("Printing array a, expecting {1,2,3,4} : "); Replace.printArray(a); + System.out.print("Printing array b, expecting {5,6,7} : "); Replace.printArray(b); + System.out.print("Printing array c, expecting {8,9,10,11,12} : "); Replace.printArray(c); + System.out.print("Printing array d, expecting {1,5,6,7,2,3,4} : "); Replace.printArray(d); + System.out.print("Printing array e, expecting {1,2,3,4,5,6,7} : "); Replace.printArray(e); + System.out.print("Printing array f, expecting {1,2,3,4,5,6,7}: "); Replace.printArray(f); + System.out.print("Printing array g, expecting {8,9,10,1,2,3,4,11,12} : "); Replace.printArray(g); + System.out.print("Printing array h, expecting {1,2,3,4,8,9,10,11,12} : "); Replace.printArray(h); + System.out.print("Printing array i, expecting {8,9,10,11,12,1,2,3,4} : "); Replace.printArray(i); + + } + +} diff --git a/Week8/SublimeText/IsReverse.class b/Week8/SublimeText/IsReverse.class new file mode 100644 index 0000000..b918ab2 Binary files /dev/null and b/Week8/SublimeText/IsReverse.class differ diff --git a/Week8/SublimeText/IsReverse.java b/Week8/SublimeText/IsReverse.java new file mode 100644 index 0000000..3cf02dc --- /dev/null +++ b/Week8/SublimeText/IsReverse.java @@ -0,0 +1,35 @@ +//This code is written by Siwat Sirichai +import java.util.Arrays; + +public class IsReverse { + + public static boolean isReverse(int[] a, int[] b) { + if (a==null&&b==null)return true; //Special Case #1: if a and b is null, result: true, return right away to prevent NullPointer exception. + if((a==null&&b!=null)||(a!=null&&b==null))return false; //Special Case #2: if only one of a or b is null, result: false, return right away to prevent NullPointer exception. + if (a.length != b.length)return false; //Special Case #3: if the length of a and b is not equal, result: false, return right away to prevent ArrayIndexoutofBound exception. + for(int i = 0;i