Total de visualizações de página

sábado, 11 de dezembro de 2010

Implement a class RoachPopulation that simulates the growth of a population of roaches. Provide an application to run the simulation.

CS 120 Assignment # 6

Objective

Implement a class RoachPopulation that simulates the growth of a population of roaches.  Provide an application to run the simulation.

Project Specifications

1.                  The class RoachPopulation keeps track of the size of the population of roaches.
2.                  The constructor takes the size of the initial roach population.
3.                  Overload the constructor to allow for a default initial value.
4.                  Provide a method breed to simulate a period where the population doubles. 
5.                  Provide a method spray to simulate spraying with insecticide.  When we call this method, the population will automatically be reduced by 10%.
6.                  Provide a method getRoaches() that returns the current value for the population of roaches.
7.                  Provide a program called RoachSimulation to simulate a kitchen that starts out with some roaches.
8.                  Breed, spray and then print the roach count.  Repeat several times.
9.                  Please use comments to display your name, date and assignment number.
10.       Provide documentation comments for your class RoachPopulation.  Make sure JavaDoc produces a usable documentation file.

Final Touches

Submit both java files ZIPPED through blackboard no later than November 18 by 11:59 pm. 
Item A6

>> View/Complete Assignment: A6
Item Solution
public class RoachPopulation
{
   /**
      Constructs the population.
      @param initialPopulation the initial population
   */
   public RoachPopulation(int initialPopulation)
   {
      roaches = initialPopulation;
   }
   /**
      Waits for the population to double.
   */
   public void breed()
   {
      roaches = 2 * roaches;
   }
   /**
      Simulates applying of insecticide.
   */
   public void spray()
   {
      roaches = roaches * 9 / 10;     
   }
   /**
      Gets the current number of roaches.
      @return the roach count
   */
   public int getRoaches()
   {
      return roaches;
   }
   private int roaches;
}
/**
   This program tests the RoachPopulation class.
*/
public class ExP2_16

   public static void main(String[] args)
   { 
      RoachPopulation kitchen = new RoachPopulation(10);
      kitchen.breed();
      kitchen.spray();
      System.out.println(kitchen.getRoaches() + " roaches");
      kitchen.breed();
      kitchen.spray();
      System.out.println(kitchen.getRoaches() + " roaches");
      kitchen.breed();
      kitchen.spray();
      System.out.println(kitchen.getRoaches() + " roaches");
      kitchen.breed();
      kitchen.spray();
      System.out.println(kitchen.getRoaches() + " roaches");
   }
}

OK 
 

2 comentários:

  1. Create a class named Package with data fields for weight in ounces, shipping method, and shipping cost. The shipping method is a character: A for air, T for truck, or M for mail. The Package class contains a constructor that requires arguments for weight and shipping method. The constructor calls a private calculateCost() method that determines the shipping cost based on the following:

    Shipping Method ($)

    Weight (ounce) Air Truck Mail

    1 to 8 2.00 1.50 0.50

    9 to 16 3.00 2.35 1.50

    17 and over 4.50 3.25 2.15

    The Package class also contains a display() method that displays the values using appropriate fields. Create a subclass named InsuredPackage that adds an insurance cost to the shipping cost based on the following:

    Shipping Cost Before Insurance ($) Additional Cost ($)

    0 to 1.00 2.45

    1.01 to 3.00 3.95

    3.01 and over 5.55

    Write a program that instantiates at least three objects of each type (Package and InsuredPackage) using a variety of weights and shipping method codes. Display the result for each Package and InsuredPackage.

    ResponderExcluir