136k views
1 vote
Write a full class definition for a class named Averager, and containing the following members:______An data member named sum of type integer.An data member named count of type integer.A constructor with no parameters. Theconstructor initializes the data members sum and the data member count to 0.A function named getSum that accepts noparameters and returns an integer. getSumreturns the value of sum.A function named add that accepts an integer parameter and returns no value. add increases the value of sum by the value of theparameter, and increments the value of countby one.A function named getCount that accepts noparameters and returns an integer. getCountreturns the value of the count data member, that is, the number of values added to sum.A function named getAverage that accepts noparameters and returns a double. getAveragereturns the average of the values added to sum. The value returned should be a value of type double (and therefore you must cast the data members to double prior to performing the division).

User Terryl
by
5.2k points

2 Answers

4 votes

Answer:

#ifndef AVERAGER

#define AVERAGER

class Averager{

public:

int sum;

int count;

Averager(){

sum = 0;

count = 0;

}

int getSum(){

return sum;

}

void add(int n){

sum += n;

count++;

}

int getCount(){

return count;

}

double getAverage(){

return (double)sum/count;

}

};

#endif

Step-by-step explanation:

User Novice C
by
4.8k points
7 votes

Answer:

  1. public class Averager {
  2. private int sum;
  3. private int count;
  4. public Averager(int sum, int count) {
  5. this.sum = 0;
  6. this.count = 0;
  7. }
  8. public int getSum(){
  9. return sum;
  10. }
  11. public void add( int num){
  12. this.sum+=num;
  13. this.count++;
  14. }
  15. public int getCount(){
  16. return this.count;
  17. }
  18. public double getAverage(){
  19. double ave = (int)this.sum/this.count;
  20. return ave;
  21. }
  22. }

Step-by-step explanation:

  • Lines 1-3 contains the class declaration and the member (data fields)
  • Lines 4-7 is the constructor that initializes the fields to 0
  • Lines 8-10 is the method that returns the value of sum getSum()
  • lines 11-14 iss the method add() that adds a number to the member field sum and increases count by 1
  • lines 15 - 17 is the method that returns total count getCount()
  • Lines 18-21 is the method getAverage() That computes the average and returns a double representing the average values

User Chbu
by
5.3k points