Skip to content

Commit

Permalink
Downloaded All Exercises From Part 05
Browse files Browse the repository at this point in the history
  • Loading branch information
abidmuin committed May 8, 2024
1 parent fad7148 commit 793ce51
Show file tree
Hide file tree
Showing 87 changed files with 4,632 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
force_new_sandbox: true
73 changes: 73 additions & 0 deletions mooc-java-programming-i/part05-Part05_01.OneMinute/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>tkt</groupId>
<artifactId>Part05_01.OneMinute</artifactId>
<name>Part05_01.OneMinute</name>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>fi.helsinki.cs.tmc</groupId>
<artifactId>edu-test-utils</artifactId>
<version>0.4.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>tmc</id>
<name>TMC repo</name>
<url>https://maven.mooc.fi/releases</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>tmc</id>
<name>TMC repo</name>
<url>https://maven.mooc.fi/releases</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

public class ClockHand {

private int value;
private int limit;

public ClockHand(int limit) {
this.limit = limit;
this.value = 0;
}

public void advance() {
this.value = this.value + 1;

if (this.value >= this.limit) {
this.value = 0;
}
}

public int value() {
return this.value;
}

public String toString() {
if (this.value < 10) {
return "0" + this.value;
}

return "" + this.value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import java.util.Scanner;

public class Program {

public static void main(String[] args) {
// You can test your program here


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@

import fi.helsinki.cs.tmc.edutestutils.MockStdio;
import fi.helsinki.cs.tmc.edutestutils.Points;
import fi.helsinki.cs.tmc.edutestutils.Reflex;
import java.util.Random;
import org.junit.*;
import static org.junit.Assert.*;

@Points("05-01")
public class TimerTest {

@Rule
public MockStdio io = new MockStdio();

@Test
public void classAndConstructor() {
createTimer();
}

@Test
public void toStringInTheBeginning() {
Object timer = createTimer();

String toStringFromTimer = callToString(timer);

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"
+ "Timer t = new Timer();\n"
+ "System.out.println(t);", "00:00", toStringFromTimer);
}

@Test
public void advanceMethodExists() {
Object timer = createTimer();
Reflex.reflect("Timer").method("advance").returningVoid().takingNoParams().requirePublic();

try {
Reflex.reflect("Timer").method("advance").returningVoid().takingNoParams().invokeOn(timer);
} catch (Throwable t) {
fail("An error occurred when calling the method 'advance'. The error was: " + t.getMessage() + "\nTry it out:\n"
+ "Timer t = new Timer();\n"
+ "t.advance();");
}

String toStringFromTimer = callToString(timer);

assertEquals("After a timer has advanced once, the result of toString should be \"00:01\". Now it was " + toStringFromTimer + "Try it out yourself:\n"
+ "Timer t = new Timer();\n"
+ "t.advance();\n"
+ "System.out.println(t);", "00:01", toStringFromTimer);
}

@Test
public void advanceFar() {
Object timer = createTimer();

int randomAdvancementTime = new Random().nextInt(1000) + 1000;
for (int i = 1; i <= randomAdvancementTime; i++) {
try {
Reflex.reflect("Timer").method("advance").returningVoid().takingNoParams().invokeOn(timer);
} catch (Throwable t) {
fail("An error occurred when calling the 'advance' method. The error was: " + t.getMessage() + "\nTry calling the advance method " + (i) + " times.");
}
}

int seconds = ((randomAdvancementTime / 100) % 60);
int hundredths = (randomAdvancementTime % 100);

String s = seconds < 10 ? "0" + seconds : "" + seconds;
String hos = hundredths < 10 ? "0" + hundredths : "" + hundredths;

String toStringFromTimer = callToString(timer);
String expectedPrint = "" + s + ":" + hos;

assertEquals("When the advance method is called " + randomAdvancementTime + " times, the print should be \"" + expectedPrint + "\".\nNow it was " + toStringFromTimer + "\nTry it out:\n"
+ "Timer t = new Timer();\n"
+ "int i = 0;\n"
+ "while (i < " + randomAdvancementTime + ") {\n"
+ " t.advance();\n"
+ "}"
+ "System.out.println(t);", expectedPrint, toStringFromTimer);
}

@Test
public void advanceVeryFar() {
Object timer = createTimer();

int randomAdvancementTime = new Random().nextInt(10000) + 360000;
for (int i = 1; i <= randomAdvancementTime; i++) {
try {
Reflex.reflect("Timer").method("advance").returningVoid().takingNoParams().invokeOn(timer);
} catch (Throwable t) {
fail("An error occurred when calling the 'advance' method. The error was: " + t.getMessage() + "\nTry calling the advance method " + (i) + " times.");
}
}

int seconds = ((randomAdvancementTime / 100) % 60);
int hundredths = (randomAdvancementTime % 100);

String s = seconds < 10 ? "0" + seconds : "" + seconds;
String hos = hundredths < 10 ? "0" + hundredths : "" + hundredths;

String toStringFromTimer = callToString(timer);
String expectedPrint = "" + s + ":" + hos;

assertEquals("When the advance method is called " + randomAdvancementTime + " times, the print should be \"" + expectedPrint + "\".\nNow it was " + toStringFromTimer + "\nTry it out:\n"
+ "Timer t = new Timer();\n"
+ "int i = 0;\n"
+ "while (i < " + randomAdvancementTime + ") {\n"
+ " t.advance();\n"
+ "}"
+ "System.out.println(t);", expectedPrint, toStringFromTimer);
}

private String callToString(Object timer) {
String out = io.getSysOut();

String toStringFromTimer = null;

try {
toStringFromTimer = timer.toString();
} catch (Throwable e) {
fail("Error when calling the toString method of the timer. Try:\n"
+ "Timer timer = new Timer();\n"
+ "timer.toString();\n"
+ "The error that occurred: " + e.getMessage());
}

assertEquals("Calling the method toString shouldn't print anything. It is only to return a string.", out, io.getSysOut());

return toStringFromTimer;
}

private Object createTimer() {
Reflex.reflect("Timer").ctor().takingNoParams().requirePublic();
try {
return Reflex.reflect("Timer").ctor().takingNoParams().invoke();
} catch (Throwable ex) {
fail("An error occured while testing the program. Try:\nTimer timer = new Timer();\nError: " + ex.getMessage());
}
return null;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
force_new_sandbox: true
73 changes: 73 additions & 0 deletions mooc-java-programming-i/part05-Part05_02.Book/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>tkt</groupId>
<artifactId>Part05_02.Book</artifactId>
<name>Part05_02.Book</name>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>fi.helsinki.cs.tmc</groupId>
<artifactId>edu-test-utils</artifactId>
<version>0.4.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>tmc</id>
<name>TMC repo</name>
<url>https://maven.mooc.fi/releases</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>tmc</id>
<name>TMC repo</name>
<url>https://maven.mooc.fi/releases</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

public class Program {

public static void main(String[] args) {
// This is simply an empty main method where you are
// free to experiment with your Book class.
// An example that you can use for testing:


// Book b = new Book("J. K. Rowling", "Harry Potter and the Sorcerer's Stone", 223);
// System.out.println(b);

}
}
Loading

0 comments on commit 793ce51

Please sign in to comment.