-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberStatistics.java
98 lines (72 loc) · 2.66 KB
/
NumberStatistics.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class NumberStatistics {
private int amountOfNumbers;
private int sum;
// private double average;
// private int sumOfOdd;
// private int sumOfEven;
public NumberStatistics() {
this.amountOfNumbers = 0;
this.sum = 0;
// this.average = 0;
}
public void addNumber(int number) {
//if (number != -1) {
// if (number % 2 == 0) {
// this.sumOfEven += number;
// } else {
// this.sumOfOdd += number;
// }
this.sum += number;
this.amountOfNumbers++;
//}
}
public int amountOfNumbers() {
return this.amountOfNumbers;
}
public int sum() {
return this.sum;
}
public double average() {
// this.average = (this.sum / (double)this.amountOfNumbers);
if (this.amountOfNumbers == 0) {
return 0;
} else {
return ((double)this.sum / (double)this.amountOfNumbers);
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// Write test code here
// Remember to remove all the extra code when doing assignments 79.3 and 79.4
// Define three NumberStatistics objects in your program:
// The first is used to track the sum of all given numbers.
// The second takes care of even numbers and the third the odd numbers.
// The tests does not work if you do not create the objects in the correct order
NumberStatistics sumOfAllNumbers = new NumberStatistics();
NumberStatistics sumOfAllEvens = new NumberStatistics();
NumberStatistics sumOfAllOdds = new NumberStatistics();
System.out.println("Type numbers ");
int userInput = 0;
while (userInput != -1) {
//System.out.println("Type an integer: ");
userInput = Integer.parseInt(reader.nextLine());
if (userInput == -1)
{
break;
}
if (userInput % 2 == 0) {
sumOfAllEvens.addNumber(userInput);
} else {
sumOfAllOdds.addNumber(userInput);
}
sumOfAllNumbers.addNumber(userInput);
//System.out.println();
}
System.out.println("sum: " + sumOfAllNumbers.sum());
System.out.println("sum of even: " + sumOfAllEvens.sum());
System.out.println("sum of odd: " + sumOfAllOdds.sum());
}
}