Roach Master: Putting it all together Mohammad T . Irfan - - PDF document

roach master
SMART_READER_LITE
LIVE PREVIEW

Roach Master: Putting it all together Mohammad T . Irfan - - PDF document

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


slide-1
SLIDE 1

10/29/2013 1

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

slide-2
SLIDE 2

10/29/2013 2

What we want

Controller Class

  • Create many different food items
  • Create many different roaches
  • Shoo roaches near the mouse pointer

away

Roach Class <Template for just one roach>

  • Moves in a given direction
  • Moves automatically on the

floor, within bounds

  • Eats food

Food Class <Template for just one circular food item>

  • Decrease its diameter

Controller Class

slide-3
SLIDE 3

10/29/2013 3

Roach Class Food Class

slide-4
SLIDE 4

10/29/2013 4

Anatomy of a method

Access Control: public/private Return type: void/int/bool/doub le/Location/ <some class> Method Name <list of parameters> ) ( { //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 } Following is within some class A Optional: Can save the returned value from the method in another variable. Can’t do this if the method returns void.

How to “call” a method?

Name of instance variable/object of a class Name of a method defined within that same class A variable of the same type as the return type of the method

=

Need to give an object name explicitly when you are calling a method from a different class

slide-5
SLIDE 5

10/29/2013 5

Example

  • The following method is defined within a class named A.
  • stw = something wrong

public double divide (double a, double b) { 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 ??? }

Within class A //create an instance variable of class A to call A’s method A aInstance = new A(); int result = aInstance.divide (10, 0); // stw ???

Example (cont...)

Within class B (inside a method) //no need for an instance variable to call a method ... //within the same class A double result = divide (10, 100); Within class A (inside a method)

slide-6
SLIDE 6

10/29/2013 6

Another example

private void exchange ( int a, int b) { int c = a; a = b; b = c; } public static void main (Stirng [] args) { int a = 10; int b = 20; exchange (a, b); System.out.println(a + “, ” + b); //prints 10, 20. why? } Within class A arguments parameters

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 completely different!

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

slide-7
SLIDE 7

10/29/2013 7

Controller Class RoachMasterV2

slide-8
SLIDE 8

10/29/2013 8

RoachMasterV2: begin()

RoachMasterV2: onMouseMove()

slide-9
SLIDE 9

10/29/2013 9

Roach Class

Roach Class

slide-10
SLIDE 10

10/29/2013 10

Roach: Constructor Method Roach: Move in a given direction

slide-11
SLIDE 11

10/29/2013 11

Roach: Move randomly – newly added Roach: Move Randomly (cont...)

slide-12
SLIDE 12

10/29/2013 12

Roach: getLocation() method Roach: Eat food – newly added

slide-13
SLIDE 13

10/29/2013 13

Roach: run() method – newly added

Food Class

slide-14
SLIDE 14

10/29/2013 14

Food Class – newly added Food: Constructor method

slide-15
SLIDE 15

10/29/2013 15

Food: decrease() Food: getCenterLocation()

slide-16
SLIDE 16

10/29/2013 16

Food: getTopLeftLocation()