RU beehive logo ITEC dept promo banner
ITEC 120
2009spring
ibarland
nokie
jmdymacek

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs

hw04
metamorphosis

Due Feb.20 (Fri). Here are some hw04-pi-notes—hw04 notes for PIs.

  1. (2pts) R3.10 (2ed: R3.12)
  2. (2pts) R3.11 (2ed: R3.1)
  3. (2pts) R3.12 (2ed: R3.2)
  4. (9pts) P3.11 (2ed: P3.12), RoachPopulations.
  5. (9pts) We'll modify the previous problem, by adding a method sprayWith, which takes in the name of the chemical being used.

    It's a little-known fact that the effectiveness of a chemical depends only on the length of its name(!), divided by four. Spraying with "DichloroDiphenylTrichloroethane" (32 letters) will reduce a RoachPopulation to 1/8 of the previous size, spraying with with "Agent Orange" (12 letters) will reduce a RoachPopulation to 1/3 of its previous size1, and spraying with "coffee" will reduce a population to 1/(6/4) = 4/6 = 2/3 of its original size. You can round results using Math.round(double)2

    Note carefully: sprayWith reduces a population to a certain fraction, while the book's spray reduced a population by a certain fraction.

    Remember the sequence to follow, when writing any method:

    The tutors/PIs have been instructed not to help with the code for sprayWith until they see your test-cases and signature.3 (Of course, they'll help you with those steps, if you ask.)

  6. (6pts) Finally, add a method predictKills which takes in the name of a pesticide, and returns the number of individual roaches that would be killed, if you were to spray with that pesticide. However, this method does not actually modify the RoachPopulation. (Writing test cases shouldn't be too difficult, since you can easily adapt the test cases you already wrote for the previous problem.)

  7. (0pts) Self-challenge: If you finish all the above, are bored, and are itching to do more: You might be bothered by the fact that two different places in your code now do the same thing: both sprayWith and predictKills do very similar arithmetic. Your challenge is to:

    get rid of the redundant code by having your sprayWith method call the predictKills method as one step in its solution.
    This won't change your program's answers, but it will result in better code (clearer, cleaner, and easier to modify in the future).

(Note that you can turn in one single class RoachPopulation which answers 4, 5, and 6. You don't need to make three separate classes.)


1Okay okay, Agent Orange is actually an herbicide, which might explain why it's not as effective as DichloroDiphenylTrichloroethane.      

2 One Java glitch: Math.round returns not an int, but a long (a “long integer”). The best solution is for your program to use longs instead of ints. (Sigh.)      

3 The reason for this is not just to be annoying and inflexible; rather, experience has shown that students who skip that step will have trouble with the code precisely because they are not quite sure what the method needs to do exactly. This is the true purpose of developing test cases and method-signature first. This approach is what is used by programmers in industry.      

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs


©2009, Ian Barland, Radford University
Last modified 2009.Feb.22 (Sun)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme