Object-Oriented Programming with Java, part I – Exercise 79

This exercise uses the class NumberStatistics to calculate the sums of multiple objects.

My Code

Main


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
        NumberStatistics stats = new NumberStatistics();
        NumberStatistics even = new NumberStatistics();
        NumberStatistics odd = new NumberStatistics();
        int input = 0;
        System.out.println("Type numbers: ");
        input = Integer.parseInt(reader.nextLine());
        while (input != -1) {
            stats.addNumber(input);
            if (input%2 == 0){
                even.addNumber(input);
            } else {
                odd.addNumber(input);
            }
            input = Integer.parseInt(reader.nextLine());

        }

        System.out.println("sum: " + stats.sum());
        System.out.println("sum of even: " + even.sum());
        System.out.println("sum of odd: " + odd.sum());
    }
}

NumberStatistics


public class NumberStatistics {

    private int amountOfNumbers;
    private int sum;

    public NumberStatistics() {
        // initialize here the object variable amountOfNumbers
        this.amountOfNumbers = 0;
        this.sum = 0;
    }

    public void addNumber(int number) {
        // code here
        this.amountOfNumbers++;
        this.sum = this.sum + number;
    }

    public int amountOfNumbers() {
        // code here
        return this.amountOfNumbers;
    }

    public int sum() {
        // code here
        return this.sum;
    }

    public double average() {
        // code here
        if (this.amountOfNumbers == 0) {
            return 0;
        } else {
            return (double) this.sum / this.amountOfNumbers;
        }
    }
}

Model Code

Main

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 sum = new NumberStatistics();

        NumberStatistics even = new NumberStatistics();

        NumberStatistics odd = new NumberStatistics();

 

        System.out.println("Type numbers: ");

 

        while (true) {

            int number = Integer.parseInt(reader.nextLine());

            if (number == -1) {

                break;

            }

 

            sum.addNumber(number);

 

            if (number % 2 == 0) {

                even.addNumber(number);

            } else {

                odd.addNumber(number);

            }

        }

 

        System.out.println("sum: " + sum.sum());

        System.out.println("sum of even: " + even.sum());

        System.out.println("sum of odd: " + odd.sum());

    }

}

 

NumberStatistics

public class NumberStatistics {

 

    private int amountOfNumbers;

    private int sum;

 

    public NumberStatistics() {

        this.amountOfNumbers = 0;

        this.sum = 0;

    }

 

    public void addNumber(int luku) {

        this.amountOfNumbers++;

        this.sum += luku;

    }

 

    public int amountOfNumbers() {

        return this.amountOfNumbers;

    }

 

    public int sum() {

        return this.sum;

    }

 

    public double average() {

        if (this.amountOfNumbers == 0) {

            return 0;

        }

 

        return 1.0 * this.sum / this.amountOfNumbers;

    }

}

Comments

At first I thought my code was awful but on second glance it is only bad. I used a if/else statement where an if would have been enough and the model while loop in the main seems a lot nicer than mine.

Leave a comment