Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9fd8885

Browse files
Add files via upload
0 parents commit 9fd8885

File tree

70 files changed

+2438
-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.

70 files changed

+2438
-0
lines changed

‎part03-Part03_01.ThirdElement/pom.xml

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>Part03_01.ThirdElement</artifactId>
6+
<name>Part03_01.ThirdElement</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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
import java.util.ArrayList;
3+
import java.util.Scanner;
4+
5+
public class ThirdElement {
6+
7+
public static void main(String[] args) {
8+
Scanner scanner = new Scanner(System.in);
9+
10+
ArrayList<String> list = new ArrayList<>();
11+
while (true) {
12+
String input = scanner.nextLine();
13+
if (input.equals("")) {
14+
break;
15+
}
16+
17+
list.add(input);
18+
}
19+
20+
System.out.println(list.get(2));
21+
}
22+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.NoSuchElementException;
7+
import org.junit.*;
8+
import static org.junit.Assert.*;
9+
10+
@Points("03-01")
11+
public class ThirdElementTest {
12+
13+
@Rule
14+
public MockStdio io = new MockStdio();
15+
16+
@Test
17+
public void test() {
18+
String[][] input = {{"Tom", "Emma", "Alex", "Mary", "", "Alex"}, {"Emma", "Alex", "Mary", "", "Mary"}};
19+
20+
for (int i = 0; i < input.length; i++) {
21+
check(input[i]);
22+
}
23+
}
24+
25+
private void check(String... strings) {
26+
int oldOut = io.getSysOut().length();
27+
28+
String in = "";
29+
for (int i = 0; i < strings.length - 1; i++) {
30+
in += strings[i] + "\n";
31+
}
32+
33+
io.setSysIn(in);
34+
callMain(ThirdElement.class);
35+
String out = io.getSysOut().substring(oldOut);
36+
37+
assertTrue("you're not printing anything!", out.length() > 0);
38+
39+
String ans = getLastWord(out);
40+
String expectedAns = strings[strings.length - 1];
41+
42+
for (int i = 0; i < strings.length - 1; i++) {
43+
String name = strings[i];
44+
if (name.equals(expectedAns)) {
45+
continue;
46+
}
47+
48+
if (name.equals("")) {
49+
continue;
50+
}
51+
52+
if (out.contains(name)) {
53+
fail("Input:\n" + in + "\n the following output was not expected \"" + name + "\", but it got printed.\nThe output was:\n" + out);
54+
}
55+
}
56+
57+
String virheIlm = "Input:\n" + in + "\n\n Expected output: \"" + expectedAns + "\", you printed: \"" + ans + "\"\n";
58+
assertEquals(virheIlm, expectedAns, ans);
59+
}
60+
61+
private void callMain(Class kl) {
62+
try {
63+
kl = ReflectionUtils.newInstanceOfClass(kl);
64+
String[] t = null;
65+
String x[] = new String[0];
66+
Method m = ReflectionUtils.requireMethod(kl, "main", x.getClass());
67+
ReflectionUtils.invokeMethod(Void.TYPE, m, null, (Object) x);
68+
} catch (NoSuchElementException e) {
69+
fail("Your program tried to read too much input. Remember to use nextLine() method to read!");
70+
} catch (Throwable e) {
71+
fail("Something unexpected happened. The public static void main(String[] args) method of '" + kl + "' class has disappeared \n"
72+
+ "or something unexpected happened. More info: " + e);
73+
}
74+
}
75+
76+
private static String getLastWord(String inputStr) {
77+
String[] parts = inputStr.split("\\s+");
78+
return parts[parts.length - 1];
79+
}
80+
}
1.13 KB
Binary file not shown.
3.97 KB
Binary file not shown.
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>Part03_02.SecondPlusThird</artifactId>
6+
<name>Part03_02.SecondPlusThird</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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
import java.util.ArrayList;
3+
import java.util.Scanner;
4+
5+
public class SecondPlusThird {
6+
7+
public static void main(String[] args) {
8+
Scanner scanner = new Scanner(System.in);
9+
10+
ArrayList<Integer> numbers = new ArrayList<>();
11+
while (true) {
12+
int number = Integer.valueOf(scanner.nextLine());
13+
if (number == 0) {
14+
break;
15+
}
16+
17+
numbers.add(number);
18+
}
19+
20+
System.out.println(numbers.get(1) + numbers.get(2));
21+
}
22+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.NoSuchElementException;
7+
import org.junit.*;
8+
import static org.junit.Assert.*;
9+
10+
@Points("03-02")
11+
public class SecondPlusThirdTest {
12+
13+
@Rule
14+
public MockStdio io = new MockStdio();
15+
16+
@Test
17+
public void test() {
18+
String[][] input = {{"1", "3", "5", "7", "0", "8"}, {"2", "3", "4", "0", "7"}};
19+
20+
for (int i = 0; i < input.length; i++) {
21+
check(input[i]);
22+
}
23+
}
24+
25+
private void check(String... strs) {
26+
int oldOut = io.getSysOut().length();
27+
28+
String in = "";
29+
for (int i = 0; i < strs.length - 1; i++) {
30+
in += strs[i] + "\n";
31+
}
32+
33+
io.setSysIn(in);
34+
callMain(SecondPlusThird.class);
35+
String out = io.getSysOut().substring(oldOut);
36+
37+
assertTrue("you're not printing anything!", out.length() > 0);
38+
39+
String result = getLast(out);
40+
String expectedResult = strs[strs.length - 1];
41+
42+
for (int i = 0; i < strs.length - 1; i++) {
43+
String num = strs[i];
44+
if (num.equals(expectedResult)) {
45+
continue;
46+
}
47+
48+
if (num.equals("")) {
49+
continue;
50+
}
51+
52+
if (out.contains(num)) {
53+
fail("Input:\n" + in + "\nThe output was not expected to contain \"" + num + "\".\nThe output was:\n" + out);
54+
}
55+
}
56+
57+
String errorMsg = "Input:\n" + in + "\n\n Expected output: \"" + expectedResult + "\", the output was: \"" + result + "\"\n";
58+
assertEquals(errorMsg, expectedResult, result);
59+
}
60+
61+
private void callMain(Class kl) {
62+
try {
63+
kl = ReflectionUtils.newInstanceOfClass(kl);
64+
String[] t = null;
65+
String x[] = new String[0];
66+
Method m = ReflectionUtils.requireMethod(kl, "main", x.getClass());
67+
ReflectionUtils.invokeMethod(Void.TYPE, m, null, (Object) x);
68+
} catch (NoSuchElementException e) {
69+
fail("Your program tried to read too much input. Remember to use nextLine() method to read!");
70+
} catch (Throwable e) {
71+
fail("Something unexpected happened. The public static void main(String[] args) method of '" + kl + "' class has disappeared \n"
72+
+ "or something unexpected happened. More info: " + e);
73+
}
74+
}
75+
76+
private static String getLast(String inputStr) {
77+
String[] pieces = inputStr.split("\\s+");
78+
return pieces[pieces.length - 1];
79+
}
80+
}
1.22 KB
Binary file not shown.
3.98 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /