Welcome to JavaRanch!

The addAll() method is defined for Collections - ArrayList, HashSet, etc. What type of object do you have?

[OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]

Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2

I need to collect numbers and manipulate them the following code but I don't know where to start

Well, a good place to start is with the definition... What does it mean to add a statistician to another statistician? After all, you can't code the logic, if you don't know what the logic is supposed to be.

Henry

Originally posted by Teri Fisher:

I believe that I need to create an instance of Statistician and pass it a series of numbers using .next number() and then addAll will hold the total.

Using this definition, shouldn't you worry about implementing the next() method first? Or at least in parallel with the addAll() method?

Anyway... What have you tried? How do you think you should store this series of numbers being passed via the next method? A good place to start is with this -- you can worry about how to add them up, once you know how they are stored.

Henry

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 ]

- To do min, you probably need to do something similar to what you did with max.

- What is the difference between sum and total?

Now, you just need to implement the next() method, which is probably easier as next() is adding one number, versus addAll() which adds a whole statistician.

Henry

I am also supposed to use IntArrayBag without using an array but I haven't been able to get that to work.

Well, when you figured out enough, to get to a specific issue, feel free to ask about it here. Obviously, "haven't been able to get that to work" doesn't give us enough details.

As for "IntArrayBag", that is probably a class provided by your instructor -- so keep in mind, we don't have any details on that either.

Henry

Originally posted by Teri Fisher:

Sorry, I guess I neglected to say that I had updated my code so I was referring to that as far as trying to figure out my mess. I am now trying to test my code and I keep getting "double cannot be dereferenced" error.

Thanks.

A quick look at you code...

In Line A, currentNum is a variable that holds a double precision floating point number (AKA double). In Line B, you are trying to use it to call the addend() method. The compiler is trying to dereference the variable, in order to call the method -- which for doubles is not possible.

Henry

Originally posted by Teri Fisher:

Help!! I have posted my new code and am having trouble creating my test code. Any suggestions?

Well, you are the developer of the Statistician class, so it is you who know best how to exercise the class. The purpose of the test program is to see if the class that you wrote works as you designed. If you are having trouble creating test code, how do you expect us to do it?

BTW, as a suggestion, try to detact yourself from your code. Many developers know their code so well, that they subconsciencely write unit tests that does *not* test stuff that they know won't work. One of the goals of you test program should be to try to break your code.

Henry

Don't lose any more code

[ February 04, 2008: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."

quote:

___________________________________________________________________________

Well, when you figured out enough, to get to a specific issue, feel free to ask about it here. ___________________________________________________________________________

Feel free???

**[Paul Clapham]: Well, you keep saying you've "posted" or "replaced" your code, but you haven't. The last code I see is in a post dated January 31st. It's pretty hard to comment on code we can't see.**

I think Teri is referring to the post dated the 28th, which is also edited February 2. Teri, it will probably be clearer if in the future you post your new code in new posts. Otherwise it gets very confusing for people trying to understand the sequence of events - replies to your Jan 28 post don't make sense anymore if your Jan 28 post has changed substantially.

Also, please use code tags when you post code, as it makes the code more readable by preserving indentation. (As you see with Nick's post above.)

"I'm not back." - Bill Harding, *Twister*

Well, now that you point it out, you did edit that post on Feb 2nd. Did you expect us to notice that, though? I certainly didn't. But now that you have pointed it out, what's your question? I'm quite lost, and all you said is "am having trouble creating my test code". That doesn't really zero in on a problem.Originally posted by Teri Fisher:

Hmmm...looks to me like the code I originally posted on Jan 28 was last edited/updated by me on Feb 2nd.

Don't get me started about those stupid light bulbs. |