Skip to content

Commit

Permalink
added java mooc 2
Browse files Browse the repository at this point in the history
  • Loading branch information
awstn committed Jan 16, 2024
1 parent 276056a commit 7a720a4
Show file tree
Hide file tree
Showing 1,215 changed files with 57,583 additions and 0 deletions.
2 changes: 2 additions & 0 deletions part08-Part08_01.Cubes/.tmcproject.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
sandbox_image: eu.gcr.io/moocfi-public/tmc-sandbox-java
73 changes: 73 additions & 0 deletions part08-Part08_01.Cubes/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>Part08_01.Cubes</artifactId>
<name>Part08_01.Cubes</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>
20 changes: 20 additions & 0 deletions part08-Part08_01.Cubes/src/main/java/Cubes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

import java.util.Scanner;

public class Cubes {

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

while (true){
String input = scanner.nextLine();
if (input.equals("end")){
break;
}
int numInput = Integer.valueOf(input);
int cube = numInput * numInput * numInput;
System.out.println(cube);
}

}
}
91 changes: 91 additions & 0 deletions part08-Part08_01.Cubes/src/test/java/CubestTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

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.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import org.junit.*;
import static org.junit.Assert.*;

@Points("08-01")
public class CubestTest {

@Rule
public MockStdio io = new MockStdio();

@Test
public void test() {
String[][] inputs = {{"8", "3", "123", "end"}, {"9", "end"}, {"16", "32", "end"}};

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

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

List<Integer> expected = new ArrayList<>();
String in = "";
for (int i = 0; i < input.length; i++) {
try {
int number = Integer.valueOf(input[i]);
expected.add(number * number * number);
} catch (Exception e) {

}

in += input[i] + "\n";
}

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

assertTrue("When the input is:\n" + in + "\nyou are not printing anything!", out.length() > 0);

String[] prints = takePrints(out);
for (String print : prints) {
int number = -1;
try {
number = Integer.valueOf(print);
} catch (NumberFormatException e) {
continue;
}

if (!expected.contains(number)) {
fail("Input:\n" + in + "\nWas not expecting \"" + number + "\" to be printed, but it was.\nThe output was:\n" + out);
}

expected.remove(Integer.valueOf(number));
}

if (!expected.isEmpty()) {
for (Integer expectedNumber : expected) {
fail("Input:\n" + in + "\n\n Expected number: \"" + expectedNumber + "\", the output was: \"" + out + "\"\n");
}

}
}

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. Be sure to use the nextLine() method to read input!");
} catch (Throwable e) {
fail("public static void main(String[] args) method of the " + kl + " class has disappeared "
+ "or something else unexpected occurred, more information: " + e);
}
}

private static String[] takePrints(String str) {
return str.split("\\s+");
}
}
Binary file added part08-Part08_01.Cubes/target/classes/Cubes.class
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cubes.class
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/home/austin/NetBeansProjects/mooc-java-programming-ii/part08-Part08_01.Cubes/src/main/java/Cubes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CubestTest.class
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/home/austin/NetBeansProjects/mooc-java-programming-ii/part08-Part08_01.Cubes/src/test/java/CubestTest.java
Empty file.
Empty file.
Binary file not shown.
2 changes: 2 additions & 0 deletions part08-Part08_02.AverageOfPositiveNumbers/.tmcproject.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
sandbox_image: eu.gcr.io/moocfi-public/tmc-sandbox-java
73 changes: 73 additions & 0 deletions part08-Part08_02.AverageOfPositiveNumbers/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>Part08_02.AverageOfPositiveNumbers</artifactId>
<name>Part08_02.AverageOfPositiveNumbers</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,40 @@
import java.util.ArrayList;
import java.util.Scanner;

public class AverageOfPositiveNumbers {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<>();

int sum = 0;

while (true){
int input = Integer.valueOf(scanner.nextLine());
if (input == 0){
break;
}

if (input > 0){
numbers.add(input);
}
}

for (int i : numbers){
sum += i;
}

if (numbers.isEmpty()){
System.out.println("Cannot calculate the average");
} else {
System.out.println((1.0 * sum / numbers.size()));
}





}


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

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 org.junit.*;
import static org.junit.Assert.*;

@Points("08-02")
public class AverageOfPositiveNumbersTest {

@Rule
public MockStdio io = new MockStdio();

@Test(timeout = 1000)
public void test1() {
test("0\n", "nnot", "0", "1", "-1");
}

@Test(timeout = 1000)
public void test2() {
test("1\n2\n0\n", "1.5", "0");
}

@Test(timeout = 1000)
public void test3() {
test("-1\n3\n0\n", "3.0", "1");
}

@Test(timeout = 1000)
public void test4() {
test("1\n1\n1\n0\n", "1.0", "0.3", "0.7");
}

public void test(String input, String expected, String... notExpected) {

int oldOut = io.getSysOut().length();
io.setSysIn(input);
callMain(AverageOfPositiveNumbers.class);
String out = io.getSysOut().substring(oldOut);

assertTrue("When input was:\n" + input + ", the expected out put was:\n" + expected + "\nOutput was not found.", out.contains(expected));
for (String unexpected : notExpected) {
assertFalse("When input was:\n" + input + ", output shouldn't contain:\n" + unexpected + "", out.contains(unexpected));
}
}

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 (Throwable e) {
fail("Something unexpected happened. The public static void main(String[] args) method of '" + kl + "' class has disappeared \n"
+ "or your program crashed because of an exception. More info: " + e);
}
}

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AverageOfPositiveNumbers.class
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/home/austin/NetBeansProjects/mooc-java-programming-ii/part08-Part08_02.AverageOfPositiveNumbers/src/main/java/AverageOfPositiveNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AverageOfPositiveNumbersTest.class
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/home/austin/NetBeansProjects/mooc-java-programming-ii/part08-Part08_02.AverageOfPositiveNumbers/src/test/java/AverageOfPositiveNumbersTest.java
Empty file.
Empty file.
Binary file not shown.
2 changes: 2 additions & 0 deletions part08-Part08_03.LiquidContainers/.tmcproject.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
sandbox_image: eu.gcr.io/moocfi-public/tmc-sandbox-java
Loading

0 comments on commit 7a720a4

Please sign in to comment.