Skip to content

Commit c3267c0

Browse files
committed
added example code for lec 5
1 parent caf262f commit c3267c0

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed

lec4/live2/Sort.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/** We will provide methods for printing out args in sorted order.
2+
* @author Josh Hug
3+
*/
4+
5+
public class Sort {
6+
/** Puts A into sorted order. */
7+
public static void sort(String[] a) {
8+
sort(a, 0);
9+
}
10+
11+
/** Sorts the array A starting from START. */
12+
private static void sort(String[] a, int start) {
13+
if (start == a.length)
14+
return;
15+
16+
// Find the smallest thing (location).
17+
int mindex = indexOfSmallest(a, start);
18+
19+
// Swap it to the front.
20+
swap(a, start, mindex);
21+
22+
// Selection sort the remaining N-1 things. [done]
23+
sort(a, start + 1);
24+
}
25+
26+
/** Swaps the items in IX and IY of A. */
27+
public static void swap(String[] a, int ix, int iy) {
28+
String temp = a[ix];
29+
a[ix] = a[iy];
30+
a[iy] = temp;
31+
}
32+
33+
/** Returns the smallest item in A's position, starting from
34+
* position START. */
35+
public static int indexOfSmallest(String[] a, int start) {
36+
int mindex = start;
37+
int i = start;
38+
while (i < a.length) {
39+
int compareNumber = a[i].compareTo(a[mindex]);
40+
41+
if (compareNumber < 0) {
42+
mindex = i;
43+
}
44+
i = i + 1;
45+
}
46+
return mindex;
47+
}
48+
49+
/** Prints out A. */
50+
public static void print(String[] a) {
51+
52+
}
53+
54+
/** Prints ARGS in sorted order.
55+
*/
56+
public static void main(String[] args) {
57+
58+
}
59+
}

lec4/live2/TestSort.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/** Tests the methods in the Sort class.
2+
* @author Josh Hug
3+
*/
4+
import org.junit.Test;
5+
import static org.junit.Assert.*;
6+
7+
/** current annoyances:
8+
* 1. We have to manually invoke all of our tests in main.
9+
* 2. We have to comment things out that we don't actaully want to run.
10+
* 2b (alternate statement). Test failure causes entire thing to stop.
11+
* 3. Output is hard to read.
12+
13+
To fix these:
14+
1. Prepend each test with @org.junit.Test
15+
2. Let's make all tests non-static (!!??)
16+
3. Replace manual invocations with jh61b.junit.textui.runClasses
17+
18+
Remaining annoyances:
19+
1. It is annoying to write org.junit all over the place
20+
2. org.junit.Assert is also annoying.
21+
22+
To fix these, use MORE MAGIC:
23+
import org.junit.Test; <-- means you can just type @Test
24+
import static org.junit.Assert.*; <-- you can omit org.junit.Assert
25+
*/
26+
27+
public class TestSort {
28+
/** Tests the sort method of the Sort class. */
29+
@Test
30+
public void testSort() {
31+
String[] input = {"a", "clever", "happy", "dog"};
32+
String[] expected = {"a", "clever", "dog", "happy"};
33+
Sort.sort(input);
34+
35+
// Check that input is equal to expected.
36+
// If it is not, then print out a useful message for
37+
// the programmer (to be used for fixing code).
38+
assertArrayEquals(expected, input);
39+
}
40+
41+
@Test
42+
public void testIndexOfSmallest() {
43+
String[] input = {"a", "clever", "happy", "dog"};
44+
int actual = Sort.indexOfSmallest(input, 0);
45+
int expected = 0;
46+
47+
assertEquals(expected, actual);
48+
actual = Sort.indexOfSmallest(input, 2);
49+
expected = 3;
50+
51+
assertEquals(expected, actual);
52+
53+
}
54+
55+
@Test
56+
public void testSwap() {
57+
String[] input = {"a", "clever", "happy", "dog"};
58+
Sort.swap(input, 0, 2);
59+
String[] expected = {"happy", "clever", "a", "dog"};
60+
assertArrayEquals(input, expected);
61+
}
62+
63+
/** Run tests.
64+
*/
65+
public static void main(String[] args) {
66+
jh61b.junit.textui.runClasses(TestSort.class);
67+
}
68+
}
69+
70+
/** Graveyard of forgotten dreams and sadness.
71+
// Check that input is equal to expected.
72+
// If it is not, then print out a useful message for
73+
// the programmer (to be used for fixing code).
74+
if (input.length != expected.length) {
75+
System.out.println("testSort(): Lengths did not match!");
76+
System.out.println("input length: " + input.length);
77+
System.out.println("expected length: " + expected.length);
78+
}
79+
80+
int i = 0;
81+
while (i < input.length) {
82+
if (input[i] != expected[i]) {
83+
...
84+
85+
}
86+
} */

lec5/examples/FloatingPoint.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/** Some Java mysteries involving floating point numbers.
2+
* Won't talk about today (phew...)
3+
* @author Josh Hug
4+
*/
5+
6+
public class FloatingPoint {
7+
/**
8+
*/
9+
10+
public static void main(String[] args) {
11+
System.out.println(0.1);
12+
System.out.println(0.2);
13+
System.out.println(0.1 + 0.2);
14+
System.out.println(0.1 + 0.2 - 0.1);
15+
}
16+
}

lec5/examples/Integers.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** Some Java mysteries involving integers.
2+
* @author Josh Hug
3+
*/
4+
5+
public class Integers {
6+
public static void main(String[] args) {
7+
System.out.println(2147483647 + 1);
8+
System.out.println(1000000000 * 5);
9+
System.out.println(29 & 15);
10+
}
11+
}

lec5/examples/PowerTour.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.io.IOException;
2+
/** Experimentation with integers
3+
* @author Josh Hug
4+
*/
5+
6+
public class PowerTour {
7+
private static void powerTour(int x) throws IOException {
8+
int acc = x;
9+
while (true) {
10+
System.out.println(acc);
11+
System.in.read();
12+
acc = acc * x;
13+
}
14+
}
15+
16+
public static void main(String[] args) throws IOException {
17+
powerTour(32);
18+
}
19+
}

0 commit comments

Comments
 (0)