Java Reference
In-Depth Information
public Bug(int initialPosition)
and methods
public void turn()
public void move()
public int getPosition()
127
128
Sample usage:
Bug bugsy = new Bug(10);
bugsy.move(); // now the position is 11
bugsy.turn();
bugsy.move(); // now the position is 10
Your BugTester should construct a bug, make it move and turn a few
times, and print the actual and expected position.
΢΢Exercise P3.10. Implement a class Moth that models a moth flying across
a straight line. The moth has a position, the distance from a fixed origin.
When the moth moves toward a point of light, its new position is halfway
between its old position and the position of the light source. Supply a
constructor
public Moth(double initialPosition)
and methods
public void moveToLight(double lightPosition)
public void getPosition()
Your MothTester should construct a moth, move it toward a couple of light
sources, and check that the moth's position is as expected.
΢ Exercise P3.11. Implement a class RoachPopulation that simulates
the growth of a roach population. The constructor takes the size of the
initial roach population. The breed method simulates a period in which
the roaches breed, which doubles their population. The spray method
simulates spraying with insecticide, which reduces the population by 10%.
The getRoaches method returns the current number of roaches. A
program called RoachSimulation simulates a population that starts out
Search WWH ::




Custom Search