update .gitignore
This commit is contained in:
parent
a3b1782237
commit
4b16ccf00e
|
@ -0,0 +1,4 @@
|
||||||
|
.metadata/**
|
||||||
|
**/.metadata/**
|
||||||
|
**/.sonarlint/**
|
||||||
|
.sonarlint/**
|
|
@ -1,4 +1,5 @@
|
||||||
//This code is written by Siwat Sirichai
|
//This code is written by Siwat Sirichai
|
||||||
|
package partC;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
Binary file not shown.
Binary file not shown.
|
@ -1,58 +0,0 @@
|
||||||
//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);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Binary file not shown.
|
@ -1,35 +0,0 @@
|
||||||
//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");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Binary file not shown.
|
@ -1,55 +0,0 @@
|
||||||
//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);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,100 +0,0 @@
|
||||||
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
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
the java files in this folder require the Program Eclipse IDE
|
||||||
|
if you want to run it with your terminal, remove the package declaration first!
|
Loading…
Reference in New Issue