-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statistics2.java
57 lines (45 loc) · 1.5 KB
/
Statistics2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class Statistics {
private int count;
private int sum;
public Statistics(){
this.count = 0;
this.sum = 0;
}
public void addNumber(int number){
this.count = this.count + 1;
this.sum = this.sum + number;
}
public int getCount(){
return count;
}
public int sum(){
return sum;
}
public double average(){
if(count == 0){
return 0.0;
}
return sum * 1.0 / count;
}
}
_____________________________________________
import java.util.Scanner;
public class MainProgram {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// you can write test code here
// however, remove all unnecessary code when doing the final parts of the exercise
// In order for the tests to work, the objects must be created in the
// correct order in the main program. First the object that tracks the total
// sum, secondly the object that tracks the sum of even numbers,
// and lastly the one that tracks the sum of odd numbers!
Statistics statistics = new Statistics();
statistics.addNumber(3);
statistics.addNumber(5);
statistics.addNumber(1);
statistics.addNumber(2);
System.out.println("Count: " + statistics.getCount());
System.out.println("Sum: " + statistics.sum());
System.out.println("Average: " + statistics.average());
}
}