Skip to content

Commit b085d11

Browse files
author
aghaei
committed
Final Commit
0 parents  commit b085d11

File tree

494 files changed

+54845
-0
lines changed

Some content is hidden

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

494 files changed

+54845
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
sandbox_image: eu.gcr.io/moocfi-public/tmc-sandbox-java
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>Part08_01.Cubes</artifactId>
6+
<name>Part08_01.Cubes</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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
import java.util.Scanner;
3+
4+
public class Cubes {
5+
6+
public static void main(String[] args) {
7+
Scanner scanner = new Scanner(System.in);
8+
9+
while (true) {
10+
System.out.println("Enter ino");
11+
String userInput = scanner.nextLine();
12+
13+
if (userInput.equals("end")) {
14+
15+
break;
16+
17+
}
18+
int result = 0;
19+
20+
int convertedInput = Integer.valueOf(userInput);
21+
22+
result = (convertedInput * convertedInput) * convertedInput;
23+
24+
System.out.println(result);
25+
26+
}
27+
28+
}
29+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
import fi.helsinki.cs.tmc.edutestutils.MockStdio;
3+
import fi.helsinki.cs.tmc.edutestutils.Points;
4+
import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils;
5+
import java.lang.reflect.Method;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.NoSuchElementException;
9+
import org.junit.*;
10+
import static org.junit.Assert.*;
11+
12+
@Points("08-01")
13+
public class CubestTest {
14+
15+
@Rule
16+
public MockStdio io = new MockStdio();
17+
18+
@Test
19+
public void test() {
20+
String[][] inputs = {{"8", "3", "123", "end"}, {"9", "end"}, {"16", "32", "end"}};
21+
22+
for (int i = 0; i < inputs.length; i++) {
23+
check(inputs[i]);
24+
}
25+
}
26+
27+
private void check(String... input) {
28+
int oldOut = io.getSysOut().length();
29+
30+
List<Integer> expected = new ArrayList<>();
31+
String in = "";
32+
for (int i = 0; i < input.length; i++) {
33+
try {
34+
int number = Integer.valueOf(input[i]);
35+
expected.add(number * number * number);
36+
} catch (Exception e) {
37+
38+
}
39+
40+
in += input[i] + "\n";
41+
}
42+
43+
io.setSysIn(in);
44+
callMain(Cubes.class);
45+
String out = io.getSysOut().substring(oldOut);
46+
47+
assertTrue("When the input is:\n" + in + "\nyou are not printing anything!", out.length() > 0);
48+
49+
String[] prints = takePrints(out);
50+
for (String print : prints) {
51+
int number = -1;
52+
try {
53+
number = Integer.valueOf(print);
54+
} catch (NumberFormatException e) {
55+
continue;
56+
}
57+
58+
if (!expected.contains(number)) {
59+
fail("Input:\n" + in + "\nWas not expecting \"" + number + "\" to be printed, but it was.\nThe output was:\n" + out);
60+
}
61+
62+
expected.remove(Integer.valueOf(number));
63+
}
64+
65+
if (!expected.isEmpty()) {
66+
for (Integer expectedNumber : expected) {
67+
fail("Input:\n" + in + "\n\n Expected number: \"" + expectedNumber + "\", the output was: \"" + out + "\"\n");
68+
}
69+
70+
}
71+
}
72+
73+
private void callMain(Class kl) {
74+
try {
75+
kl = ReflectionUtils.newInstanceOfClass(kl);
76+
String[] t = null;
77+
String x[] = new String[0];
78+
Method m = ReflectionUtils.requireMethod(kl, "main", x.getClass());
79+
ReflectionUtils.invokeMethod(Void.TYPE, m, null, (Object) x);
80+
} catch (NoSuchElementException e) {
81+
fail("Your program tried to read too much input. Be sure to use the nextLine() method to read input!");
82+
} catch (Throwable e) {
83+
fail("public static void main(String[] args) method of the " + kl + " class has disappeared "
84+
+ "or something else unexpected occurred, more information: " + e);
85+
}
86+
}
87+
88+
private static String[] takePrints(String str) {
89+
return str.split("\\s+");
90+
}
91+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
sandbox_image: eu.gcr.io/moocfi-public/tmc-sandbox-java
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>Part08_02.AverageOfPositiveNumbers</artifactId>
6+
<name>Part08_02.AverageOfPositiveNumbers</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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
import java.util.Scanner;
3+
4+
public class AverageOfPositiveNumbers {
5+
6+
public static void main(String[] args) {
7+
Scanner scanner = new Scanner(System.in);
8+
9+
int sum = 0;
10+
int count = 0;
11+
12+
while (true) {
13+
14+
System.out.println("Enter input");
15+
int userInput = Integer.valueOf(scanner.nextLine());
16+
17+
if (userInput == 0) {
18+
19+
if (sum > 0) {
20+
System.out.println(average(sum, count));
21+
22+
} else {
23+
System.out.println("Cannot calculate the average");
24+
}
25+
26+
break;
27+
}
28+
29+
if (userInput > 0) {
30+
count++;
31+
sum += userInput;
32+
33+
}
34+
35+
}
36+
37+
}
38+
39+
public static double average(int sum, int count) {
40+
41+
double result = 0;
42+
43+
result = 1.0 * sum / count;
44+
45+
return result;
46+
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
import fi.helsinki.cs.tmc.edutestutils.MockStdio;
3+
import fi.helsinki.cs.tmc.edutestutils.Points;
4+
import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils;
5+
import java.lang.reflect.Method;
6+
import org.junit.*;
7+
import static org.junit.Assert.*;
8+
9+
@Points("08-02")
10+
public class AverageOfPositiveNumbersTest {
11+
12+
@Rule
13+
public MockStdio io = new MockStdio();
14+
15+
@Test(timeout = 1000)
16+
public void test1() {
17+
test("0\n", "nnot", "0", "1", "-1");
18+
}
19+
20+
@Test(timeout = 1000)
21+
public void test2() {
22+
test("1\n2\n0\n", "1.5", "0");
23+
}
24+
25+
@Test(timeout = 1000)
26+
public void test3() {
27+
test("-1\n3\n0\n", "3.0", "1");
28+
}
29+
30+
@Test(timeout = 1000)
31+
public void test4() {
32+
test("1\n1\n1\n0\n", "1.0", "0.3", "0.7");
33+
}
34+
35+
public void test(String input, String expected, String... notExpected) {
36+
37+
int oldOut = io.getSysOut().length();
38+
io.setSysIn(input);
39+
callMain(AverageOfPositiveNumbers.class);
40+
String out = io.getSysOut().substring(oldOut);
41+
42+
assertTrue("When input was:\n" + input + ", the expected out put was:\n" + expected + "\nOutput was not found.", out.contains(expected));
43+
for (String unexpected : notExpected) {
44+
assertFalse("When input was:\n" + input + ", output shouldn't contain:\n" + unexpected + "", out.contains(unexpected));
45+
}
46+
}
47+
48+
private void callMain(Class kl) {
49+
try {
50+
kl = ReflectionUtils.newInstanceOfClass(kl);
51+
String[] t = null;
52+
String x[] = new String[0];
53+
Method m = ReflectionUtils.requireMethod(kl, "main", x.getClass());
54+
ReflectionUtils.invokeMethod(Void.TYPE, m, null, (Object) x);
55+
} catch (Throwable e) {
56+
fail("Something unexpected happened. The public static void main(String[] args) method of '" + kl + "' class has disappeared \n"
57+
+ "or your program crashed because of an exception. More info: " + e);
58+
}
59+
}
60+
61+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
sandbox_image: eu.gcr.io/moocfi-public/tmc-sandbox-java

0 commit comments

Comments
 (0)