/* SimpleExample/Main.java Main -- Simple class to get program going. In this case, it creates a few Ant instances, asks them to do a few things. Note we use some Java classes -- ArrayList and Random Compile: javac Main.java Ant.java Run: java Main Copy: cp /users/rlr/Courses/530/Src/SimpleExample/*.java yourDir */ import java.util.*; // for misc java classes (eg ArrayList, Random) public class Main { public static void main ( String args[] ) { Random rng; // address of a instance of Random int numberOfAnts = 8; // create this many ants. Ant anAnt, anAnt1; // variables to hold addresses of Ant instances // define an ArrayList object that can contain pointers to Ant objects // note we specify that this ArrayList can refer *only* to Ant objects ArrayList antList = new ArrayList (); rng = new Random(); // create instance of Random -- see the Java API Ant.setRng( rng ); // tell the Ant class about the rng we created. // lets create a bunch of ants, adding them to the antList. System.out.printf( "\nCreate %d ants...", numberOfAnts ); for ( int i = 0; i < numberOfAnts; ++i ) { anAnt = new Ant( i, i*2 , i * 4.2 ); antList.add( anAnt ); } // Now send the printSelf message to each Ant on the list. // get(i) returns the pointer to the i-th entry (NB: counts from zero!!) System.out.printf( "Created %d ants. They are:\n", antList.size() ); for ( int i = 0; i < antList.size(); ++i ) { anAnt = antList.get( i ); anAnt.printSelf(); } // note we don't have to store the reference in a local variable: System.out.printf( "\nAgain the Ants are:\n", antList.size() ); for ( int i = 0; i < antList.size(); ++i ) (antList.get( i )).printSelf(); // Lets send the ants another message, and print what each returns to us. System.out.printf( "\nSend doSomething() to the Ants, print return value:\n"); for ( int i = 0; i < antList.size(); ++i ) { anAnt = antList.get( i ); double d = anAnt.doSomething( i ); System.out.printf( " --- %.1f placed in variable d in Main.\n", d ); } // remove ant by position in list (NB: counts from zero!!) int index = 3; anAnt = antList.remove( index ); System.out.printf( "\nRemoved Ant from position (index) %d:\n", index ); anAnt.printSelf(); System.out.printf( "The list now:\n" ); Main.printAntList( antList ); // change its weight, then add it back by position anAnt.setWeight( 1000.0 ); antList.add( index, anAnt ); System.out.printf( "\nList after add Ant (wt=1000) back to pos=%d:\n",index ); Main.printAntList( antList ); // lets add it in again (to the end this time!) antList.add( anAnt ); System.out.printf( "\nThe list after adding ant again to end:\n" ); Main.printAntList( antList ); // lets change the weight in the Ant that anAnt references. anAnt.setWeight( 20000 ); System.out.printf( "\nThe list after setting weight of anAnt to 20000:\n" ); Main.printAntList( antList ); // lets remove it by reference -- removes the first occurence in the list! antList.remove( anAnt ); System.out.printf( "\nThe list after removing it by reference:\n" ); Main.printAntList( antList ); // lets re-assign weights at random using the new java for-construct: for ( Ant a : antList ) a.setWeight( rng.nextDouble() ); System.out.printf( "\nThe list after randomizing weights:\n" ); Main.printAntList( antList ); // an example of a while-loop and if-else test System.out.printf( "\nThe list categorized:\n" ); int i = 0; while ( i < antList.size() ) { Ant wAnt = antList.get(i); // Tthese variables exist ONLY in this loop!! double lowerBd = 0.333, upperBd = 0.667; if ( wAnt.getWeight() < lowerBd ) { System.out.printf( "Ant %d wt = %.3f (wt < %.1f).\n", wAnt.getID(), wAnt.getWeight(), lowerBd ); } else if ( wAnt.getWeight() > upperBd ) { System.out.printf( "Ant %d wt = %.3f (wt > %.1f).\n", wAnt.getID(), wAnt.getWeight(), upperBd ); } else { System.out.printf( "Ant %d wt = %.3f (%.1f <= wt <= %.1f).\n", wAnt.getID(), wAnt.getWeight(), lowerBd, upperBd ); } ++i; // DON'T FORGET THIS!! } System.out.printf( "\nAll done!\n\n" ); } // a (static) method to print the contents of an Ant list public static void printAntList ( ArrayList list ) { for ( Ant a : list ) // Note this kind of loop construct! System.out.printf( " ID = %d, weight = %.3f\n", a.getID(), a.getWeight() ); } }