Introduction to Computer Science

Program 3

(Thanks to Dr. Liszka) (Rubric)

Your mother’s kitchen is being overrun by cockroaches! These are not only the most despicable creatures on the earth (next to college professors, of course) but this particular clan of crustaceans has been scientifically altered by a graduate student in the Biology department to be extremely hardy. Apparently several of them hitched a ride home with you in your backpack. Needless to say, your mother is very upset with you! Since you are taking a class in programming and not pest control, you’ve advised your mother to buy a can of Raid and not walk around the house in bare feet. Needless to say, she’s not buying it. My first suggestion is to apply your educational skills to analyze the problem. In the manner of a typical (boring) academic, you should study the situation by writing a program that simulates roach population growth and control. This will help you decide if your mother’s concerns are legitimate or if she is simply overreacting.

The Program

A Kitchen class contains a population of cockroaches (think variable here) along with various methods for increasing and decreasing the population.

/** 
	your header information
 */

public class Kitchen
{

  // declare your class variable(s) here


// Constructors (remove this comment line)

  /** Constructs a brand new Kitchen with NO roaches (initialized to 0) */


  /** Constructs a Kitchen with a parameter 
      @param initialRoaches  Initial number of roaches*/



// Mutator methods (remove this comment line)

  /** Leave food on the counter, increasing the population */


  /** Clean counters, put away food, 
  slightly decreasing the population by starvation */

 
  /** Take action and spray them with harsh chemicals */


  /** Take drastic action and call an exterminator */



// Accessor methods (remove this comment line) 

  /** Get current number of roaches in kitchen
      @return the roachPopulation */



}

Use the above skeleton, Kitchen.java. Let’s look at what you should have in your Kitchen class (File: Kitchen.java).
Constructors:

Mutator Methods:

Important Note! Live roaches must contain all body parts! That means you will need to convert a floating point operation to an integer. Do this with something called type casting. Example:

  int xVal;
xVal = (int)(3.14 * radius); // xVal will stay an integer; you should be ok

Accessor Method:

Now you need to test your program. Create a class in another file, called RoachTester. This will have one method, main, that will create a Kitchen object and perform these tasks, in the following order:

Day 1:
   Print out the number of roaches that arrived in your backpack.
   Leave some food out on the kitchen counter overnight.
Day 2:
   Print out the number of roaches present in the morning..
   Clean up the kitchen before bed.
Day 3:
   Print out the number of roaches present in the morning.
   Leave more food out on the kitchen counter overnight.
Day 4:
   Print out the number of roaches present in the morning.
   Spray the counter with commercial bug spray.
Day 5:
   Print out the number of roaches present in the morning.
   Leave crumbs out on the counter (you’re a slow learner).
Day 6:
   Print out the number of roaches present in the morning.
   Call the exterminator.
Day 7:
   Print out the number of roaches present after the exterminator leaves

Your dialog (what you actually print) is up to you as long as the proper numbers are displayed in the proper order. However, make sure it is clear what is happening in the program. (Ex. “There are 16 roaches currently living in the kitchen.”) You also need to document the program – both the Kitchen.java file and your RoachTester.java. Make sure you have proper headers. Use good variable and method names.

Your main program will need to start like this in order to determine the initial population. We will be using a command line argument.

public static void main(String[] args){
   int startingPopulation = Integer.parseInt(args[0]);

The value provided on the command line when the program is run will be used to instantiate the Kitchen with a beginning population. How do you use command line arguments?

You can supply the test value on the command line when you run the program if you are running programs this way. For example:

java TestKitchen 100

In jGrasp, you specify the command line argument, the 100, in a little textbox at the top of the editor window. You will need to turn on the Run Arguments option under the Build menu. Just type the number in the text box before running the program. Try different numbers.

In Eclipse, use the Run... under the Run menu, and then in the Run dialog, click the Arguments tab. Enter the test value, 100, in the program arguments textbox, then click Run. The test value will be remembered for subsequent testing. You can put different numbers in the box to test other initial populations.

Test for starting input of 100 roaches. You should get the following counts:
150, 120, 180, 108, 162, 16

Note that we will be testing for different starting numbers of roaches so you should play around with it and manually check your results to see if they are correct.

Submission instructions

Be sure to follow the Documentation Standards (for this and every program). When you are ready to submit, obtain a printed copy of your program. Remember to sign and attach the required Academic Integrity Pledge cover sheet. The printed program must be turned in to your instructor by the start of class on the date the program is due. You must also email a zipped copy of the program to your instructor. Identify the email with the subject: ICS Program 3 and be sure that your name appears inside the message. The attached zip file must be named as your last name and the program number (example: smith_3.zip). The zip file must contain a folder having the same name (smith_3). The program files (two Java source files) should be inside this folder. Be sure to email your working solution before the due date! Do not submit non-working programs.

Email addresses: