Skip to content

Commit 91843ac

Browse files
committed
Part 5 added
1 parent e8f66e5 commit 91843ac

93 files changed

Lines changed: 5150 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
force_new_sandbox: true
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project>
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>tkt</groupId>
5+
<artifactId>Part05_01.OneMinute</artifactId>
6+
<name>Part05_01.OneMinute</name>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>fi.helsinki.cs.tmc</groupId>
25+
<artifactId>edu-test-utils</artifactId>
26+
<version>0.4.2</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>3.8.0</version>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>exec-maven-plugin</artifactId>
41+
<version>1.6.0</version>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
<repositories>
47+
<repository>
48+
<id>tmc</id>
49+
<name>TMC repo</name>
50+
<url>https://maven.mooc.fi/releases</url>
51+
<releases>
52+
<enabled>true</enabled>
53+
</releases>
54+
<snapshots>
55+
<enabled>true</enabled>
56+
</snapshots>
57+
</repository>
58+
</repositories>
59+
60+
<pluginRepositories>
61+
<pluginRepository>
62+
<id>tmc</id>
63+
<name>TMC repo</name>
64+
<url>https://maven.mooc.fi/releases</url>
65+
<releases>
66+
<enabled>true</enabled>
67+
</releases>
68+
<snapshots>
69+
<enabled>true</enabled>
70+
</snapshots>
71+
</pluginRepository>
72+
</pluginRepositories>
73+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
public class ClockHand {
3+
4+
private int value;
5+
private int limit;
6+
7+
public ClockHand(int limit) {
8+
this.limit = limit;
9+
this.value = 0;
10+
}
11+
12+
public void advance() {
13+
this.value = this.value + 1;
14+
15+
if (this.value >= this.limit) {
16+
this.value = 0;
17+
}
18+
}
19+
20+
public int value() {
21+
return this.value;
22+
}
23+
24+
public String toString() {
25+
if (this.value < 10) {
26+
return "0" + this.value;
27+
}
28+
29+
return "" + this.value;
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
public class Program {
3+
4+
public static void main(String[] args) {
5+
6+
Timer timer = new Timer();
7+
8+
while(true) {
9+
System.out.println(timer);
10+
timer.advance();
11+
12+
try {
13+
Thread.sleep(10);
14+
} catch (Exception e) {
15+
System.out.println("Error: " + e.getMessage());
16+
}
17+
}
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
public class Timer {
3+
private ClockHand hundredths;
4+
private ClockHand seconds;
5+
6+
public Timer() {
7+
this.hundredths = new ClockHand(60);
8+
this.seconds = new ClockHand(100);
9+
}
10+
11+
public void advance() {
12+
this.seconds.advance();
13+
if(this.seconds.value() == 0) {
14+
this.hundredths.advance();
15+
}
16+
}
17+
18+
public String toString() {
19+
return this.hundredths + ":" + this.seconds;
20+
}
21+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
2+
import fi.helsinki.cs.tmc.edutestutils.MockStdio;
3+
import fi.helsinki.cs.tmc.edutestutils.Points;
4+
import fi.helsinki.cs.tmc.edutestutils.Reflex;
5+
import java.util.Random;
6+
import org.junit.*;
7+
import static org.junit.Assert.*;
8+
9+
@Points("05-01")
10+
public class TimerTest {
11+
12+
@Rule
13+
public MockStdio io = new MockStdio();
14+
15+
@Test
16+
public void classAndConstructor() {
17+
createTimer();
18+
}
19+
20+
@Test
21+
public void toStringInTheBeginning() {
22+
Object timer = createTimer();
23+
24+
String toStringFromTimer = callToString(timer);
25+
26+
assertEquals("Printing the result of toString of a newly created timer should result in \"00:00\". Now the output was " + toStringFromTimer + "\nTry it out yourself:\n"
27+
+ "Timer t = new Timer();\n"
28+
+ "System.out.println(t);", "00:00", toStringFromTimer);
29+
}
30+
31+
@Test
32+
public void advanceMethodExists() {
33+
Object timer = createTimer();
34+
Reflex.reflect("Timer").method("advance").returningVoid().takingNoParams().requirePublic();
35+
36+
try {
37+
Reflex.reflect("Timer").method("advance").returningVoid().takingNoParams().invokeOn(timer);
38+
} catch (Throwable t) {
39+
fail("An error occurred when calling the method 'advance'. The error was: " + t.getMessage() + "\nTry it out:\n"
40+
+ "Timer t = new Timer();\n"
41+
+ "t.advance();");
42+
}
43+
44+
String toStringFromTimer = callToString(timer);
45+
46+
assertEquals("After a timer has advanced once, the result of toString should be \"00:01\". Now it was " + toStringFromTimer + "Try it out yourself:\n"
47+
+ "Timer t = new Timer();\n"
48+
+ "t.advance();\n"
49+
+ "System.out.println(t);", "00:01", toStringFromTimer);
50+
}
51+
52+
@Test
53+
public void advanceFar() {
54+
Object timer = createTimer();
55+
56+
int randomAdvancementTime = new Random().nextInt(1000) + 1000;
57+
for (int i = 1; i <= randomAdvancementTime; i++) {
58+
try {
59+
Reflex.reflect("Timer").method("advance").returningVoid().takingNoParams().invokeOn(timer);
60+
} catch (Throwable t) {
61+
fail("An error occurred when calling the 'advance' method. The error was: " + t.getMessage() + "\nTry calling the advance method " + (i) + " times.");
62+
}
63+
}
64+
65+
int seconds = ((randomAdvancementTime / 100) % 60);
66+
int hundredths = (randomAdvancementTime % 100);
67+
68+
String s = seconds < 10 ? "0" + seconds : "" + seconds;
69+
String hos = hundredths < 10 ? "0" + hundredths : "" + hundredths;
70+
71+
String toStringFromTimer = callToString(timer);
72+
String expectedPrint = "" + s + ":" + hos;
73+
74+
assertEquals("When the advance method is called " + randomAdvancementTime + " times, the print should be \"" + expectedPrint + "\".\nNow it was " + toStringFromTimer + "\nTry it out:\n"
75+
+ "Timer t = new Timer();\n"
76+
+ "int i = 0;\n"
77+
+ "while (i < " + randomAdvancementTime + ") {\n"
78+
+ " t.advance();\n"
79+
+ "}"
80+
+ "System.out.println(t);", expectedPrint, toStringFromTimer);
81+
}
82+
83+
@Test
84+
public void advanceVeryFar() {
85+
Object timer = createTimer();
86+
87+
int randomAdvancementTime = new Random().nextInt(10000) + 360000;
88+
for (int i = 1; i <= randomAdvancementTime; i++) {
89+
try {
90+
Reflex.reflect("Timer").method("advance").returningVoid().takingNoParams().invokeOn(timer);
91+
} catch (Throwable t) {
92+
fail("An error occurred when calling the 'advance' method. The error was: " + t.getMessage() + "\nTry calling the advance method " + (i) + " times.");
93+
}
94+
}
95+
96+
int seconds = ((randomAdvancementTime / 100) % 60);
97+
int hundredths = (randomAdvancementTime % 100);
98+
99+
String s = seconds < 10 ? "0" + seconds : "" + seconds;
100+
String hos = hundredths < 10 ? "0" + hundredths : "" + hundredths;
101+
102+
String toStringFromTimer = callToString(timer);
103+
String expectedPrint = "" + s + ":" + hos;
104+
105+
assertEquals("When the advance method is called " + randomAdvancementTime + " times, the print should be \"" + expectedPrint + "\".\nNow it was " + toStringFromTimer + "\nTry it out:\n"
106+
+ "Timer t = new Timer();\n"
107+
+ "int i = 0;\n"
108+
+ "while (i < " + randomAdvancementTime + ") {\n"
109+
+ " t.advance();\n"
110+
+ "}"
111+
+ "System.out.println(t);", expectedPrint, toStringFromTimer);
112+
}
113+
114+
private String callToString(Object timer) {
115+
String out = io.getSysOut();
116+
117+
String toStringFromTimer = null;
118+
119+
try {
120+
toStringFromTimer = timer.toString();
121+
} catch (Throwable e) {
122+
fail("Error when calling the toString method of the timer. Try:\n"
123+
+ "Timer timer = new Timer();\n"
124+
+ "timer.toString();\n"
125+
+ "The error that occurred: " + e.getMessage());
126+
}
127+
128+
assertEquals("Calling the method toString shouldn't print anything. It is only to return a string.", out, io.getSysOut());
129+
130+
return toStringFromTimer;
131+
}
132+
133+
private Object createTimer() {
134+
Reflex.reflect("Timer").ctor().takingNoParams().requirePublic();
135+
try {
136+
return Reflex.reflect("Timer").ctor().takingNoParams().invoke();
137+
} catch (Throwable ex) {
138+
fail("An error occured while testing the program. Try:\nTimer timer = new Timer();\nError: " + ex.getMessage());
139+
}
140+
return null;
141+
}
142+
143+
144+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
force_new_sandbox: true
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project>
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>tkt</groupId>
5+
<artifactId>Part05_02.Book</artifactId>
6+
<name>Part05_02.Book</name>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>fi.helsinki.cs.tmc</groupId>
25+
<artifactId>edu-test-utils</artifactId>
26+
<version>0.4.2</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>3.8.0</version>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>exec-maven-plugin</artifactId>
41+
<version>1.6.0</version>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
<repositories>
47+
<repository>
48+
<id>tmc</id>
49+
<name>TMC repo</name>
50+
<url>https://maven.mooc.fi/releases</url>
51+
<releases>
52+
<enabled>true</enabled>
53+
</releases>
54+
<snapshots>
55+
<enabled>true</enabled>
56+
</snapshots>
57+
</repository>
58+
</repositories>
59+
60+
<pluginRepositories>
61+
<pluginRepository>
62+
<id>tmc</id>
63+
<name>TMC repo</name>
64+
<url>https://maven.mooc.fi/releases</url>
65+
<releases>
66+
<enabled>true</enabled>
67+
</releases>
68+
<snapshots>
69+
<enabled>true</enabled>
70+
</snapshots>
71+
</pluginRepository>
72+
</pluginRepositories>
73+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
public class Book {
3+
private String author;
4+
private String title;
5+
private int pages;
6+
7+
public Book(String author, String title, int pages) {
8+
this.author = author;
9+
this.title = title;
10+
this.pages = pages;
11+
}
12+
13+
public String getAuthor() {
14+
return this.author;
15+
}
16+
17+
public String getName() {
18+
return this.title;
19+
}
20+
21+
public int getPages() {
22+
return this.pages;
23+
}
24+
25+
public String toString() {
26+
return this.getAuthor() + ", " + this.getName() + ", " + this.getPages() + " pages";
27+
}
28+
}

0 commit comments

Comments
 (0)