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

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs

hw07
potions and padding

Due Apr.08 (Wed).

  1. In the game “World of Wow” (abbreviation: “W.o.Wow”), every PotionBottle has a color, a potency (a real number in [0,1] — that is, between 0 and 1 inclusive), and a number of remaining doses. Write the following methods:
    1. A constructor which takes in all three inputs.
    2. Getters for the potency and number of doses
    3. A method isEmpty, which returns whether or not there are no remaining doses.
    4. A method consumeOneDose, which reduces the remaining doses by one (but a PotionBottle's remaining doses can never become negative.)
    5. A method toString, which returns a description of the PotionBottle: something like "4 doses of a red potion (potency 0.7)". (You will need to be sure to make this method public, to compile.)
    6. calculateStorePrice. A potion's market price (in gold pieces) is calculated as: 15 times its potency times the number of doses.
    7. If a player wants to sell a PotionBottle, they can't get the full store price1. Write the method calculateSecondhandPrice, which is always2 2/3 of the store price.
    Here is some tester code (it should look similar to the tests you'd come up with yourself).
  2. Write the method stringTimes, which returns a multiple copies of a string appended together. One example is given; you should figure out the expected results for the other cases shown and test those as well:
          stringTimes("cat",3)  returns  "catcatcat"
          stringTimes("cat",1)  returns                  
          stringTimes("cat",0)  returns                  
          stringTimes("z", 5)  returns                  
          stringTimes("z", 1)  returns                  
          stringTimes("", 5)  returns                  
          stringTimes("", 0)  returns                  
        
  3. Write the method padLeft(String s, int width): it returns the string s, but padded so that it is at least width characters long. That is, if s is shorter than width, then add spaces to the front of s; if s is already longer than width then just return s (without adding any spaces to it).
          padLeft("cat",6)   returns  "   cat"
          padLeft("horse",6) returns  " horse"
          padLeft("cat",3)   returns  "cat"
          padLeft("cat",2)   returns  "cat"
        
    Don't repeat code; your solution is only a few lines of code, and should not have a loop(!). Here's a hint.

1Note that the same thing holds true, even for commodities: a new CD might cost $15 on amazon, but used copies (which are exactly as good as new) often sell for only $10 or less. Mostly this is because the buyer is taking a bit of a risk, buying from an unknown individual who might not exchange a defective item. (Another reason bringing down the price of used commodities is that if someone is buying the CD as a gift, it can be a bit gauche to give a used copy. I suspect this is a relatively small part of the price-difference.)      

2 If W.o.Wow ever changes its formula for the market price changes, that would of course require changing the code for calculateStorePrice. But, that should not require changing the code for calculateSecondhandPrice, which should still return 2/3 of the (revised) store price.      

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs


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