Skip to content

Commit 4f0283e

Browse files
committed
Part 12
1 parent a463ecc commit 4f0283e

File tree

14 files changed

+240
-0
lines changed

14 files changed

+240
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
force_new_sandbox: true
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
<groupId>tkt</groupId>
8+
<artifactId>Part12_08.Lottery</artifactId>
9+
<name>Part12_08.Lottery</name>
10+
<version>1.0-SNAPSHOT</version>
11+
<packaging>jar</packaging>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>4.12</version>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>fi.helsinki.cs.tmc</groupId>
28+
<artifactId>edu-test-utils</artifactId>
29+
<version>0.4.2</version>
30+
<scope>test</scope>
31+
</dependency>
32+
</dependencies>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-compiler-plugin</artifactId>
39+
<version>3.8.0</version>
40+
</plugin>
41+
<plugin>
42+
<groupId>org.codehaus.mojo</groupId>
43+
<artifactId>exec-maven-plugin</artifactId>
44+
<version>1.6.0</version>
45+
</plugin>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-surefire-plugin</artifactId>
49+
<version>2.22.1</version>
50+
<configuration>
51+
<argLine>-Djdk.net.URLClassPath.disableClassPathURLCheck=true</argLine>
52+
</configuration>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
<repositories>
58+
<repository>
59+
<id>tmc</id>
60+
<name>TMC repo</name>
61+
<url>https://maven.mooc.fi/releases</url>
62+
<releases>
63+
<enabled>true</enabled>
64+
</releases>
65+
<snapshots>
66+
<enabled>true</enabled>
67+
</snapshots>
68+
</repository>
69+
</repositories>
70+
71+
<pluginRepositories>
72+
<pluginRepository>
73+
<id>tmc</id>
74+
<name>TMC repo</name>
75+
<url>https://maven.mooc.fi/releases</url>
76+
<releases>
77+
<enabled>true</enabled>
78+
</releases>
79+
<snapshots>
80+
<enabled>true</enabled>
81+
</snapshots>
82+
</pluginRepository>
83+
</pluginRepositories>
84+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
import java.util.ArrayList;
3+
import java.util.Random;
4+
5+
public class LotteryRow {
6+
7+
private Random random;
8+
private ArrayList<Integer> numbers;
9+
10+
public LotteryRow() {
11+
// Draw the numbers when the LotteryRow is created
12+
this.randomizeNumbers();
13+
}
14+
15+
public ArrayList<Integer> numbers() {
16+
return this.numbers;
17+
}
18+
19+
public void randomizeNumbers() {
20+
// Initialize the list for numbers
21+
this.numbers = new ArrayList<>();
22+
// Implement the random number generation here
23+
// the method containsNumber is probably useful
24+
this.random = new Random();
25+
for (int i = 0; i < 7; i++) {
26+
int randomNum = this.random.nextInt(40) + 1;
27+
if (containsNumber(randomNum)) {
28+
i--;
29+
continue;
30+
}
31+
this.numbers.add(randomNum);
32+
}
33+
}
34+
35+
public boolean containsNumber(int number) {
36+
// Check here whether the number is among the drawn numbers
37+
return this.numbers.contains(number);
38+
}
39+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
import java.util.ArrayList;
3+
4+
public class Program {
5+
6+
public static void main(String[] args) {
7+
LotteryRow row = new LotteryRow();
8+
ArrayList<Integer> lotteryNumbers = row.numbers();
9+
10+
System.out.println("Lottery numbers:");
11+
for (Integer number : lotteryNumbers) {
12+
System.out.print(number + " ");
13+
}
14+
System.out.println("");
15+
}
16+
}
17+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
import fi.helsinki.cs.tmc.edutestutils.Points;
3+
import static org.junit.Assert.*;
4+
import org.junit.Test;
5+
import java.util.ArrayList;
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
@Points("12-08")
10+
public class LotteryRowTest {
11+
12+
public ArrayList<Integer> test() {
13+
LotteryRow row;
14+
ArrayList<Integer> numbers;
15+
16+
try {
17+
row = new LotteryRow();
18+
numbers = row.numbers();
19+
} catch (Throwable t) {
20+
fail("Something went wrong when creating a new LotteryRow object! More information: " + t);
21+
return new ArrayList<>();
22+
}
23+
24+
assertEquals("The total number of returned lottery numbers should be 7!", 7, numbers.size());
25+
26+
Set<Integer> remainingNumbers = new HashSet<>();
27+
for (int i = 1; i <= 40; i++) {
28+
remainingNumbers.add(i);
29+
}
30+
31+
Set<Integer> drawnNumbers = new HashSet<>();
32+
for (int i : numbers) {
33+
assertTrue("Each lottery number should be in the range 1-40. However, you returned: " + i,
34+
(i >= 1 && i <= 40));
35+
assertTrue("The method 'containsNumber' returns the value false even though the number was in the list of drawn numbers: " + i,
36+
row.containsNumber(i));
37+
assertTrue("LotteryRow contains the same number multiple times: " + i,
38+
drawnNumbers.add(i));
39+
remainingNumbers.remove(i);
40+
}
41+
42+
for (int i : remainingNumbers) {
43+
assertFalse("The method 'containsNumber' returns the value true even though the number is not included in the list of drawn numbers: " + i,
44+
row.containsNumber(i));
45+
}
46+
47+
return numbers;
48+
}
49+
50+
@Test
51+
public void testOneRandomization() {
52+
test();
53+
}
54+
55+
@Test
56+
public void testRandomizeNumbersRandomizesNewNumbers() {
57+
String error = "When the following code was executed:\n LotteryRow lotteryRow = new LotteryRow();\nSystem.out.println(lotteryRow.numbers());\nlotteryRow.randomizeNumbers();\nSystem.out.println(lotteryRow.numbers());\n";
58+
LotteryRow row;
59+
ArrayList<Integer> numbers;
60+
try {
61+
row = new LotteryRow();
62+
numbers = row.numbers();
63+
} catch (Throwable t) {
64+
fail("Something went wrong when creating a new LotteryRow object! More information: " + t);
65+
return;
66+
}
67+
String numbersString = numbers.toString();
68+
assertEquals("The total number of returned lottery numbers should be 7!", 7, numbers.size());
69+
row.randomizeNumbers();
70+
assertTrue(error + "The total number of returned lottery numbers should be 7. Now it was " + row.numbers().size(), 7 == row.numbers().size());
71+
assertFalse(error + "Your program drew the same numbers again. Quite unlikely!", numbersString.equals(row.numbers().toString()));
72+
}
73+
74+
@Test
75+
public void testMultipleRandomizations() {
76+
int[] arr = new int[41];
77+
for (int i = 0; i < 200; i++) {
78+
for (int x : test()) {
79+
arr[x]++;
80+
}
81+
}
82+
83+
int howMany = 0;
84+
for (int i = 1; i <= 40; i++) {
85+
if (arr[i] > 0) {
86+
howMany++;
87+
}
88+
}
89+
90+
assertEquals("200 lottery rows generated only " + howMany
91+
+ " different numbers! Not very random!", 40, howMany);
92+
}
93+
}
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
LotteryRow.class
2+
Program.class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
D:\00Codes\Java\mooc-java-programming-ii\part12-Part12_08.Lottery\src\main\java\Program.java
2+
D:\00Codes\Java\mooc-java-programming-ii\part12-Part12_08.Lottery\src\main\java\LotteryRow.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LotteryRowTest.class

0 commit comments

Comments
 (0)