|
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
The fancy bread shop, Oh Bonnie Pain, sells delicious Sugarbomb Donuts for 85 cents each. In order to seem european, they will list their prices using a comma as the decimal-separator: that is, “$4,50” rather than “$4.50”.
Your task: write a class DonutShop which has just one method, which gives a fancy-sounding message when somebody orders doughnuts.
class DonutShop { /** Respond to a donut order, at Oh Bonnie Pain. * @param numDonuts The number of donuts ordered. Non-negative. * @return A sophisticated-sounding message reporting the total price. * (see test cases). */ String donutOrderriere( int numDonuts ) { return "You'll replace this stub function."; } /** The price of one donut, *in pennies*. */ private int centsPerDonut; /** Construct a donut shop which charges 85 cents for a donut: */ public DonutShop() { this.centsPerDonut = 85; } } |
class DonutShopTester { public static void main( String[] args ) { DonutShop auBonP = new DonutShop(); System.out.println( "Ordering 10 donuts; the cashier's reply is:"); System.out.println( "Actual: \"" + auBonP.placeOrderriere(10) + "\"" ); System.out.println( "Expected: \"10 sucrebombe donuts will cost $8,50. Merci!\"" ); System.out.println( "Ordering 13 donuts; the cashier's reply is:"); System.out.println( "Actual: \"" + auBonP.placeOrderriere(13) + "\"" ); System.out.println( "Expected: \"13 sucrebombe donuts will cost $11,05. Merci!\"" ); System.out.println( "Ordering 2 donuts; the cashier's reply is:"); System.out.println( "Actual: REPLACE THIS WITH A METHOD CALL" ); System.out.println( "Expected: REPLACE THIS WITH EXPECTED ANSWER" ); System.out.println( "Ordering 1 donut; the cashier's reply is:"); System.out.println( "Actual: REPLACE THIS WITH A METHOD CALL" ); System.out.println( "Expected: REPLACE THIS WITH EXPECTED ANSWER" ); } } |
hint: To check whether or not a number n is equal to 1, use (n==1) as the condition in an if-else statement.
The above task really has two different parts: constructing the text-message, and converting a price into a fancy-euro-format. Modify your code by adding a second methods: priceInEuroFormat (which only deals with prices, and has nothing to do with donuts). Then modify your method donutOrderriere so that it calls priceInEuroFormat, passing in a price.
This version of the program behaves the same as before, but is better because it will let future users convert other (non-donut) prices into this format.
And if you finish that too:
Make a more general method priceInAnyFormat:
System.out.println( "Expect \"$8.50\": \"" + priceInAnyFormat( 850, "$", "." ) + "\"" ); System.out.println( "Expect \"USD8,50\": \"" + priceInAnyFormat( 850, "USD", "," ) + "\"" ); System.out.println( "Expect \"£8x50\": \"" + priceInAnyFormat( 850, "£", "x" ) + "\"" ); |
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
©2009, Ian Barland, Radford University Last modified 2009.May.06 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |