add Week8 Files
This commit is contained in:
parent
5fc39bc569
commit
88e09d3d46
31 changed files with 609 additions and 0 deletions
10
Week8/EclipseIDE/Assignment8/.classpath
Normal file
10
Week8/EclipseIDE/Assignment8/.classpath
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-14">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
17
Week8/EclipseIDE/Assignment8/.project
Normal file
17
Week8/EclipseIDE/Assignment8/.project
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Assignment8</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
|
@ -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
|
BIN
Week8/EclipseIDE/Assignment8/bin/module-info.class
Normal file
BIN
Week8/EclipseIDE/Assignment8/bin/module-info.class
Normal file
Binary file not shown.
BIN
Week8/EclipseIDE/Assignment8/bin/partA/IsReverse.class
Normal file
BIN
Week8/EclipseIDE/Assignment8/bin/partA/IsReverse.class
Normal file
Binary file not shown.
BIN
Week8/EclipseIDE/Assignment8/bin/partA/package-info.class
Normal file
BIN
Week8/EclipseIDE/Assignment8/bin/partA/package-info.class
Normal file
Binary file not shown.
BIN
Week8/EclipseIDE/Assignment8/bin/partB/Replace.class
Normal file
BIN
Week8/EclipseIDE/Assignment8/bin/partB/Replace.class
Normal file
Binary file not shown.
BIN
Week8/EclipseIDE/Assignment8/bin/partB/package-info.class
Normal file
BIN
Week8/EclipseIDE/Assignment8/bin/partB/package-info.class
Normal file
Binary file not shown.
BIN
Week8/EclipseIDE/Assignment8/bin/partC/CountNumbers.class
Normal file
BIN
Week8/EclipseIDE/Assignment8/bin/partC/CountNumbers.class
Normal file
Binary file not shown.
BIN
Week8/EclipseIDE/Assignment8/bin/partC/package-info.class
Normal file
BIN
Week8/EclipseIDE/Assignment8/bin/partC/package-info.class
Normal file
Binary file not shown.
BIN
Week8/EclipseIDE/Assignment8/bin/partD/InsertArray.class
Normal file
BIN
Week8/EclipseIDE/Assignment8/bin/partD/InsertArray.class
Normal file
Binary file not shown.
BIN
Week8/EclipseIDE/Assignment8/bin/partD/package-info.class
Normal file
BIN
Week8/EclipseIDE/Assignment8/bin/partD/package-info.class
Normal file
Binary file not shown.
100
Week8/EclipseIDE/Assignment8/numbers.txt
Normal file
100
Week8/EclipseIDE/Assignment8/numbers.txt
Normal file
|
@ -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
|
2
Week8/EclipseIDE/Assignment8/src/module-info.java
Normal file
2
Week8/EclipseIDE/Assignment8/src/module-info.java
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
module Assignment8 {
|
||||||
|
}
|
37
Week8/EclipseIDE/Assignment8/src/partA/IsReverse.java
Normal file
37
Week8/EclipseIDE/Assignment8/src/partA/IsReverse.java
Normal file
|
@ -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;i<a.length;i++)if(a[i]!=b[a.length-i-1])return false; //This is compare the first element of a with the last element of b, then second first of a and second last of b, so on...
|
||||||
|
return true;//If none of the condition are met (both a and b is not null), the two array are the reverse of each other
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] a = { 1, 2, 2, 3, 4, 5 };
|
||||||
|
int[] b = { 5, 4, 3, 2, 2, 1 };
|
||||||
|
int[] c = { 1, 2, 0, 3, 4, 5 };
|
||||||
|
int[] d = { 1, 2, 2, 3, 4, 5, 0 };
|
||||||
|
int[] e = { 0, 5, 4, 3, 2, 2, 1 };
|
||||||
|
int[] f = null;
|
||||||
|
int[] g = null;
|
||||||
|
|
||||||
|
System.out.println("isReverse(a,b):" + isReverse(a, b) + ", expected to be true");
|
||||||
|
System.out.println("isReverse(b,c):" + isReverse(b, c) + ", expected to be false");
|
||||||
|
System.out.println("isReverse(a,c):" + isReverse(a, c) + ", expected to be false");
|
||||||
|
System.out.println("isReverse(d,e):" + isReverse(d, e) + ", expected to be true");
|
||||||
|
System.out.println("isReverse(e,d):" + isReverse(e, d) + ", expected to be true");
|
||||||
|
System.out.println("isReverse(d,b):" + isReverse(b, d) + ", expected to be false");
|
||||||
|
System.out.println("isReverse(f,g):" + isReverse(f, g) + ", expected to be true");
|
||||||
|
System.out.println("isReverse(f,a):" + isReverse(f, a) + ", expected to be false");
|
||||||
|
System.out.println("isReverse(a,f):" + isReverse(a, f) + ", expected to be false");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
1
Week8/EclipseIDE/Assignment8/src/partA/package-info.java
Normal file
1
Week8/EclipseIDE/Assignment8/src/partA/package-info.java
Normal file
|
@ -0,0 +1 @@
|
||||||
|
package partA;
|
56
Week8/EclipseIDE/Assignment8/src/partB/Replace.java
Normal file
56
Week8/EclipseIDE/Assignment8/src/partB/Replace.java
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
//This code is written by Siwat Sirichai
|
||||||
|
package partB;
|
||||||
|
public class Replace {
|
||||||
|
|
||||||
|
public static int[] replace(int[] a, int v, int v2) {
|
||||||
|
if (a==null)return a; //If a is a null pointer, return immediately to prevent a NullPointerException
|
||||||
|
for(int i = 0; i < a.length;i++) //loop from 0 to the length of array a, number of completed iteration is stored in i (type int)
|
||||||
|
if(a[i]==v)a[i]=v2; //if the value of a at index i is equal to v, replace it with v2
|
||||||
|
return a; //Return the replaced array a
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printArray(int[] a) {
|
||||||
|
if(a==null) {
|
||||||
|
System.out.println(); //Create a newline to make the format looks uniform
|
||||||
|
return; //If a is a null pointer, return immediately to prevent a NullPointerException
|
||||||
|
}
|
||||||
|
//NOTE: return; (a return statement without a return value) in a method with no return type will break out of the method.
|
||||||
|
for(int i = 0;i<a.length;i++) { //loop from 0 to the length of array a, number of completed iteration is stored in i (type int)
|
||||||
|
System.out.print(a[i]); //print out the value of array a at index i
|
||||||
|
if(i<a.length-1)System.out.print(","); //print out a comma if the printed digit before it is not the last digit (last digit does not need a comma)
|
||||||
|
}
|
||||||
|
System.out.println(); //Create a newline to make the format looks uniform
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
int[] a = { 1, 2, 3, 1, 5, 4, 1, 2, 3, 1 };
|
||||||
|
int[] b = { 1, 5, 5, 1, 4, 5, 4, 2, 1, 5, 5, 2};
|
||||||
|
int[] b2 = { 1, 5, 5, 1, 4, 5, 4, 2, 1, 5, 5, 2};
|
||||||
|
int[] c = null;
|
||||||
|
int[] d = { 1, 2, 3, 4};
|
||||||
|
|
||||||
|
System.out.print("Printing array a: "); printArray(a);
|
||||||
|
System.out.print("Printing array b: "); printArray(b);
|
||||||
|
System.out.print("Printing array b2: "); printArray(b2);
|
||||||
|
System.out.print("Printing array c: "); printArray(c);
|
||||||
|
System.out.print("Printing array d: "); printArray(d);
|
||||||
|
|
||||||
|
System.out.println("Now calling replace in each!");
|
||||||
|
replace(a,1,55);
|
||||||
|
replace(b,5,66);
|
||||||
|
replace(b2,4,77);
|
||||||
|
replace(c,1,5);
|
||||||
|
replace(d,5,7);
|
||||||
|
|
||||||
|
System.out.println("Printing result of each replace!");
|
||||||
|
System.out.print("Printing array a, replacing 1 with 55: "); printArray(a);
|
||||||
|
System.out.print("Printing array b, replacing 5 with 66: "); printArray(b);
|
||||||
|
System.out.print("Printing array b2, replacing 4 with 77: "); printArray(b2);
|
||||||
|
System.out.print("Printing array c, replacing 1 with 5: "); printArray(c);
|
||||||
|
System.out.print("Printing array d, replacing 5 with 7: "); printArray(d);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
1
Week8/EclipseIDE/Assignment8/src/partB/package-info.java
Normal file
1
Week8/EclipseIDE/Assignment8/src/partB/package-info.java
Normal file
|
@ -0,0 +1 @@
|
||||||
|
package partB;
|
31
Week8/EclipseIDE/Assignment8/src/partC/CountNumbers.java
Normal file
31
Week8/EclipseIDE/Assignment8/src/partC/CountNumbers.java
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
//This code is written by Siwat Sirichai
|
||||||
|
package partC;
|
||||||
|
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;i<int_array.length;i++)int_array[i]=sc.nextInt(); //populate the array with the file's value
|
||||||
|
System.out.println("The number of integers greater than 25: "+greater(int_array,25));
|
||||||
|
System.out.println("The number of integers greater than 25: "+greater(int_array,50));
|
||||||
|
System.out.println("The number of integers greater than 25: "+greater(int_array,75));
|
||||||
|
} catch(FileNotFoundException e) {
|
||||||
|
System.out.println("File \"numbers.txt\" is not found in the current directory!"); //If no fie named numbers.txt is found, show "File "numbers.txt" is not found in the current directory!" and exit the program with exit code 0
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
public static int greater(int[] a,int v) {
|
||||||
|
int count = 0; //Create an int name count to store the number of numbers that are greater than a at all indexes
|
||||||
|
for(int val : a)if(val>v)count++;//This is called a for-each loop, it will loop for a.length iteration,
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
1
Week8/EclipseIDE/Assignment8/src/partC/package-info.java
Normal file
1
Week8/EclipseIDE/Assignment8/src/partC/package-info.java
Normal file
|
@ -0,0 +1 @@
|
||||||
|
package partC;
|
60
Week8/EclipseIDE/Assignment8/src/partD/InsertArray.java
Normal file
60
Week8/EclipseIDE/Assignment8/src/partD/InsertArray.java
Normal file
|
@ -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<length;i++) b[i+bpos] = a[i+apos];
|
||||||
|
}
|
||||||
|
public static int[] insert(int[] a, int[] b, int pos) {
|
||||||
|
int[] result = new int[a.length+b.length]; //Create an array with length of a+b
|
||||||
|
if (pos<=0){ //If pos <= 0, just put a b before a
|
||||||
|
arraycopy(b, 0, result, 0, b.length);
|
||||||
|
arraycopy(a, 0, result, b.length, a.length);
|
||||||
|
}
|
||||||
|
else if (pos>=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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
1
Week8/EclipseIDE/Assignment8/src/partD/package-info.java
Normal file
1
Week8/EclipseIDE/Assignment8/src/partD/package-info.java
Normal file
|
@ -0,0 +1 @@
|
||||||
|
package partD;
|
BIN
Week8/SublimeText/CountNumbers.class
Normal file
BIN
Week8/SublimeText/CountNumbers.class
Normal file
Binary file not shown.
30
Week8/SublimeText/CountNumbers.java
Normal file
30
Week8/SublimeText/CountNumbers.java
Normal file
|
@ -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;i<int_array.length;i++)int_array[i]=sc.nextInt(); //populate the array with the file's value
|
||||||
|
System.out.println("The number of integers greater than 25: "+greater(int_array,25));
|
||||||
|
System.out.println("The number of integers greater than 25: "+greater(int_array,50));
|
||||||
|
System.out.println("The number of integers greater than 25: "+greater(int_array,75));
|
||||||
|
} catch(FileNotFoundException e) {
|
||||||
|
System.out.println("File \"numbers.txt\" is not found in the current directory!"); //If no fie named numbers.txt is found, show "File "numbers.txt" is not found in the current directory!" and exit the program with exit code 0
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
public static int greater(int[] a,int v) {
|
||||||
|
int count = 0; //Create an int name count to store the number of numbers that are greater than a at all indexes
|
||||||
|
for(int val : a)if(val>v)count++;//This is called a for-each loop, it will loop for a.length iteration,
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
BIN
Week8/SublimeText/InsertArray.class
Normal file
BIN
Week8/SublimeText/InsertArray.class
Normal file
Binary file not shown.
58
Week8/SublimeText/InsertArray.java
Normal file
58
Week8/SublimeText/InsertArray.java
Normal file
|
@ -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<length;i++) b[i+bpos] = a[i+apos];
|
||||||
|
}
|
||||||
|
public static int[] insert(int[] a, int[] b, int pos) {
|
||||||
|
int[] result = new int[a.length+b.length]; //Create an array with length of a+b
|
||||||
|
if (pos<=0){ //If pos <= 0, just put a b before a
|
||||||
|
arraycopy(b, 0, result, 0, b.length);
|
||||||
|
arraycopy(a, 0, result, b.length, a.length);
|
||||||
|
}
|
||||||
|
else if (pos>=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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
BIN
Week8/SublimeText/IsReverse.class
Normal file
BIN
Week8/SublimeText/IsReverse.class
Normal file
Binary file not shown.
35
Week8/SublimeText/IsReverse.java
Normal file
35
Week8/SublimeText/IsReverse.java
Normal file
|
@ -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<a.length;i++)if(a[i]!=b[a.length-i-1])return false; //This is compare the first element of a with the last element of b, then second first of a and second last of b, so on...
|
||||||
|
return true;//If none of the condition are met (both a and b is not null), the two array are the reverse of each other
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] a = { 1, 2, 2, 3, 4, 5 };
|
||||||
|
int[] b = { 5, 4, 3, 2, 2, 1 };
|
||||||
|
int[] c = { 1, 2, 0, 3, 4, 5 };
|
||||||
|
int[] d = { 1, 2, 2, 3, 4, 5, 0 };
|
||||||
|
int[] e = { 0, 5, 4, 3, 2, 2, 1 };
|
||||||
|
int[] f = null;
|
||||||
|
int[] g = null;
|
||||||
|
|
||||||
|
System.out.println("isReverse(a,b):" + isReverse(a, b) + ", expected to be true");
|
||||||
|
System.out.println("isReverse(b,c):" + isReverse(b, c) + ", expected to be false");
|
||||||
|
System.out.println("isReverse(a,c):" + isReverse(a, c) + ", expected to be false");
|
||||||
|
System.out.println("isReverse(d,e):" + isReverse(d, e) + ", expected to be true");
|
||||||
|
System.out.println("isReverse(e,d):" + isReverse(e, d) + ", expected to be true");
|
||||||
|
System.out.println("isReverse(d,b):" + isReverse(b, d) + ", expected to be false");
|
||||||
|
System.out.println("isReverse(f,g):" + isReverse(f, g) + ", expected to be true");
|
||||||
|
System.out.println("isReverse(f,a):" + isReverse(f, a) + ", expected to be false");
|
||||||
|
System.out.println("isReverse(a,f):" + isReverse(a, f) + ", expected to be false");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
BIN
Week8/SublimeText/Replace.class
Normal file
BIN
Week8/SublimeText/Replace.class
Normal file
Binary file not shown.
55
Week8/SublimeText/Replace.java
Normal file
55
Week8/SublimeText/Replace.java
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
//This code is written by Siwat Sirichai
|
||||||
|
public class Replace {
|
||||||
|
|
||||||
|
public static int[] replace(int[] a, int v, int v2) {
|
||||||
|
if (a==null)return a; //If a is a null pointer, return immediately to prevent a NullPointerException
|
||||||
|
for(int i = 0; i < a.length;i++) //loop from 0 to the length of array a, number of completed iteration is stored in i (type int)
|
||||||
|
if(a[i]==v)a[i]=v2; //if the value of a at index i is equal to v, replace it with v2
|
||||||
|
return a; //Return the replaced array a
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printArray(int[] a) {
|
||||||
|
if(a==null) {
|
||||||
|
System.out.println(); //Create a newline to make the format looks uniform
|
||||||
|
return; //If a is a null pointer, return immediately to prevent a NullPointerException
|
||||||
|
}
|
||||||
|
//NOTE: return; (a return statement without a return value) in a method with no return type will break out of the method.
|
||||||
|
for(int i = 0;i<a.length;i++) { //loop from 0 to the length of array a, number of completed iteration is stored in i (type int)
|
||||||
|
System.out.print(a[i]); //print out the value of array a at index i
|
||||||
|
if(i<a.length-1)System.out.print(","); //print out a comma if the printed digit before it is not the last digit (last digit does not need a comma)
|
||||||
|
}
|
||||||
|
System.out.println(); //Create a newline to make the format looks uniform
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
int[] a = { 1, 2, 3, 1, 5, 4, 1, 2, 3, 1 };
|
||||||
|
int[] b = { 1, 5, 5, 1, 4, 5, 4, 2, 1, 5, 5, 2};
|
||||||
|
int[] b2 = { 1, 5, 5, 1, 4, 5, 4, 2, 1, 5, 5, 2};
|
||||||
|
int[] c = null;
|
||||||
|
int[] d = { 1, 2, 3, 4};
|
||||||
|
|
||||||
|
System.out.print("Printing array a: "); printArray(a);
|
||||||
|
System.out.print("Printing array b: "); printArray(b);
|
||||||
|
System.out.print("Printing array b2: "); printArray(b2);
|
||||||
|
System.out.print("Printing array c: "); printArray(c);
|
||||||
|
System.out.print("Printing array d: "); printArray(d);
|
||||||
|
|
||||||
|
System.out.println("Now calling replace in each!");
|
||||||
|
replace(a,1,55);
|
||||||
|
replace(b,5,66);
|
||||||
|
replace(b2,4,77);
|
||||||
|
replace(c,1,5);
|
||||||
|
replace(d,5,7);
|
||||||
|
|
||||||
|
System.out.println("Printing result of each replace!");
|
||||||
|
System.out.print("Printing array a, replacing 1 with 55: "); printArray(a);
|
||||||
|
System.out.print("Printing array b, replacing 5 with 66: "); printArray(b);
|
||||||
|
System.out.print("Printing array b2, replacing 4 with 77: "); printArray(b2);
|
||||||
|
System.out.print("Printing array c, replacing 1 with 5: "); printArray(c);
|
||||||
|
System.out.print("Printing array d, replacing 5 with 7: "); printArray(d);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
100
Week8/SublimeText/numbers.txt
Normal file
100
Week8/SublimeText/numbers.txt
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue