C++: Called statistician initialized, it can be given a sequence of double numbe
ID: 3675070 • Letter: C
Question
C++:
Called statistician initialized, it can be given a sequence of double numbers. Each number in the sequence is function called next_number delcare a statistician called s
sequence of numbers 1.1, -2.4, 0.8 as shown here:
statistician s;
s.next_number(1.1); s.next_number(-2.4); s.next_number(0.8);
Include member functions that will provide the length of the sequence, the last number of the sequence, the sumof all numbers in the sequence, the arithmetic mean of the numbres (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 lnegth and sum functions 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. But the other member functions all have a precondition requiring that the sequence is non-empty. You should also provide a member function that erases the sequence (so that the statistician can start with a new sequence). Notes: Do not start the entire sequence (because you don't know how long this sequence will be). Instead, just store the necessary information about the sequence: What is the sequence length? What is the sum of the numbers in the sequence? What are the last, smallest, and largest numbers? Each of these pieces of information can be stared in a private member variable that is updated whenever next_number is activated.
Explanation / Answer
import java.util.* ;
class Statistician
{
public int length = 0 ;
public double last = 0 ;
public double first = 0 ;
public double smallest = 0 ;
public double biggest = 0 ;
public double sum = 0 ;
int length ( )
{
return length ;
}
double first ( )
{
return first ;
}
double last ( )
{
return last ;
}
double mean ( )
{
return sum / ((double)length) ;
}
double biggest ( )
{
return biggest ;
}
double smallest ( )
{
return smallest ;
}
void nextNumber ( double lastNumber )
{
/*
System.out.print ( "Please enter a number --> " ) ;
double lastNumber = in.nextDouble ;
*/
length++ ;
if ( lastNumber < smallest ) smallest = lastNumber ;
if ( lastNumber > biggest ) biggest = lastNumber ;
if ( length == 1 ) first = lastNumber ;
last = lastNumber ;
sum = sum + lastNumber ;
}
void print ( )
{
System.out.println ( first+ last+ smallest+ biggest+ mean ( )+ length ) ;
}
Statistician ( ) { } ;
}
class hw_1_Statistician
{
public static void main ( String[] args )
{
Scanner in = new Scanner ( System.in ) ;
Statistician s = new Statistician ( ) ;
s.nextNumber ( 1.1 ) ;
s.nextNumber ( -2.4 ) ;
s.nextNumber ( 0.8 ) ;
s.nextNumber ( 0.16666 ) ;
System.out.println ( s.last ( ) + " " + s.first ( ) + " " + s.biggest ( ) + s.smallest ( ) + s.mean ( ) ) ;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.