Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Test Fails but I don't know why, please help

  1. #1
    Junior Member cutekill0's Avatar
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Test Fails but I don't know why, please help

    Hi everyone, I was doing this assignment and I though I had everything right until I had to run the Test program, here's my code:

     
    public class Statistician implements Cloneable {  
    	/****************************
    	 * class invariant:
    	 * - resetting the statistician resets all values here
    	 * - these values are computed since the most recent reset
    	 * - sumOfValues contains the sum of all values entered (or 0)
    	 * - sumOfValues may have a value signifying arithmetic errors
    	 *        Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY
    	 * - count contains the number of values (or 0) but may contain
    	 *        Integer.MAX_VALUE if too many values are entered
    	 * - smallestValue contains smallest value entered (or 0)
    	 * - largestValue contains largest value entered (or 0)
    	 */
    	private double sumOfValues;
    	private int count;
    	private double smallestValue;
    	private double largestValue;
     
    	/**
    	 * Initialize a new Statistician.  
    	 * @param none
    	 * @postcondition
    	 *   This Statistician is newly initialized and has not yet been 
    	 *   given any numbers.
    	 **/   
    	public Statistician( )
    	{
    		count = 0;
    	}        
     
    	/**
    	 * Reset this Statistician. 
    	 * @param none
    	 * @postcondition
    	 *   This Statistician is reinitialized as if it has never been 
    	 *   given any numbers.
    	 **/
    	public void reset( )
    	{
    		count = 0; 
    	}
     
    	/**
    	 * Returns a separate copy of this Statistician that will appear
    	 * to be indistinguishable from the original but separate
    	 * @postcondition
    	 *   The returned Statistician is a separate copy of this Statistician
    	 */
    	public Statistician clone( )
    	{	
    		// student adds clone implementation that returns clone
    		return null;
    	}
     
    	/**
    	 * Give a new number to this Statistician. 
    	 * @param number
    	 *   the new number that is being given to this Statistician
    	 * @postcondition
    	 *   The specified number has been given to this Statistician 
    	 *   and it will be included in any subsequent statistics.
    	 **/
    	public void insert(double number)
    	{
    		if (count == 0)
    		{
    			sumOfValues = number;
    			largestValue = number;
     
    		}
    		else
    		{
    			sumOfValues += number;
    			if (number > largestValue)
    				largestValue = number;
    			if (number < smallestValue)
    				smallestValue = number;
    		}
    		count++;
    	}
     
    	/**
    	 * Compare this Statistician to another object for equality.
    	 * @param obj
    	 *   an object with which this Statistician will be compared
    	 * @return
    	 *   A return value of true indicates that obj refers to a Statistican 
    	 *   object with the same length, sum, mean, largest and smallest as 
    	 *   this Statistician. Otherwise the return value is false.
    	 * Note:
    	 *   If obj is null or does not refer to a Statistician object, 
    	 *   then the answer is false.
    	 **/   
    	public boolean equals(Object obj)
    	{
    		if( obj instanceof Statistician)
    		{
    			Statistician stat = (Statistician) obj;
    			return (stat.count == count && stat.sumOfValues == sumOfValues &&
    				stat.largestValue == largestValue && stat.smallestValue == smallestValue);
    		}
    		return false;
    	} 
     
     
    	/**
    	 * Determine how many numbers have been given to this Statistician.
    	 * @param none
    	 * @return
    	 *   count of how many numbers have been given to this Statistician
    	 *   since it was initialized or reinitialized.
    	 * Note:
    	 *   Giving a Statistician more than Integer.MAX_VALUE numbers, 
    	 *   will cause failure with an arithmetic overflow.
    	 **/ 
    	public int length( )
    	{
     
    		return count;
    	}
     
    	/**
    	 * 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.
    	 * Note:
    	 *   If the sum exceeds the bounds of double numbers, then the answer
    	 *   from this method may be Double.POSITIVE_INFINITY or
    	 *   Double.NEGATIVE_INFINITY.
    	 **/ 
    	public double sum( )
    	{
    		if (count == 0)
    			return 0;
    		return sumOfValues;
     
    	}
     
     
    	/**
    	 * 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.
    	 * Note:
    	 *   If this Statistician has been given more than Integer.MAX_VALUE 
    	 *   numbers, then this method fails because of arithmetic overflow.
    	 *   If length() is zero, then the answer is Double.NaN.
    	 *   If sum() exceeds the bounds of double numbers, then the answer 
    	 *   may be Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY.
    	 **/ 
    	public double mean( )
    	{
    		if(count == 0)
    		{
    			return Double.NaN;
    		}
    		return sumOfValues / count;
    	}
     
     
    	/**
    	 * Determine 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.
    	 * Note:
    	 *   If length() is zero, then the answer is Double.NaN.
    	 **/ 
    	public double smallest( )
    	{
    		if(count == 0)
    		{
    			return Double.NaN;
    		}
    		return smallestValue;
    	}
     
     
    	/**
    	 * Determine 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.
    	 * Note:
    	 *   If length() is zero, then the answer is Double.NaN.
    	 **/ 
    	public double largest( )
    	{
    		if(count == 0)
    		{
    			return Double.NaN;
    		}
    		return largestValue;
    	}
     
     
    	/**
    	 * Add the numbers of another Statistician (addend) to this Statistician.
    	 * @param addend
    	 *   a Statistician whose numbers will be added to this Statistician
    	 * @precondition
    	 *   The parameter, addend, is not null. 
    	 * @postcondition
    	 *   The numbers from addend have been added to this Statistician. 
    	 *   After the operation, this Statistician acts as if it were given 
    	 *   all of its numbers and also given all of the numbers from the 
    	 *   addend.
    	 * @exception NullPointerException
    	 *   Indicates that addend is null.
    	 **/
    	public void add(Statistician addend)
    	{
    		if (addend.count != 0)
    		{
    			if (count == 0)
    			{
    				count = addend.count;
    				sumOfValues = addend.sumOfValues;
    				largestValue = addend.largestValue;
    				smallestValue = addend.smallestValue;
    			}
    			else
    			{
    				count += addend.count;
    				sumOfValues += addend.sumOfValues;
    				if (addend.largestValue > largestValue)
    					largestValue = addend.largestValue;
    				if (addend.smallestValue < smallestValue)
    					smallestValue = addend.smallestValue;
    			}
    		}
    	}   
     
    	/**
    	 * Create a new Statistician that behaves as if it was given all 
    	 * the numbers from this and the other Statistician 
    	 * @param other
    	 *   an existing Statistician
    	 * @precondition
    	 *   Neither this nor the other Statistician is null.
    	 * @return
    	 *   a new Statistician that acts as if it was given all the 
    	 *   numbers from this Statistician and the other Statistician 
    	 * @exception NullPointerException.
    	 *   Indicates that the argument is null.
    	 **/   
    	public Statistician union(Statistician other)
    	{
    		if (other == null)
    		{
    			throw new NullPointerException();
    		}
    		Statistician outcome = new Statistician();
    		outcome.add(other);
    		return outcome;
    	}
     
    }

    here's the Test, I only have 7 failures and I really don't know why:
     
    import org.junit.Test;
    import static org.junit.Assert.*;
     
    /**
     * You must include the Junit4 library to use this test unit.
     *
     * To add it, select the project and then choose Properties... from the File menu.
     * Click on "Java Build Path" in the panel at the right.
     * Select Libraries from the list at the top.
     * Select Add Library... from the right side of the panel.
     * Select JUnit and click on Next>.
     * Change the version to JUnit 4 and click on Finish..
     * Click on OK in the properties panel.
     */
     
     
    public class StatTest {
    	/**
    	 * create Statistician and check whether it's constructed properly
    	 */
    	@Test
    	public void testConstructor() {
    		Statistician s = new Statistician( );
     
    		assertTrue("Sum wrong", s.sum( ) == 0);
    		assertTrue("Length wrong", s.length( ) == 0);
    		assertTrue("Mean wrong", Double.isNaN(s.mean( )));
    		assertTrue("Largest wrong", Double.isNaN(s.largest( )));
    		assertTrue("Smallest wrong", Double.isNaN(s.smallest( )));
    	}
     
    	/**
    	 * try length and insert
    	 */
    	@Test
    	public void one( ) {
    		Statistician s = new Statistician( );
    		s.insert(42);
    		assertTrue("Length should be one", s.length( ) == 1);
    		s.insert(12);
    		s.insert(17);
    		assertTrue("Length should be three", s.length( ) == 3);
    	}
     
     
    	/**
    	 * test reset by adding some things and reseting
    	 */
    	@Test
    	public void reset() {
    		Statistician s = new Statistician( );
     
    		s.insert(42);
    		s.insert(17);
    		s.insert(-5.5);
    		s.insert(0);
     
    		assertTrue("Sum wrong", s.sum( ) == 53.5);
    		assertTrue("Length wrong", s.length( ) == 4);
    		assertEquals("Mean wrong", 13.375, s.mean( ), 0.0001);
    		assertEquals("Largest wrong", 42, s.largest( ), 0.0001);
    		assertEquals("Smallest wrong", -5.5, s.smallest( ), 0.0001);
     
    		s.reset( );	
     
    		assertTrue("Sum wrong after reset", s.sum( ) == 0);
    		assertTrue("Length wrong after reset", s.length( ) == 0);
    		assertTrue("Mean wrong after reset", Double.isNaN(s.mean( )));
    		assertTrue("Largest wrong after reset", Double.isNaN(s.largest( )));
    		assertTrue("Smallest wrong after reset", Double.isNaN(s.smallest( )));
    	}
     
    	/**
    	 * try to find problem of not handling all values being negative correctly
    	 */
    	@Test
    	public void allNegative( ) {
    		Statistician s = new Statistician( );
     
    		s.insert(-42);
    		s.insert(-10);
    		s.insert(-8.15);
     
    		assertEquals("Sum wrong", -60.15, s.sum( ), 0.0001);
    		assertTrue("Length wrong", s.length( ) == 3);
    		assertEquals("Mean wrong", -20.05, s.mean( ), 0.0001);
    		assertEquals("Largest wrong", -8.15, s.largest( ), 0.0001);
    		assertEquals("Smallest wrong", -42, s.smallest( ), 0.0001);
    	}
     
    	/**
    	 * uncover bug of using 0 in initial seting for smallest
    	 */
    	@Test
    	public void smallestZero( ) {
    		Statistician s = new Statistician( );
     
    		s.insert(1);
    		s.insert(0);
    		s.insert(2);
     
    		assertEquals("Smallest wrong", 0, s.smallest( ), 0.0001);
    	}
     
    	/**
    	 * Creates statisticians with single different values
    	 * and compares the statisticians to see if they are different.
    	 * It then adds the 'opposing' values to each and compares to
    	 * see that the statisticians seem to be the same.
    	 */
    	@Test
    	public void compareTwoSmall() {
    		Statistician s = new Statistician( );
    		Statistician t = new Statistician( );
     
    		s.insert(42);
    		t.insert(-736.3);
     
    		assertTrue("t not equal to s", !s.equals(t));
    		assertTrue("s not equal to t", !t.equals(s));
     
    		s.insert(-736.3);
    		t.insert(42);
     
    		assertTrue("t equals to s", s.equals(t));
    		assertTrue("s equals to t", t.equals(s));
    		assertTrue("not equal to null", !s.equals(null));
    	}
     
     
    	/**
    	 * Creates statisticians with several different values
    	 * and compares the statisticians to see if they are the same.
    	 * It then adds an extra value to one and compares to
    	 * see that the statisticians seem to be different.
    	 */
    	@Test
    	public void compareTwoMedium() {
    		Statistician s = new Statistician( );
    		Statistician t = new Statistician( );
     
    		for (int i = -5; i <= 10; i++) {
    			s.insert(i);
    		}
    		for (int i = 10; i >= -5; i--) {
    			t.insert(i);
    		}
    		assertTrue("t equals to s", s.equals(t));
    		assertTrue("s equals to t", t.equals(s));
     
    		s.insert(15);
     
    		assertTrue("t not equal to s", !s.equals(t));
    		assertTrue("s not equal to t", !t.equals(s));
     
    		t.insert(-20);
     
    		assertTrue("t not equal to s", !s.equals(t));
    		assertTrue("s not equal to t", !t.equals(s));		
    	}
     
    	/**
    	 * Creates statisticians with a lot of values and sees
    	 * if that causes problems. But it doesn't try to
    	 * overflow it.
    	 */
    	@Test
    	public void compareALot() {
    		Statistician s = new Statistician( );
    		for (int i=1; i <= 10000; i++) {
    			s.insert(i);
    		}
     
    		assertEquals("Mean too far off", 5000.5, s.mean( ), 0.0000001);
    		assertTrue("Wrong count", s.length( ) == 10000);
    	}
     
    	/**
    	 * Creates two statisticians, adds them, and checks answers
    	 */
    	@Test
    	public void union() {
    		Statistician s = new Statistician( );
    		Statistician t = new Statistician( );
    		Statistician w = s.union(t);
     
    		assertTrue("All empty", (s.length( ) + t.length( ) + w.length( )) == 0);
     
    		for (int i=1; i <= 3; i++) {
    			s.insert(i);
    		}
    		for (int i=5; i <= 7; i++) {
    			t.insert(i);
    		}
     
    		w =  s.union(t);
     
    		assertTrue("Union not empty", w.length( ) == 6);
    		assertTrue("Sums equal", w.sum( ) == (t.sum( ) + s.sum( )));
     
    		double big = s.largest( ) > t.largest( ) ? s.largest( ) : t.largest( );
    		assertEquals("Union has largest large", big, w.largest( ), 0.000001);
     
    		double little = s.smallest( ) < t.smallest( ) ? s.smallest( ) : t.smallest( );
    		assertEquals("Union has smallest small", little, w.smallest( ), 0.000001);
    	}
     
    	/**
    	 * Creates two empty statisticians, unions them, and checks answers
    	 */
    	@Test
    	public void unionEmpty() {
     
    		Statistician s = new Statistician( );
    		Statistician t = new Statistician( );
    		Statistician w = s.union(t);
     
    		assertTrue("All should be empty", (s.length( ) + t.length( ) + w.length( )) == 0);
    		assertTrue("Largest NaN after empty union", Double.isNaN(w.largest( )));
    		assertTrue("Smallest NaN after empty union", Double.isNaN(w.smallest( )));
    	}
     
    	/**
    	 * Creates two empty statisticians, adds them, and checks answers
    	 */
    	@Test
    	public void addEmpty() {
     
    		Statistician s = new Statistician( );
    		Statistician t = new Statistician( );
    		s.add(t);
     
    		assertTrue("Both should be empty", (s.length( ) + t.length( )) == 0);
    		assertTrue("Largest NaN after empty union", Double.isNaN(s.largest( )));
    		assertTrue("Smallest NaN after empty union", Double.isNaN(s.smallest( )));
    	}
     
     
    	/**
    	 * Creates two statisticians, adds them, and checks largest and smallest
    	 */
    	@Test
    	public void unionSmall() {
     
    		Statistician s = new Statistician( );
    		Statistician t = new Statistician( );
    		Statistician w = new Statistician( );
    		double value = 42.0;
     
    		s.insert(value);  // s contains one
     
    		s.add(w);   // add empty to s
    		assertEquals("after adding empty w", value, s.smallest( ), 0.000001);
    		assertEquals("after adding empty w", value, s.largest( ), 0.000001);
     
    		t.add(s);   // add s to empty
    		assertEquals("t smallest after add", value, t.smallest( ), 0.000001);
    		assertEquals("t largest after add", value, t.largest( ), 0.000001);
     
    		t.insert(value*2 + 10);
    		w =  s.union(t);  // union the two
     
    		assertTrue("Union not empty", w.length( ) == 3);
    		assertTrue("Sums equal", w.sum( ) == (t.sum( ) + s.sum( )));
     
    		double big = s.largest( ) > t.largest( ) ? s.largest( ) : t.largest( );
    		assertEquals("Union has largest large", big, w.largest( ), 0.000001);
     
    		double little = s.smallest( ) < t.smallest( ) ? s.smallest( ) : t.smallest( );
    		assertEquals("Union has smallest small", little, w.smallest( ), 0.000001);
    	}
     
    	/**
    	 * Creates two statisticians, adds them, and checks largest and smallest
    	 */
    	@Test
    	public void addExtremes( ) {
     
    		Statistician s = new Statistician( );
    		Statistician t = new Statistician( );
    		s.insert(17.17);
    		s.insert(78.0);
    		t.insert(4.0);
    		t.insert(100.0);
     
    		t.add(s);
    		assertEquals("t smallest after add", 4.0, t.smallest( ), 0.000001);
    		assertEquals("t largest after add", 100.0, t.largest( ), 0.000001);
    	}
     
    	/**
    	 * Looks for bugs in the clone and equals methods
    	 */
    	@Test
    	public void cloneEquals( ) {
    		Statistician a = new Statistician( );
    		Statistician b;
     
    		a.insert(42);
    		a.insert(-1.5);
    		a.insert(0);
    		a.insert(-0.5);
    		b = a.clone( );
    		assertTrue("clone should be equal", a.equals(b));
    		assertTrue("clone should be equal", b.equals(a));
    		assertFalse("clone should be separate object", a == b);
    		b.insert(40);
    		assertTrue("clone should not be equal after insert", !a.equals(b));		
    	}
     
    	/**
    	 * Looks for bug in passing a null pointer to union
    	 */
    	@Test(expected=NullPointerException.class)
    	public void unionException( ) {
    		Statistician a = new Statistician( );
    		Statistician b = null;
     
    		a.union(b);		
    	}
     
    	/**
    	 * Looks for bug in passing a null pointer to add
    	 */
    	@Test(expected=NullPointerException.class)
    	public void addException( ) {
    		Statistician a = new Statistician( );
    		Statistician b = null;
     
    		a.add(b);		
    	}
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Test Fails but I don't know why, please help

    here's the Test, I only have 7 failures and I really don't know why:
    You do not mention which tests fail. This is key to understanding the failures

  3. #3
    Junior Member cutekill0's Avatar
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Test Fails but I don't know why, please help

    I'm very sorry, the tests that are failing are union, allNegative, compareTwoSmall, compareTwoMedium, unionSmall, addExtremes, and cloneEquals, I hope you can help me

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Test Fails but I don't know why, please help

    Quote Originally Posted by cutekill0 View Post
    I'm very sorry, the tests that are failing are union, allNegative, compareTwoSmall, compareTwoMedium, unionSmall, addExtremes, and cloneEquals, I hope you can help me
    Given the number of failures, its probably best to start one at a time. Your union has several assertions - which one fails?

  5. #5
    Junior Member cutekill0's Avatar
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Test Fails but I don't know why, please help

    well the failure in union is at assertTrue("Union not empty", w.length( ) == 6);

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Test Fails but I don't know why, please help

    Quote Originally Posted by cutekill0 View Post
    well the failure in union is at assertTrue("Union not empty", w.length( ) == 6);
    A perfect example of the use of test cases - failure implies the logic is incorrect somewhere (which is indeed the case). Inspect the methods in the statistician class used prior to that assertion. Draw out what happens up to that point to the value you are evaluating in the assertion that fails (the count variable of w, which you expect to be the count of t plus the count of s)

  7. #7
    Junior Member cutekill0's Avatar
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Test Fails but I don't know why, please help

    ok so I fixed some things and I'm passing most of my tests except the union tests
     
    public class Statistician implements Cloneable {  
    	/****************************
    	 * class invariant:
    	 * - resetting the statistician resets all values here
    	 * - these values are computed since the most recent reset
    	 * - sumOfValues contains the sum of all values entered (or 0)
    	 * - sumOfValues may have a value signifying arithmetic errors
    	 *        Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY
    	 * - count contains the number of values (or 0) but may contain
    	 *        Integer.MAX_VALUE if too many values are entered
    	 * - smallestValue contains smallest value entered (or 0)
    	 * - largestValue contains largest value entered (or 0)
    	 */
    	private double sumOfValues;
    	private int count;
    	private double smallestValue;
    	private double largestValue;
     
    	/**
    	 * Initialize a new Statistician.  
    	 * @param none
    	 * @postcondition
    	 *   This Statistician is newly initialized and has not yet been 
    	 *   given any numbers.
    	 **/   
    	public Statistician( )
    	{
    		count = 0;
    		smallestValue = 0;
    		largestValue = 0;
    		sumOfValues = 0;
    	}        
     
    	/**
    	 * Reset this Statistician. 
    	 * @param none
    	 * @postcondition
    	 *   This Statistician is reinitialized as if it has never been 
    	 *   given any numbers.
    	 **/
    	public void reset( )
    	{
    		count = 0; 
    		smallestValue = 0;
    		largestValue = 0;
    		sumOfValues = 0;
    	}
     
    	/**
    	 * Returns a separate copy of this Statistician that will appear
    	 * to be indistinguishable from the original but separate
    	 * @postcondition
    	 *   The returned Statistician is a separate copy of this Statistician
    	 */
    	public Statistician clone( )
    	{	
    		Statistician outcome = null;
    		try
    		{
    			outcome= (Statistician) super.clone();
    		} 
    		catch (CloneNotSupportedException e)
    		{
    			e.printStackTrace();
    		}
     
    		return outcome;
    	}
     
    	/**
    	 * Give a new number to this Statistician. 
    	 * @param number
    	 *   the new number that is being given to this Statistician
    	 * @postcondition
    	 *   The specified number has been given to this Statistician 
    	 *   and it will be included in any subsequent statistics.
    	 **/
    	public void insert(double number)
    	{
    		if (count == 0)
    		{
    			sumOfValues = number;
    			largestValue = number;
    			smallestValue = number;
     
    		}
    		else
    		{
    			sumOfValues += number;
    			if (number > largestValue)
    				largestValue = number;
    			if (number < smallestValue)
    				smallestValue = number;
    		}
    		count++;
    	}
     
    	/**
    	 * Compare this Statistician to another object for equality.
    	 * @param obj
    	 *   an object with which this Statistician will be compared
    	 * @return
    	 *   A return value of true indicates that obj refers to a Statistican 
    	 *   object with the same length, sum, mean, largest and smallest as 
    	 *   this Statistician. Otherwise the return value is false.
    	 * Note:
    	 *   If obj is null or does not refer to a Statistician object, 
    	 *   then the answer is false.
    	 **/   
    	public boolean equals(Object obj)
    	{
    		if( obj instanceof Statistician)
    		{
    			Statistician stat = (Statistician) obj;
    			return (stat.count == count && stat.sumOfValues == sumOfValues &&
    				stat.largestValue == largestValue && stat.smallestValue == smallestValue);
    		}
    		return false;
    	} 
     
     
    	/**
    	 * Determine how many numbers have been given to this Statistician.
    	 * @param none
    	 * @return
    	 *   count of how many numbers have been given to this Statistician
    	 *   since it was initialized or reinitialized.
    	 * Note:
    	 *   Giving a Statistician more than Integer.MAX_VALUE numbers, 
    	 *   will cause failure with an arithmetic overflow.
    	 **/ 
    	public int length( )
    	{
     
    		return count;
    	}
     
    	/**
    	 * 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.
    	 * Note:
    	 *   If the sum exceeds the bounds of double numbers, then the answer
    	 *   from this method may be Double.POSITIVE_INFINITY or
    	 *   Double.NEGATIVE_INFINITY.
    	 **/ 
    	public double sum( )
    	{
    		if (count == 0)
    			return 0;
    		return sumOfValues;
     
    	}
     
     
    	/**
    	 * 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.
    	 * Note:
    	 *   If this Statistician has been given more than Integer.MAX_VALUE 
    	 *   numbers, then this method fails because of arithmetic overflow.
    	 *   If length() is zero, then the answer is Double.NaN.
    	 *   If sum() exceeds the bounds of double numbers, then the answer 
    	 *   may be Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY.
    	 **/ 
    	public double mean( )
    	{
    		if(count == 0)
    		{
    			return Double.NaN;
    		}
    		return sumOfValues / count;
    	}
     
     
    	/**
    	 * Determine 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.
    	 * Note:
    	 *   If length() is zero, then the answer is Double.NaN.
    	 **/ 
    	public double smallest( )
    	{
    		if(count == 0)
    		{
    			return Double.NaN;
    		}
     
    		return smallestValue;
    	}
     
     
    	/**
    	 * Determine 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.
    	 * Note:
    	 *   If length() is zero, then the answer is Double.NaN.
    	 **/ 
    	public double largest( )
    	{
    		if(count == 0)
    		{
    			return Double.NaN;
    		}
    		return largestValue;
    	}
     
     
    	/**
    	 * Add the numbers of another Statistician (addend) to this Statistician.
    	 * @param addend
    	 *   a Statistician whose numbers will be added to this Statistician
    	 * @precondition
    	 *   The parameter, addend, is not null. 
    	 * @postcondition
    	 *   The numbers from addend have been added to this Statistician. 
    	 *   After the operation, this Statistician acts as if it were given 
    	 *   all of its numbers and also given all of the numbers from the 
    	 *   addend.
    	 * @exception NullPointerException
    	 *   Indicates that addend is null.
    	 **/
    	public void add(Statistician addend)
    	{
    		if (addend.count != 0)
    		{
    			if (count == 0)
    			{
    				count = addend.count;
    				sumOfValues = addend.sumOfValues;
    				largestValue = addend.largestValue;
    				smallestValue = addend.smallestValue;
    			}
    			else
    			{
    				count += addend.count;
    				sumOfValues += addend.sumOfValues;
    				if (addend.largestValue > largestValue)
    					largestValue = addend.largestValue;
    				if (addend.smallestValue < smallestValue)
    					smallestValue = addend.smallestValue;
    			}
    		}
    	}   
     
    	/**
    	 * Create a new Statistician that behaves as if it was given all 
    	 * the numbers from this and the other Statistician 
    	 * @param other
    	 *   an existing Statistician
    	 * @precondition
    	 *   Neither this nor the other Statistician is null.
    	 * @return
    	 *   a new Statistician that acts as if it was given all the 
    	 *   numbers from this Statistician and the other Statistician 
    	 * @exception NullPointerException.
    	 *   Indicates that the argument is null.
    	 **/   
    	public Statistician union(Statistician other)
    	{
    		if (other == null)
    		{
    			throw new NullPointerException();
    		}
    		Statistician outcome = new Statistician();
    		outcome.add(other);
    		return outcome;
    	}
     
    }
    can you please tell me what might be wrong with the union method, they told me to create a clone instead of a new Statistician but I'm confused on how to do it, please

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Test Fails but I don't know why, please help

    Read the description of the union method:
    Create a new Statistician that behaves as if it was given all the numbers from this and the other Statistician
    The key words there are this and the other

  9. #9
    Junior Member cutekill0's Avatar
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Test Fails but I don't know why, please help

    Thank you very very much

Similar Threads

  1. Test Cpu and Thread
    By megaoverclock in forum Threads
    Replies: 0
    Last Post: August 25th, 2011, 04:37 AM
  2. java applet program question (on getting path fails)
    By hellocheese in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 30th, 2011, 04:34 PM
  3. Polymorphism test
    By speedycerv in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2011, 07:15 AM
  4. JUnit test for ER
    By raphytaffy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 20th, 2010, 09:26 PM
  5. Test and Set Explanation
    By bananasplitkids in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 6th, 2010, 01:10 PM