import java.util.*;
public class Statistician2 extends java.lang.Object
{
/**
* Initialize a new Statistician.
* @param - none
* <dt><b>Postcondition:</b><dd>
* This Statistician is newly initialized and has not yet been given any
* numbers.
**/
private double currentNum;
private double totalNum;
private double sum;
private double max;
private double min;
private double mean;
public Statistician2( )
{
currentNum = 0.0;
totalNum = 0.0;
sum = 0.0;
max = 0.0;
min = 0.0;
mean = 0.0;
}
/**
* Add the numbers of another Statistician to this Statistician.
* @param <CODE>addend</CODE>
* a Statistician whose numbers will be added to this Statistician
* <dt><b>Precondition:</b><dd>
* The parameter, <CODE>addend</CODE>, is not null.
* <dt><b>Postcondition:</b><dd>
* The numbers from <CODE>addend</CODE> have been added to this
* Statistician. After the operation, this Statistician acts as if
* if was given all of its numbers and also given all of the numbers
* from the addend.
* @exception NullPointerException
* Indicates that <CODE>addend</CODE> is null.
**/
public void addAll(Statistician2 addend)
{
if (addend == null)
throw new NullPointerException("Statistician2 is null");
this.totalNum = this.totalNum + addend.totalNum;
this.sum = this.sum + addend.sum;
if(addend.min<this.min)
this.min=addend.min;
if(addend.max>this.max)
this.max=addend.max;
if(addend.mean>this.mean)
this.mean=(this.mean+addend.mean/2);
}
/**
* Clear this Statistician.
* @param - none
* <dt><b>Postcondition:</b><dd>
* This Statistician is reinitialized as if it has never been given any
* numbers.
**/
public void clear( )
{
currentNum = 0.0;
totalNum = 0.0;
sum = 0.0;
max = 0.0;
min = 0.0;
mean = 0.0;
}
/**
* Compare this <CODE>Statistician</CODE> to another object for equality.
* @param <CODE>obj</CODE>
* an object with which this <CODE>Statistician</CODE> will be compared
* @return
* A return value of <CODE>true</CODE> indicates that
* <CODE>obj</CODE> refers to a
* <CODE>Statistican</CODE> object with the same length, sum, mean,
* minimum and maximum as this
* <CODE>Statistician</CODE>. Otherwise the return value is
* <CODE>false</CODE>.
* <dt><b>Note:</b><dt>
* If <CODE>obj</CODE> is null or does not refer to a
* <CODE>Statistician</CODE> object, then the answer is
* <CODE>false</CODE>.
**/
public boolean equals(Object obj)
{
if (obj instanceof Statistician2)
{
Statistician2 candidate = (Statistician2) obj;
return
(candidate.currentNum==currentNum)
&&
(candidate.totalNum==totalNum)
&&
(candidate.sum==sum)
&&
(candidate.max==max)
&&
(candidate.min==min)
&&
(candidate.mean==mean);
}
else
return false;
}
/**
* Determine how many numbers have been given to this Statistician.
* @param - none
* @return
* the count of how many numbers have been given to this Statistician
* since it was initialized or reinitialized.
* <dt><b>Note:</b><dd>
* Giving a Statistician more than
* <CODE>Integer.MAX_VALUE</CODE> numbers, will
* cause failure with an arithmetic overflow.
**/
public double length( )
{
return totalNum;
}
/**
* Determine the largest number that has been given
* to this Statistician.
* @param - none
* @return
* the largest number that has been given to this
* Statistician
* since it was initialized or reinitialized.
* <dt><b>Note:</b><dd>
* If <CODE>length()</CODE> is zero, then the answer from this method
* is <CODE>Double.NaN</CODE>.
**/
public double maximum( )
{
return max;
}
/**
* Determine the arithmetic average of all the numbers that have been given
* to this Statistician.
* @param - none
* @return
* the arithmetic mean of all the number that have been given to this
* Statistician
* since it was initialized or reinitialized.
* <dt><b>Note:</b><dd>
* If this Statistician has been given more than
* <CODE>Integer.MAX_VALUE</CODE> numbers, then this method fails
* because of arithmetic overflow.
* If <CODE>length()</CODE> is zero, then the answer from this method
* is <CODE>Double.NaN</CODE>.
* If <CODE>sum()</CODE> exceeds the bounds of double numbers, then the
* answer from this method may be
* <CODE>Double.POSITIVE_INFINITY</CODE> or
* <CODE>Double.NEGATIVE_INFINITY</CODE>.
**/
public double mean( )
{
if (totalNum<=0)
return Double.NaN;
else
return sum/totalNum;
}
/**
* Determine the smallest number that has been given
* to this Statistician.
* @param - none
* @return
* the smallest number that has been given to this
* Statistician
* since it was initialized or reinitialized.
* <dt><b>Note:</b><dd>
* If <CODE>length()</CODE> is zero, then the answer from this method
* is <CODE>Double.NaN</CODE>.
**/
public double minimum( )
{
return min;
}
/**
* Give a new number to this Statistician.
* @param <CODE>number</CODE>
* the new number that is being given the this Statistician
* <dt><b>Postcondition:</b><dd>
* The specified number has been given to this Statistician and
* it will be included in any subsequent statistics.
**/
public void next(double num)
{
currentNum = num;
totalNum = totalNum + 1;
sum = sum + num;
mean = sum / totalNum;
}
/**
* Determine the sum of all the numbers that have been given to this
* Statistician.
* @param - none
* @return
* the sum of all the number that have been given to this
* Statistician
* since it was initialized or reinitialized.
* <dt><b>Note:</b><dd>
* If the sum exceeds the bounds of double numbers, then the answer
* from this method may be
* <CODE>Double.POSITIVE_INFINITY</CODE> or
* <CODE>Double.NEGATIVE_INFINITY</CODE>.
**/
public double sum( )
{
return sum;
}
/**
* Create a new Statistician that behaves as if it was given all the
* numbers from two other bags.
* @param <CODE>s1</CODE>
* the first of two Statisticians
* @param <CODE>s2</CODE>
* the second of two Statisticians
* <dt><b>Precondition:</b><dd>
* Neither s1 nor s2 is null.
* @return
* a new Statistician that acts as if it was given all the numbers from
* s1 and s2.
* @exception NullPointerException.
* Indicates that one of the arguments is null.
**/
public static Statistician2 union(Statistician2 s1, Statistician2 s2)
{
if ((s1 == null)&& (s2 == null))
throw new NullPointerException("s1 or s2 is null");
return null;
// Statistician2 s3 = new Statistician2();
// s3.totalNum = s1.totalNum + s2.totalNum;
// s3.sum = s1.sum + s2.sum;
// s3.mean = (s1.mean + s2.mean/2);
//
// return s3;
}
}
_______________________________________________________________
import java.util.*;
public class StatisticianDemo2 {
public static void main(
String[] args)
{
// Statistician2 s1 = new Statistician2();
Scanner scannerObject = new Scanner(System.in);
System.out.println("Enter three numbers for s1 including decimals: ");
double num1 = scannerObject.nextDouble();
double num2 = scannerObject.nextDouble();
double num3 = scannerObject.nextDouble();
// Statistician2 s2 = new Statistician2();
// Statistician2 s3 = new Statistician2();
//double totalNum = scannerObject.nextDouble();
// System.out.println("The current numbers are: " +totalNum);
}
}
[ February 02, 2008: Message edited by: Teri Fisher ]