Skip to content

Commit

Permalink
fetch new exercies
Browse files Browse the repository at this point in the history
  • Loading branch information
truongvantuan committed Jan 30, 2021
1 parent 397ec7c commit d341506
Show file tree
Hide file tree
Showing 129 changed files with 5,300 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@
/part02-Part02_06.AreWeThereYet/target/
/part02-Part02_07.OnlyPositives/target/
/part02-Part02_08.NumberOfNumbers/target/
/part02-Part02_09.NumberOfNegativeNumbers/target/
/part02-Part02_09.NumberOfNegativeNumbers/target/
/part02-Part02_10.SumOfNumbers/target/
/part02-Part02_11.NumberAndSumOfNumbers/target/
/part02-Part02_12.AverageOfNumbers/target/
1 change: 1 addition & 0 deletions part03-Part03_01.ThirdElement/.tmcproject.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
force_new_sandbox: true
73 changes: 73 additions & 0 deletions part03-Part03_01.ThirdElement/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>Part03_01.ThirdElement</artifactId>
<name>Part03_01.ThirdElement</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>
22 changes: 22 additions & 0 deletions part03-Part03_01.ThirdElement/src/main/java/ThirdElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

import java.util.ArrayList;
import java.util.Scanner;

public class ThirdElement {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

ArrayList<String> list = new ArrayList<>();
while (true) {
String input = scanner.nextLine();
if (input.equals("")) {
break;
}

list.add(input);
}

System.out.println(lista.get(0));
}
}
80 changes: 80 additions & 0 deletions part03-Part03_01.ThirdElement/src/test/java/ThirdElementTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

import fi.helsinki.cs.tmc.edutestutils.MockStdio;
import fi.helsinki.cs.tmc.edutestutils.Points;
import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils;
import java.lang.reflect.Method;
import java.util.NoSuchElementException;
import org.junit.*;
import static org.junit.Assert.*;

@Points("03-01")
public class ThirdElementTest {

@Rule
public MockStdio io = new MockStdio();

@Test
public void test() {
String[][] input = {{"Tom", "Emma", "Alex", "Mary", "", "Alex"}, {"Emma", "Alex", "Mary", "", "Mary"}};

for (int i = 0; i < input.length; i++) {
check(input[i]);
}
}

private void check(String... strings) {
int oldOut = io.getSysOut().length();

String in = "";
for (int i = 0; i < strings.length - 1; i++) {
in += strings[i] + "\n";
}

io.setSysIn(in);
callMain(ThirdElement.class);
String out = io.getSysOut().substring(oldOut);

assertTrue("you're not printing anything!", out.length() > 0);

String ans = getLastWord(out);
String expectedAns = strings[strings.length - 1];

for (int i = 0; i < strings.length - 1; i++) {
String name = strings[i];
if (name.equals(expectedAns)) {
continue;
}

if (name.equals("")) {
continue;
}

if (out.contains(name)) {
fail("Input:\n" + in + "\n the following output was not expected \"" + name + "\", but it got printed.\nThe output was:\n" + out);
}
}

String virheIlm = "Input:\n" + in + "\n\n Expected output: \"" + expectedAns + "\", you printed: \"" + ans + "\"\n";
assertEquals(virheIlm, expectedAns, ans);
}

private void callMain(Class kl) {
try {
kl = ReflectionUtils.newInstanceOfClass(kl);
String[] t = null;
String x[] = new String[0];
Method m = ReflectionUtils.requireMethod(kl, "main", x.getClass());
ReflectionUtils.invokeMethod(Void.TYPE, m, null, (Object) x);
} catch (NoSuchElementException e) {
fail("Your program tried to read too much input. Remember to use nextLine() method to read!");
} catch (Throwable e) {
fail("Something unexpected happened. The public static void main(String[] args) method of '" + kl + "' class has disappeared \n"
+ "or something unexpected happened. More info: " + e);
}
}

private static String getLastWord(String inputStr) {
String[] parts = inputStr.split("\\s+");
return parts[parts.length - 1];
}
}
1 change: 1 addition & 0 deletions part03-Part03_02.SecondPlusThird/.tmcproject.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
force_new_sandbox: true
73 changes: 73 additions & 0 deletions part03-Part03_02.SecondPlusThird/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>Part03_02.SecondPlusThird</artifactId>
<name>Part03_02.SecondPlusThird</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,22 @@

import java.util.ArrayList;
import java.util.Scanner;

public class SecondPlusThird {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

ArrayList<Integer> numbers = new ArrayList<>();
while (true) {
int number = Integer.valueOf(scanner.nextLine());
if (number == 0) {
break;
}

numbers.add(number);
}

System.out.println(luvut.get(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

import fi.helsinki.cs.tmc.edutestutils.MockStdio;
import fi.helsinki.cs.tmc.edutestutils.Points;
import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils;
import java.lang.reflect.Method;
import java.util.NoSuchElementException;
import org.junit.*;
import static org.junit.Assert.*;

@Points("03-02")
public class SecondPlusThirdTest {

@Rule
public MockStdio io = new MockStdio();

@Test
public void test() {
String[][] input = {{"1", "3", "5", "7", "0", "8"}, {"2", "3", "4", "0", "7"}};

for (int i = 0; i < input.length; i++) {
check(input[i]);
}
}

private void check(String... strs) {
int oldOut = io.getSysOut().length();

String in = "";
for (int i = 0; i < strs.length - 1; i++) {
in += strs[i] + "\n";
}

io.setSysIn(in);
callMain(SecondPlusThird.class);
String out = io.getSysOut().substring(oldOut);

assertTrue("you're not printing anything!", out.length() > 0);

String result = getLast(out);
String expectedResult = strs[strs.length - 1];

for (int i = 0; i < strs.length - 1; i++) {
String num = strs[i];
if (num.equals(expectedResult)) {
continue;
}

if (num.equals("")) {
continue;
}

if (out.contains(num)) {
fail("Input:\n" + in + "\nThe output was not expected to contain \"" + num + "\".\nThe output was:\n" + out);
}
}

String errorMsg = "Input:\n" + in + "\n\n Expected output: \"" + expectedResult + "\", the output was: \"" + result + "\"\n";
assertEquals(errorMsg, expectedResult, result);
}

private void callMain(Class kl) {
try {
kl = ReflectionUtils.newInstanceOfClass(kl);
String[] t = null;
String x[] = new String[0];
Method m = ReflectionUtils.requireMethod(kl, "main", x.getClass());
ReflectionUtils.invokeMethod(Void.TYPE, m, null, (Object) x);
} catch (NoSuchElementException e) {
fail("Your program tried to read too much input. Remember to use nextLine() method to read!");
} catch (Throwable e) {
fail("Something unexpected happened. The public static void main(String[] args) method of '" + kl + "' class has disappeared \n"
+ "or something unexpected happened. More info: " + e);
}
}

private static String getLast(String inputStr) {
String[] pieces = inputStr.split("\\s+");
return pieces[pieces.length - 1];
}
}
1 change: 1 addition & 0 deletions part03-Part03_03.IndexOutOfBoundsException/.tmcproject.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
force_new_sandbox: true
Loading

0 comments on commit d341506

Please sign in to comment.