Question: Horstmann - Chapter 3: Reminder: Case is important in Java. Be sure that the class name and file name are exact and begin with a
-
Horstmann - Chapter 3:
Reminder: Case is important in Java. Be sure that the class name and file name are exact and begin with a capital letter.
Part 1: Roach.java
Implement a class Roach 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(double percent) method simulates spraying with insecticide, which reduces the population by the given percentage.
- The getRoaches method returns the current number of roaches.
A program called RoachSimulation simulates a population that starts out with 10 roaches.
- Breed, spray to reduce the population by 10 percent, and print the roach count.
- Repeat three more times.
Submit the completed code back to this assignment.
Part 2: MouseLine.java
Write a class MouseLine that models a Mouse moving along a horizontal line. The Mouse moves either to the right or left.
Initially, the bug moves to the right, but it can turn to change its direction. In each move, its position changes by one unit in the current direction.
Provide a constructor public Mouse(int initialPosition) and methods public void turn(), public void move(), and public int getPosition()
Sample usage:
Mouse mousy = new Mouse(10); mousy.move(); // Now the position is 11 mousy.turn(); mousy.move(); // Now the position is 10
Your MouseTester should construct a mouse, make it move and turn a few times, and print the actual and expected position.
Submit the completed code back to this assignment.
Part 3: House.java
Write a program House.java that draws a picture of a house. It could be as simple as the accompanying figure, or if you like, more elaborate (3-D, skyscraper, marble columns in the entryway, whatever). Implement a class House and supply a method draw(Graphics2D g2) that draws the house.
Create a DrawHouse.java to call your house drawing and put it on the screen (see the Italian Flag example in the textbook for a structure to do this).
Submit the completed code back to this assignment.
Submit back to this assignment: - Roach.java
- RoachSimulation.java
- MouseLine.java
- MouseTester.java
- House.java
- DrawHouse.java
End of Lab
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
