简体   繁体   中英

Using mutator and accesor methods in Java and creating objects to calculate the average of numbers

My assignment is to create a class to count, sum, and average a series of numbers. The specifications for the class AverageCalculator are:

instance variable of type int for the running sum that have been given to the AverageCalculator
instance variable of type int for the count of numbers that have been given to the AverageCalculator
no argument constructor: AverageCalculator()
a mutator method to add a number to the AverageCalculator: void add(int newNum)
an accessor method to return the sum of all the numbers added to the AverageCalculator: int getSum()
an accessor method to return the count, or number, of numbers added to the AverageCalculator: int getCount()
an accessor method to return the average of all numbers added to the AverageCalculator - note that the instance variables are int, and the return type is double, if no numbers have been added to the object, return 0: double getAverage()

Write a class, AverageCalculatorMain that contains a main method and does the following:

  • create a AverageCalculator object, add one number ot the AverageCalculator, print sum, count, and average
  • create another AverageCalculator object. Add three numbers, print sum, count, and average

This is what I have so far:


public class AverageCalculator {
    private int sum;
    private int count;
    
    public AverageCalculator() {
        sum = 0;
        count = 0;
    }
    public void add(int newNum) {
        this.sum = this.sum + newNum;
        count++;
    }
    
    int getSum() {
        return sum;
    }
    
    int getCount() {
        return count;
    }
    
    double getAverage() {
        return sum / count;
    }
    
}

This is what I have for the main method:

public class AverageCalculatorMain {

    public static void main(String[] args) {
        
        AverageCalculator average = new AverageCalculator(90);
        
        System.out.println("The sum is " + average.getSum() + "\nThe count is " + average.getCount() + "\nThe average is " + average.getAverage());
        

    }

}

However, when I try to create the new object (AverageCalculator average = new AverageCalculator(90);), there is an error saying it is undefined. How can I create this object and pass its value(s) successfully?

The problem is that you are trying to create an object using parametrised constructor

 AverageCalculator average = new AverageCalculator(90);

But you don't have parametrised constructor in your AverageCalculator class.

So change it to

 AverageCalculator average = new AverageCalculator();

OR

create a parametrised constructor.

public AverageCalculator(int count) {
    this.count = count;
}

Or as per your need, you can create with two parameters ie sum and count

EDIT:

You can have setter method

public void setCount(int sum) {
  this.sum = sum;
}

And after object creation, you can call this method to set the value.

Define two constructors in your class,

the no arg constuctor and the one arg constructor

public AverageCalculatorMain(){}
public AverageCalculatorMain(int x){
 sum = x;
}

If you intend to pass an unknown number of arguments at run time then you should use varargs constructor

public AverageCalculatorMain(int ...x){
     
    }

or better still pass a collection as an argument to your constructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM