Advertisement
korobushk

Untitled

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