Ask Question
28 November, 02:04

Write a class named Averager containing: An instance variable named sum of type integer, initialized to 0. An instance variable named count of type integer, initialized to 0. A method named getSum that returns the value of sum. A method named add that accepts an integer parameter. The value of sum is increased by the value of the parameter and the value of count is incremented by one. A method named getCount that accepts no parameters. getCount returns the value of the count instance variable, that is, the number of values added to sum. A method named getAverage that accepts no parameters. getAverage returns the average of the values added to sum. The value returned should be a value of type double (and therefore you must cast the instance variables to double prior to performing the division).

+4
Answers (1)
  1. 28 November, 02:20
    0
    The answer to this question can be given as:

    Class definition:

    public class Averager / /define class Average.

    {

    private int sum = 0;

    //define variable sum.

    private int count = 0;

    //define variable count.

    public int getSum () / /define function getSum ().

    {

    return sum; / /return value.

    }

    public void add (int x)

    //define function add ().

    {

    sum = sum + x; / /calculate sum

    count=count+1; / /increase value of count.

    }

    public int add (int x)

    //define function add ().

    {

    return count; / /return value.

    }

    public double getAverage () / /define function getAverage ().

    {

    return (double) sum/count; / /return value

    }

    }

    Explanation:

    The above class definition can be described as:

    In the above class definition first we define a class that is " Averager". In this class we define two integer variable that is "sum and count" and assign a value that is 0. Then we define a functions : First we define getSum () function in this function we does not pass any value and the return type of this function is int that will return an integer value. Then we define add () function we use this function two times but both functions have different from each other. In first time implementation, we define this function and the return type of this function is void which means it does not return any value. In this function, we calculate the sum value and increase the value of the count variable by 1. In second time implementation, we define this function and the return type of this function is int which means it will return a value. In this function, we will return the count variable value. At the last, we define a getAverage () that calculates the average of the added values in the sum variable and returns its value.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a class named Averager containing: An instance variable named sum of type integer, initialized to 0. An instance variable named count ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers