Skip to content

Commit 2be92ef

Browse files
committed
Java sample code
1 parent f7a9e4f commit 2be92ef

43 files changed

Lines changed: 103151 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

java/Test.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Test {
2+
public static void main(String[] args) {
3+
4+
System.out.println(3%9);
5+
System.out.println(foo(3, 9));
6+
7+
}
8+
9+
public static int foo(int a, int b){
10+
if(a%b == 0 ){
11+
return b;
12+
}
13+
else return foo(b,a%b);
14+
15+
16+
}
17+
}

java/sample1/Problem1.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @Fr4nc3
3+
* Programing Q1
4+
*/
5+
public class Problem1 {
6+
public static final int ARRAY_SIZE = 5;
7+
8+
public static <AnyType extends Comparable<AnyType>> AnyType findMax(AnyType[] arr) {
9+
int maxIndex = 0;
10+
for (int i = 0; i < arr.length; ++i) {
11+
if (arr[i].compareTo(arr[maxIndex]) > 0) {
12+
maxIndex = i;
13+
}
14+
}
15+
return arr[maxIndex];
16+
}
17+
18+
public static void main(String[] args) {
19+
/*
20+
* First test of findMax with a simple Array
21+
*/
22+
23+
Rectangle[] a = new Rectangle[ARRAY_SIZE];
24+
a[0] = new Rectangle(1, 2);
25+
a[1] = new Rectangle(2, 2);
26+
a[2] = new Rectangle(2, 3);
27+
a[3] = new Rectangle(3, 3);
28+
a[4] = new Rectangle(3, 4);
29+
30+
Rectangle maxRectangle = findMax(a);
31+
System.out.println("Found max Rectangle" + maxRectangle.toString());
32+
33+
/*
34+
* second test creating the dimensions of the array dynamically.
35+
*/
36+
System.out.println("\n\n\nSecond test with random Rectangles:\n");
37+
38+
Rectangle[] a2 = new Rectangle[ARRAY_SIZE];
39+
40+
for (int i = 0; i < ARRAY_SIZE; ++i) {
41+
int width = (int) (Math.random() * 50 + 1); //from1,50
42+
int length = (int) (Math.random() * 50 + 1);
43+
a2[i] = new Rectangle(length, width);
44+
45+
}
46+
47+
System.out.println("New Array Rectangles Dimensions:\n");
48+
49+
for (int i = 0; i < ARRAY_SIZE; ++i) {
50+
System.out.println("Rectangle " + i);
51+
System.out.println(a2[i].toString() + "\n");
52+
}
53+
54+
Rectangle newMaxRectangle = findMax(a2);
55+
System.out.println("Found max Rectangle" + newMaxRectangle.toString());
56+
}
57+
}

java/sample1/Problem2.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* @Fr4nc3
3+
* Problem2 class
4+
* Programing Q2
5+
*/
6+
import java.util.Arrays;
7+
8+
public class Problem2 {
9+
public static final int ARRAY_SIZE = 5;
10+
11+
public static <AnyType extends Comparable<AnyType>> int binarySearch(AnyType[] a, AnyType x) {
12+
return binarySearch(a, x, 0, a.length - 1);
13+
}
14+
15+
public static <AnyType extends Comparable<AnyType>> int binarySearch(AnyType[] a, AnyType x,
16+
int low, int high) {
17+
if (low > high) return -1;
18+
int mid = (low + high) / 2;
19+
if (a[mid].compareTo(x) == 0) return mid;
20+
else if (a[mid].compareTo(x) < 0)
21+
return binarySearch(a, x, mid + 1, high);
22+
else // last possibility
23+
return binarySearch(a, x, low, mid - 1);
24+
}
25+
26+
public static void main(String[] args) {
27+
28+
Rectangle[] a = new Rectangle[ARRAY_SIZE];
29+
//create an empty rectangle that we will use to find after the sort
30+
Rectangle findRectangle = new Rectangle(0, 0);
31+
for (int i = 0; i < ARRAY_SIZE; ++i) {
32+
// generate dimensions randomly
33+
int width = (int) (Math.random() * 50 + 1);
34+
int length = (int) (Math.random() * 50 + 1);
35+
36+
// we give real dimension to our rectangle
37+
// this means it always will appear in this test case
38+
if (findRectangle.getLength() == 0) {
39+
findRectangle.setLength(length);
40+
findRectangle.setWidth(width);
41+
}
42+
43+
a[i] = new Rectangle(length, width);
44+
}
45+
Arrays.sort(a);
46+
System.out.println("New Array Rectangles Dimensions:\n");
47+
48+
for (int i = 0; i < ARRAY_SIZE; ++i) {
49+
System.out.println("Rectangle " + i);
50+
System.out.println(a[i].toString() + "\n");
51+
}
52+
53+
System.out.println("Found Rectangle" + findRectangle.toString());
54+
System.out.println("Rectangle Index: " + binarySearch(a, findRectangle));
55+
56+
/*
57+
* Second test we create a rectangle with random dimensions and
58+
* it may or may not exist
59+
* */
60+
int width = (int) (Math.random() * 50 + 1);
61+
int length = (int) (Math.random() * 50 + 1);
62+
Rectangle doExistRectangle = new Rectangle(length, width);
63+
64+
System.out.println("\n\nNew Rectangle to find" + doExistRectangle.toString());
65+
System.out.println("Rectangle Index? " + binarySearch(a, doExistRectangle));
66+
67+
}
68+
}

java/sample1/Problem3.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* @Fr4nc3
3+
* Problem3 class
4+
* Programing Q3
5+
*/
6+
public class Problem3 {
7+
public static final int MAX_TEST_POWER = 9;
8+
9+
public static void methodA(int n) {
10+
int sum = 0;
11+
for (int i = 0; i < 23; i++) {
12+
for (int j = 0; j < n; j++) {
13+
sum = sum + 1;
14+
}
15+
}
16+
}
17+
18+
public static void methodB(int n) {
19+
int sum = 0;
20+
for (int i = 0; i < n; i++) {
21+
for (int k = i; k < n; k++) {
22+
sum = sum + 1;
23+
}
24+
}
25+
}
26+
27+
public static int foo(int n, int k) {
28+
if (n <= k)
29+
return 1;
30+
else
31+
return foo(n / k, k) + 1;
32+
}
33+
34+
public static int getTheN(int n) {
35+
return (int) Math.pow(10, n);
36+
}
37+
38+
public static void main(String[] args) {
39+
40+
long startTime, endTime;
41+
/*
42+
* TEST METHOD A
43+
* */
44+
for (int i = 0; i <= MAX_TEST_POWER; ++i) {
45+
startTime = System.nanoTime();
46+
int testN = getTheN(i);
47+
System.out.println("methodA N: " + testN);
48+
methodA(testN);
49+
endTime = System.nanoTime();
50+
System.out.println(endTime - startTime);
51+
}
52+
53+
/*
54+
* TEST METHOD B
55+
* */
56+
for (int i = 0; i <= MAX_TEST_POWER; ++i) {
57+
startTime = System.nanoTime();
58+
int testN = getTheN(i);
59+
System.out.println("methodB N: " + testN);
60+
methodB(testN);
61+
endTime = System.nanoTime();
62+
System.out.println(endTime - startTime);
63+
}
64+
65+
/*
66+
* TEST FOO
67+
* */
68+
for (int i = 0; i <= MAX_TEST_POWER; ++i) {
69+
startTime = System.nanoTime();
70+
int testN = getTheN(i);
71+
System.out.println("foo N: " + testN);
72+
int fooTest = foo(testN, 2); // k = 2 as requested in the hw description
73+
endTime = System.nanoTime();
74+
System.out.println(endTime - startTime);
75+
}
76+
}
77+
}

java/sample1/Problem3.txt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
methodA N: 1
2+
172429
3+
methodA N: 10
4+
17243
5+
methodA N: 100
6+
43518
7+
methodA N: 1000
8+
258644
9+
methodA N: 10000
10+
1011586
11+
methodA N: 100000
12+
539457
13+
methodA N: 1000000
14+
74720
15+
methodA N: 10000000
16+
60760
17+
methodA N: 100000000
18+
48444
19+
methodA N: 1000000000
20+
46802
21+
22+
methodB N: 1
23+
15190
24+
methodB N: 10
25+
11906
26+
methodB N: 100
27+
68150
28+
methodB N: 1000
29+
1701714
30+
methodB N: 10000
31+
655642
32+
methodB N: 100000
33+
986132
34+
methodB N: 1000000
35+
13138
36+
methodB N: 10000000
37+
11085
38+
methodB N: 100000000
39+
10675
40+
methodB N: 1000000000
41+
10674
42+
43+
44+
foo N: 1
45+
13959
46+
foo N: 10
47+
11906
48+
foo N: 100
49+
9853
50+
foo N: 1000
51+
10263
52+
foo N: 10000
53+
14779
54+
foo N: 100000
55+
10674
56+
foo N: 1000000
57+
10264
58+
foo N: 10000000
59+
10674
60+
foo N: 100000000
61+
11085
62+
foo N: 1000000000
63+
12727
64+
65+
The numbers in nanoseconds don't seem to be accurate and my theory (and after some googling)
66+
it is because JVM optimize the process, also my results may be different for reason such as
67+
68+
1) Laptop setup
69+
2) Program that I am running parallel to the test (AKA memory available for the test)
70+
71+
I found this online
72+
http://stackoverflow.com/questions/9724950/java-system-nanotime-elapsed-average-time-keeps-getting-smaller
73+
74+
and I am basing my results in this theory.
75+
Java's JIT(Just-in-time compilation) optimizes code that gets run extensively -- the more it's run,
76+
the more effort the JIT puts into optimizing it.
77+

java/sample1/README.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@Fr4nc3
2+
==========
3+
4+
Problem1.java
5+
Rectangle.java
6+
Problem2.java
7+
Problem3.java
8+
Problem3.txt
9+
README.txt
10+
11+
12+
Problem 1
13+
=========
14+
- Rectangle class
15+
- Implement comparable
16+
- Problem1 class
17+
- has two arrays to test the findMax method
18+
- one array was created hard code rectangles
19+
- second array was created with rectangles with random width and length.
20+
- random dimension number goes between 1 to 50
21+
22+
Problem 2
23+
=========
24+
- uses Rectangle class from Problem 1
25+
- test with an array created with rectangles with random width and length.
26+
- BinarySearch was convert from book example to recursive
27+
- one rectangle to used to find was explicitly created in the array
28+
- other test rectangle was created randomly and may or may not appear in the array
29+
- random dimension number goes between 1 to 50 too
30+
31+
32+
Problem 3
33+
=========
34+
- Used System.nanoTime() instead of System.currentTimeMillis() as it was suggested
35+
- Used N = 10^n where n goes 1 to 9
36+
- Numbers for the benchmark can be not representative for the writing #3 because
37+
JAVA optimize the method. (see longer answer in Problem3.txt)
38+
39+
40+
HW CODE ENVIRONMENT
41+
==================
42+
Intel(R) Core(TM) i7-48070HQ CPU @ 2.50 GHz
43+
RAM 16GB
44+
Windows 10 Pro
45+
Java 1.8.0_72
46+
IntelliJ IDEA 15.0.3

0 commit comments

Comments
 (0)