10/29/2013 Roach Master: Putting it all together Mohammad T . Irfan 10/29/2013 What we want • Want to create hundreds of roaches, each of which has a head, a body, and two antennas, with different initial locations and different speeds • Want to create many circular food items, with different locations and diameters • If the mouse pointer goes close to a roach, it shoos the roach away • The roaches move automatically on the floor (within bounds) in horizontal direction with random vertical movements • If a roach gets some food, it eats the food. In that process, it decreases the food diameter 1
10/29/2013 What we want Roach Class <Template for just one roach> • Moves in a given direction • Moves automatically on the floor, within bounds Controller Class • Eats food Create many different food items • • Create many different roaches • Shoo roaches near the mouse pointer away Food Class <Template for just one circular food item> • Decrease its diameter Controller Class 2
10/29/2013 Roach Class Food Class 3
10/29/2013 Anatomy of a method Following is within some class A Return type: Access Control: void/int/bool/doub Method <list of ( ) public/private le/Location/ Name parameters> <some class> { //Body of the method //Must return something of the same type as the return type //Once the return statement is executed, this method exits immediately //For void : you can say return; which will make this method exit } How to “call” a method? Optional: A variable of the = Name of instance Name of a method same type as the variable/object of defined within that return type of the a class same class method Can save the returned value from the method in another variable. Can’t do this if the method returns void. Need to give an object name explicitly when you are calling a method from a different class 4
10/29/2013 Example • The following method is defined within a class named A. • stw = something wrong Within public double divide (double a, double b) class A { if (b != 0) { return a/b; System.out.println(a/b); // stw ??? } else //Now, b is 0. Can’t do division by 0. { return false; // stw ??? Can you think of an alternative? } System.out.println (“Job well done! Now return.”); // stw ??? return a/b; // stw ??? } Example (cont...) Within class B (inside a //create an instance variable of class A to call A’s method method) A aInstance = new A(); int result = aInstance.divide (10, 0); // stw ??? Within class A (inside a //no need for an instance variable to call a method ... method) //within the same class A double result = divide (10, 100); 5
10/29/2013 Another example Within class A private void exchange ( int a, int b) { int c = a; parameters a = b; b = c; For primitive types, the values of } arguments are copied to the parameters. The arguments and the parameters may have the same names, but they are public static void main (Stirng [] args) completely different! { arguments int a = 10; int b = 20; exchange (a, b); System.out.println (a + “, ” + b); //prints 10, 20. why? } Object name is not required here. Why? Notes • Be careful about the package names (the names you will see in the slides might be different from the names in your classes) • You may use the codes given here as the start- up codes for your next lab assignment 6
10/29/2013 Controller Class RoachMasterV2 7
10/29/2013 RoachMasterV2: begin() RoachMasterV2: onMouseMove() 8
10/29/2013 Roach Class Roach Class 9
10/29/2013 Roach: Constructor Method Roach: Move in a given direction 10
10/29/2013 Roach: Move randomly – newly added Roach: Move Randomly (cont...) 11
10/29/2013 Roach: getLocation() method Roach: Eat food – newly added 12
10/29/2013 Roach: run() method – newly added Food Class 13
10/29/2013 Food Class – newly added Food: Constructor method 14
10/29/2013 Food: decrease() Food: getCenterLocation() 15
10/29/2013 Food: getTopLeftLocation() 16
Recommend
More recommend