Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code for c++. Specify, design, and implement a classes called Statistician . Aft

ID: 3664170 • Letter: C

Question

Code for c++.

Specify, design, and implement a classes called Statistician. After a statistician is initizlized, it can given a sequence of double numbers. Each number in the sequence is given to the statistician by activating a method called nextNumber. For example, we can declare a statistician called s and then give it the sequence of numbers 1.1, -2.4, 0.8 as shown here:

         Statistician s = new Statistician();

         s.nextNumber(1.1);

         s.nextNumber(-2.4);

         s.nextNumber(0.8);

   After a sequence has been given to a statistician, there are various methods to obtain information about the sequence. Include methods that will provide the length of the sequence, the last number of the sequence, the sum of all the numbers in the sequence, the arithmetic mean of the numbers (i.e., the sum of the numbers divided by the length of the sequence), the smallest number in the sequence, and the largest number in the sequence.

Notice that the length and sum methods can be called at any time, even if there are no numbers in the sequence. In this case of an "empty" sequence, both length and sum will be zero. The other methods should return Double.NaN if they are called for an empty sequence:

   Notes: Do not try to store the entire sequence. Instead, just store the necessary information about the sequence: What is the sequence length, what is the sum of the numbers in the sequence, and what are the last, smallest and largest numbers? Each of these pieces of information can be stored in a private instance variable that is updated whenever nextNumber is activated.

Overload the + operator to allow you to add two statisticians from the project. If s1 and s2 are two statisticians, then the result of s1+s2 should be a new statistician that behaves as if it had all of the numbers of s1 followed by all of the numbers of s2.

I have this code so far:

public class StatisticianDriver
{public static void main(String[] args)
{Statistician s=new Statistician();
System.out.println("Before anything entered");
System.out.println("mean= "+s.getMean());
System.out.println("Largest number= "+s.getBiggest());
System.out.println("Smallest number= "+s.getSmallest());
System.out.println("Last number entered= "+s.getLast());
System.out.println("Number of numbers entered= "+s.getLength());
s.nextNumber (1.1);
s.nextNumber(-2.4);
s.nextNumber (0.8);
System.out.println(" After numbers entered");
System.out.println("sum= "+s.getSum());
System.out.println("mean= "+s.getMean());
System.out.println("Largest number= "+s.getBiggest());
System.out.println("Smallest number= "+s.getSmallest());
System.out.println("Last number entered= "+s.getLast());
System.out.println("Number of numbers entered= "+s.getLength());
}
}

----------------------------------------------

public class Statistician
{private double sum;
private double small;
private double big;
private double last;
private int count;
public Statistician()
{sum=0;
count=0;   
}

public double getLength()
{return count;
}

public double getLast()
{return last;
}
public double getSum()
{return sum;
}
public double getMean()
{return sum/count;
}
public double getSmallest()
{return small;
}
public double getBiggest()
{return big;
}
public void nextNumber(double n)
{count++;
if(count==1)
{big=n;
small=n;
}
else
{if(n>big)
big=n;
if(n<small)
small=n;
}
sum+=n;
last=n;
}
}

Explanation / Answer

Note : Code is in C++ , in Question code is given in Java where as the in the begin it was mention “Code for c++. ”

#include <iostream>
#include <cmath>

using namespace std;
class Statistician
{
   private :
       double sum;
        double small;
        double big;
        double last;
        int count;
    public:
       // Constructer to initializing SUM and LENGTH
       Statistician()
            {
               sum=0;
               count=0;     
            }
      
       // Mutators & Accessors mothods for setting and retrieve values of private memebers
       double getLength(){
      
           return count;  
       }
      
       double getLast(){
      
           if (count > 0)   // checking for empty sequence
               return last;
           else
               return NAN;
       }
      
       double getSum() {   return sum;    }
      
       double getMean()    {  
           if (count > 0)   // checking for empty sequence
               return sum/count;
           else
               return NAN;
        }
      
       double getSmallest(){
           if (count > 0)   // checking for empty sequence
               return small;
           else
               return NAN;

       }
      
       double getBiggest(){
           if (count > 0)   // checking for empty sequence
               return big;
           else
               return NAN;
       }
      
      
        void setLength(double lCount)   {   count = lCount;    }
      
       void setLast(double lLast)    {   last = lLast;    }
       
       void setSum(double lSum)        {   sum = lSum;    }
      
       void setSmallest(double lSmall){   small = lSmall;    }
      
       void setBiggest( double lBig) {   big = lBig;   }
      
       void nextNumber(double num)
        {
           count++;
           if(count==1)
            {
               big = num;
               small = num;
            }
           else
            {
               if(num > big)
                   big = num;
                if(num < small)
                   small = num;
            }
           sum += num;
           last = num;
        }
};

// Overload the + operator to allow you to add two statisticians

Statistician operator+(Statistician &s1,Statistician &s2 ){

   Statistician *statSum = new Statistician();
  
   statSum->setSum(s1.getSum()+s2.getSum());
  
   statSum->setLength(s1.getLength()+s2.getLength());
  
   statSum->setLast(s2.getLast()); // Assuming 2nd operator has last as last number
  
   if (s1.getSmallest() < s2.getSmallest())  
       statSum->setSmallest(s1.getSmallest());
   else
       statSum->setSmallest(s2.getSmallest());

  
   if (s1.getBiggest() > s2.getBiggest())  
   {
       cout<<" big"<<endl;
       statSum->setBiggest(s1.getBiggest());
   }
   else
   {
   cout<<" big 2"<<endl;
  
       statSum->setBiggest(s2.getBiggest());
   }
   return *statSum;
}


// Main Driver function

int main(){
  
   Statistician stat1,stat2,stat3;
  
   cout<<"Before anything entered"<<endl;
   cout<<"Sum= "<<stat1.getSum()<<endl;
   cout<<"mean= "<<stat1.getMean()<<endl;
   cout<<"Largest number= "<<stat1.getBiggest()<<endl;
   cout<<"Smallest number= "<<stat1.getSmallest()<<endl;
   cout<<"Last number entered= "<<stat1.getLast()<<endl;
   cout<<"Number of numbers entered= "<<stat1.getLength()<<endl;

   stat1.nextNumber (1.1);
   stat1.nextNumber(-2.4);
   stat1.nextNumber (0.8);

   cout<<endl<<"After numbers entered to STAT1"<<endl;
   cout<<"Sum= "<<stat1.getSum()<<endl;
   cout<<"mean= "<<stat1.getMean()<<endl;
   cout<<"Largest number= "<<stat1.getBiggest()<<endl;
   cout<<"Smallest number= "<<stat1.getSmallest()<<endl;
   cout<<"Last number entered= "<<stat1.getLast()<<endl;
   cout<<"Number of numbers entered= "<<stat1.getLength()<<endl;

   stat2.nextNumber (3.1);
   stat2.nextNumber(5.4);
   stat2.nextNumber (6.8);
  
   cout<<endl<<"After numbers entered STAT2"<<endl;
   cout<<"Sum= "<<stat2.getSum()<<endl;
   cout<<"mean= "<<stat2.getMean()<<endl;
   cout<<"Largest number= "<<stat2.getBiggest()<<endl;
   cout<<"Smallest number= "<<stat2.getSmallest()<<endl;
   cout<<"Last number entered= "<<stat2.getLast()<<endl;
   cout<<"Number of numbers entered= "<<stat2.getLength()<<endl;

  
   // Using Overload + operator
   stat3 = stat1 + stat2;
  
   cout<<endl<<" Stat1 + stat2 numbers entered (Operater overloading on +)"<<endl;
   cout<<"Sum= "<<stat3.getSum()<<endl;
   cout<<"mean= "<<stat3.getMean()<<endl;
   cout<<"Largest number= "<<stat3.getBiggest()<<endl;
   cout<<"Smallest number= "<<stat3.getSmallest()<<endl;
   cout<<"Last number entered= "<<stat3.getLast()<<endl;
   cout<<"Number of numbers entered= "<<stat3.getLength()<<endl;

}