Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

ArrayList -- cumulatively calculates??

Thread view: 
thickface - 09 Dec 2005 06:22 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.