• September 20th, 2011, 10:46 AM
cutekill0
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:

Code Java:

```  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:
Code Java:

```  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); }   }```
• September 20th, 2011, 11:32 AM
copeg
Quote:

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
• September 20th, 2011, 08:44 PM
cutekill0
I'm very sorry, the tests that are failing are union, allNegative, compareTwoSmall, compareTwoMedium, unionSmall, addExtremes, and cloneEquals, I hope you can help me
• September 20th, 2011, 09:16 PM
copeg
Quote:

Originally Posted by cutekill0
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?
• September 20th, 2011, 09:31 PM
cutekill0
well the failure in union is at assertTrue("Union not empty", w.length( ) == 6);
• September 21st, 2011, 11:16 AM
copeg
Quote:

Originally Posted by cutekill0
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)
• September 22nd, 2011, 10:07 AM
cutekill0
ok so I fixed some things and I'm passing most of my tests except the union tests
Code Java:

```  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
• September 22nd, 2011, 11:30 AM
copeg