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 2 of 2

Thread: Code compiles, Is it logically correct?

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    35
    My Mood
    Lurking
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Code compiles, Is it logically correct?

    I think it is correct, just looking for a second opinion as my level of experience is questionable.

    /******************************************************************************
     * A Statistician keeps track of statistics about a sequence of double numbers.
     * Outline of Java Source Code for this class was obtained from:
     *   [url]http://www.cs.colorado.edu/~main/edu/colorado/homework/Statistician.java[/url]
     *   
     * Beth Katz modified it to use different method names and extra methods
     * January 2007 and January 2008
     * The student(s) listed below implemented it.
     * 
     * @author yourname
     * 
     ******************************************************************************/
     
    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 && this == null)
    		{
    			throw new NullPointerException();
    		}
    		Statistician outcome = new Statistician();
    		outcome.sumOfValues = other.sumOfValues + this.sumOfValues;
     
    		if (other.largestValue > this.largestValue){
    			largestValue = other.largestValue;
    		}
     
    		outcome.largestValue = largestValue;
     
    		if (other.smallestValue > this.largestValue)
    			smallestValue = other.largestValue;
     
    		outcome.smallestValue = smallestValue;
    		outcome.count = other.count + this.count;
    		return outcome;
    	}
     
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Code compiles, Is it logically correct?

    I'm almost sure it logically does what you wrote the code for, but what did you mean for it to do?
    What did your test results show? What tests did you do? Are there other tests you can try in addition?

Similar Threads

  1. Not sure how to logically correct a method.
    By orbin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 26th, 2012, 08:47 PM
  2. Code compiles fine but doesn't run Properly
    By nadirkhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 9th, 2011, 12:46 PM
  3. Can anybody correct the code?
    By ur2cdanger in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 24th, 2011, 12:50 AM
  4. Can anybody correct the code?
    By ur2cdanger in forum JDBC & Databases
    Replies: 1
    Last Post: October 17th, 2011, 07:07 PM
  5. I still have errors. Can you PLEASE correct my code?!
    By sam30317 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 6th, 2011, 06:10 AM