Hi all,
I have a question. I would appreciate it if anyone could help me out.
I have made 3 array lists. And now i want to calculate the average of
each array list. But my getAverage method calculate cumulatively, so
when i print the list of heavy (or light) antlist then it works well.
it only prints the number of ants in the heavy (or light ) antlists.
But as for average weight, it calculates cumulatively, in other words,
it calculates all the ants in the three lists. Does anyone know why it
happens? Thank you in advance.
import java.util.*;
public class Main {
// define class variables
public static int nextID = 0; // unique ID number for instances
public static int numberOfAnts = 32, cnt = 0;; // answer 0
public static double averageWeight = 0, sumWeight = 0;
public static void main ( String args[] ) {
Ant anAnt, anAnt1; // temp variables to hold address of Ant instance
Random rng = new Random(); // a rng
ArrayList <Ant> antList = new ArrayList <Ant> ();
ArrayList <Ant> heavyAntList = new ArrayList <Ant> ();
ArrayList <Ant> lightAntList = new ArrayList <Ant> ();
System.out.printf( "\nCreate %d ants...", numberOfAnts
);
for ( int i = 0; i < numberOfAnts; ++i ) {
anAnt = new Ant ( 4*rng.nextDouble() );
antList.add( anAnt );
}
System.out.printf( "Created %d ants. The are:\n", antList.size() );
for ( int i = 0; i < antList.size(); ++i ) {
(antList.get( i )).printSelf();
}
Main.getAverage( antList );
System.out.printf( "\nNow sort the ants depending on their weight.\n"
);
for ( int i = 0; i < antList.size(); ++i ) {
Ant a = antList.get ( i );
if ((a.getWeight()) > averageWeight ) {
heavyAntList.add( a );
} else {
lightAntList.add( a );
}
}
System.out.printf( "The heavy ants are :\n" );
Main.printAntList( heavyAntList );
Main.getAverage( heavyAntList );
System.out.printf( "\nThe light ants are :\n" );
Main.printAntList( lightAntList );
Main.getAverage( lightAntList );
....
public static void printAntList ( ArrayList <Ant> list ) {
for ( int i = 0; i < list.size(); ++i ) {
Ant a = list.get( i );
System.out.printf( "%d ID = %d, weight = %.3f\n", i,
a.getID(), a.getWeight() );
}
}
public static void getAverage ( ArrayList <Ant> list ) {
for ( int i = 0; i < list.size(); ++i ) {
Ant a = list.get( i );
sumWeight += a.getWeight();
cnt = cnt + 1 ;
averageWeight = sumWeight / cnt;
}
System.out.printf( "sum=%.3f, the number of ants=%d,", sumWeight,
cnt);
System.out.printf( " Average weight=%.3f", averageWeight);
}
}
Roedy Green - 09 Dec 2005 07:00 GMT
> public static void getAverage ( ArrayList <Ant> list ) {
this method is misnamed. If it GOT the average it would return it.
It should be called displayAverage.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 09 Dec 2005 07:01 GMT
>for ( int i = 0; i < list.size(); ++i ) {
> Ant a = list.get( i );
> sumWeight += a.getWeight()
sumWeight should be local and zeroed before the loop.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 09 Dec 2005 07:03 GMT
> for ( int i = 0; i < list.size(); ++i ) {
> Ant a = list.get( i );
> sumWeight += a.getWeight();
> cnt = cnt + 1 ;
> averageWeight = sumWeight / cnt;
you don't need to compute a count, you have it with list.size.p
you failed to zero cnt before the loop.
You don't need to compute averageWeight until you are finished the
loop.
Again AverageWeight should be local.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Bjorn Abelli - 09 Dec 2005 08:13 GMT
"thickface" wrote...
> I have a question. I would appreciate it if anyone could help me out.
> I have made 3 array lists. And now i want to calculate the average of
[quoted text clipped - 4 lines]
> it calculates all the ants in the three lists. Does anyone know why it
> happens? Thank you in advance.
> public class Main {
> // define class variables
> public static int nextID = 0; // unique ID number for instances
> public static int numberOfAnts = 32, cnt = 0;; // answer 0
> public static double averageWeight = 0, sumWeight = 0;
You haven't reset the variables above between the calculations...
> public static void getAverage ( ArrayList <Ant> list ) {
// As you use them now, you should reset the variables you use
// in the calculation here, to 0...
// Besides, why do you calculate averageWeight *inside* the loop?
// ...and why use cnt at all, when you have list.size()?
> for ( int i = 0; i < list.size(); ++i ) {
> Ant a = list.get( i );
> sumWeight += a.getWeight();
> cnt = cnt + 1 ;
> averageWeight = sumWeight / cnt;
> }
// Bjorn A
thickface - 09 Dec 2005 14:39 GMT
Thank you very much. I really appreciate you guys' advices.