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 afee7b6

Browse files
Add files via upload
1 parent cb05c6c commit afee7b6

File tree

94 files changed

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

94 files changed

+3396
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project>
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>tkt</groupId>
5+
<artifactId>Part04_15.Statistics</artifactId>
6+
<name>Part04_15.Statistics</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+
30+
<dependency>
31+
<groupId>org.powermock</groupId>
32+
<artifactId>powermock-classloading-objenesis</artifactId>
33+
<version>2.0.2</version>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.powermock</groupId>
38+
<artifactId>powermock-api-easymock</artifactId>
39+
<version>2.0.2</version>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.powermock</groupId>
44+
<artifactId>powermock-module-junit4-rule</artifactId>
45+
<version>2.0.2</version>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<version>3.8.0</version>
56+
</plugin>
57+
<plugin>
58+
<groupId>org.codehaus.mojo</groupId>
59+
<artifactId>exec-maven-plugin</artifactId>
60+
<version>1.6.0</version>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
65+
<repositories>
66+
<repository>
67+
<id>tmc</id>
68+
<name>TMC repo</name>
69+
<url>https://maven.mooc.fi/releases</url>
70+
<releases>
71+
<enabled>true</enabled>
72+
</releases>
73+
<snapshots>
74+
<enabled>true</enabled>
75+
</snapshots>
76+
</repository>
77+
</repositories>
78+
79+
<pluginRepositories>
80+
<pluginRepository>
81+
<id>tmc</id>
82+
<name>TMC repo</name>
83+
<url>https://maven.mooc.fi/releases</url>
84+
<releases>
85+
<enabled>true</enabled>
86+
</releases>
87+
<snapshots>
88+
<enabled>true</enabled>
89+
</snapshots>
90+
</pluginRepository>
91+
</pluginRepositories>
92+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import java.util.Scanner;
3+
4+
public class MainProgram {
5+
6+
public static void main(String[] args) {
7+
Scanner scanner = new Scanner(System.in);
8+
// you can write test code here
9+
// however, remove all unnecessary code when doing the final parts of the exercise
10+
11+
// In order for the tests to work, the objects must be created in the
12+
// correct order in the main program. First the object that tracks the total
13+
// sum, secondly the object that tracks the sum of even numbers,
14+
// and lastly the one that tracks the sum of odd numbers!
15+
Statistics statistics = new Statistics();
16+
Statistics evenNumber = new Statistics();
17+
Statistics oddNumber = new Statistics();
18+
System.out.println("Enter numbers:");
19+
while (true) {
20+
int userInput = scanner.nextInt();
21+
if (userInput == -1) {
22+
break;
23+
} else if (userInput % 2 == 0) {
24+
evenNumber.addNumber(userInput);
25+
} else if (userInput % 2 == 1) {
26+
oddNumber.addNumber(userInput);
27+
}
28+
statistics.addNumber(userInput);
29+
}
30+
System.out.println("Sum: " + statistics.sum());
31+
System.out.println("Sum of even numbers: " + evenNumber.sum());
32+
System.out.println("Sum of odd numbers: " + oddNumber.sum());
33+
}
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
public class Statistics {
3+
private int count;
4+
private int sum;
5+
6+
public Statistics () {
7+
count = 0;
8+
sum = 0;
9+
}
10+
11+
public void addNumber(int number) {
12+
sum += number;
13+
count++;
14+
}
15+
16+
public int getCount() {
17+
return count;
18+
}
19+
20+
public int sum() {
21+
return sum;
22+
}
23+
24+
public double average() {
25+
if (count == 0) {
26+
return 0.0;
27+
} else {
28+
return (double)sum / count;
29+
}
30+
}
31+
}

0 commit comments

Comments
(0)

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