Advertisement
korobushk

statistics.java

Mar 23rd, 2020
2,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. public class Statistics {
  2.  
  3. private int count;
  4. private int sum;
  5. private int evens;
  6. private int odds;
  7.  
  8. public Statistics() {
  9.  
  10. }
  11.  
  12. public void addNumber(int number) {
  13. if (number % 2 == 0) {
  14. this.evens = number + this.evens;
  15. } else {
  16. this.odds = number + this.odds;
  17. }
  18.  
  19. number = this.sum + number;
  20. this.sum = number;
  21. this.count++;
  22. }
  23.  
  24. public int sum() {
  25.  
  26. return this.sum;
  27. }
  28.  
  29. public int even() {
  30. return this.evens;
  31.  
  32. // write code here
  33. }
  34.  
  35. public int odd() {
  36.  
  37. return this.odds;
  38. }
  39.  
  40. public int getCount() {
  41. return this.count++; // write code here
  42. }
  43.  
  44. public double average() {
  45. if (this.count == 0) {
  46. return 0;
  47. } else {
  48. return (double) this.sum / this.count;
  49. }
  50.  
  51. }
  52. }
  53.  
  54.  
  55.  
  56. import java.util.Scanner;
  57.  
  58. public class MainProgram {
  59.  
  60. public static void main(String[] args) {
  61. Scanner scanner = new Scanner(System.in);
  62. Statistics statistics = new Statistics();
  63. {
  64. System.out.println("Enter numbers: ");
  65. while (true) {
  66.  
  67. int input = Integer.valueOf(scanner.nextLine());
  68. if (input == -1) {
  69. break;
  70. }
  71. statistics.addNumber(input);
  72.  
  73.  
  74. }
  75.  
  76. System.out.println("Sum: " + statistics.sum());
  77. System.out.println("Sum of even numbers: " + statistics.even());
  78. System.out.println("Sum of odd numbers: " + statistics.odd());
  79. // System.out.println("Sum of odd numbers: " + odd);
  80.  
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement