1

I have some java code that is supposed to be in the form of classes, take input as numbers, get the average, sum, and count of the numbers entered, and it seems to work for the most part except when actually trying to input values from the main method. How can I adjust this so that the program is able to accept the values?

public class AverageCalculator {
    private int sumOfNumbers ;
    private int countOfNumbers ;

    AverageCalculator() { // no arg constructor
        sumOfNumbers = 0;
        countOfNumbers = 0;
    }

    void add(int newNum) { // adds a number to the average calculator
        this.sumOfNumbers = newNum + sumOfNumbers;
        countOfNumbers++;
    }

    int getSum() { // returns the sum of all the numbers added to average calculator

        return sumOfNumbers;
    }

    int getCount() {// returns the number of numbers added to the average calculator
        return countOfNumbers;
    }

    double getAverage() {// returns the average of all numbers added to average calculator
        double average = this.sumOfNumbers / this.countOfNumbers;
        return average;
    }
}

class AverageCalculatorMain {
    public static void main(String[] args) {
        AverageCalculator average = new AverageCalculator(4); //with one value
        AverageCalculator average = new AverageCalculator(3,4,5)//with three values
        System.out.println("The average is " + average.getAverage() + " the sum is " + average.getSum()
                + " the count of numbers is " + average.getCount());
    }
}
Bingoned
  • 45
  • 3
  • Does this answer your question? [What is "String args\[\]"? parameter in main method Java](https://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java) – mkrieger1 Oct 31 '20 at 17:22
  • Why did you not try to use the `add` method? – mkrieger1 Oct 31 '20 at 17:24
  • Commenting out the average method so that division by zero is not an issue shows that even with the add method, it doesn't seem to change the value of countOfNumbers. – Bingoned Oct 31 '20 at 17:31

1 Answers1

1

You haven't defined a constructor with arguments.

Example constructor assuming you get only pass type int as arguments:

    AverageCalculator(int... args) {
        for(int i:args){
            sumOfNumbers+=i;
            countOfNumbers++;
        }
    }

Also, using these 2 lines in the no-arg constructor is redundant:

sumOfNumbers = 0;
countOfNumbers = 0;

Simply initialize them at the point of defining the variables.

Check out the code after the above mentioned fixes:

public class AverageCalculator {
    private int sumOfNumbers=0;
    private int countOfNumbers=0;

    AverageCalculator() { // no arg constructor
    }

    AverageCalculator(int... args) {
        for(int i:args){
            sumOfNumbers+=i;
            countOfNumbers++;
        }
    }

    void add(int newNum) { // adds a number to the average calculator
        this.sumOfNumbers = newNum + sumOfNumbers;
        countOfNumbers++;
    }

    int getSum() { // returns the sum of all the numbers added to average calculator

        return sumOfNumbers;
    }

    int getCount() {// returns the number of numbers added to the average calculator
        return countOfNumbers;
    }

    double getAverage() {// returns the average of all numbers added to average calculator
        double average = this.sumOfNumbers / this.countOfNumbers;
        return average;
    }
}

class AverageCalculatorMain {
    public static void main(String[] args) {
        AverageCalculator average = new AverageCalculator(4); //with one value
        System.out.println("The average is " + average.getAverage() + " the sum is " + average.getSum()
                + " the count of numbers is " + average.getCount());
        AverageCalculator average2 = new AverageCalculator(3, 4, 5);//with three values
        System.out.println("The average is " + average2.getAverage() + " the sum is " + average2.getSum()
                + " the count of numbers is " + average2.getCount());
    }
}