CS111 2012

Lab 5

Wed, Feb. 22, 2012

More Practice with Applications and Conditionals



EXERCISE 1 - Correct Change

Write a class CorrectChange that gives change in bills and coins based on the amount the buyer is owed. Such a program could be used by a sophisticated vending machine that can handle large bills.

Write a class method that takes two pieces of information (amount tendered and amount owed) and returns a string specifying the change, including the number of bills and coins of each denomination.

The method should determine the fewest number of each bill and coin needed.

For example, assume the buyer inserts $50 to pay for an item that costs only $2.37. The method should calculate that the buyer is owed $47.63. Assuming that a ten-dollar bill is the largest denomination returned, the program should print:

Your change is $47.63: 
4 Hamiltons
1 Lincoln
2 Washingtons
2 quarters
1 dime
3 pennies

Note that no nickels were printed because none were needed. Assume also that your machine always has enough change.


EXERCISE 2 - Rock-Paper-Scissors

Write a java program to play the "Rock-Paper-Scissors" with the computer. The player will be able to type in the keyboard one of the following three Strings:
  • "R"
  • "P"
  • "S"
for Rock, Paper, or Scissors, correspondingly.

For the computer's turn, your program should choose a random integer, from 0 (included) to 3 (not included). Each number will correspond to a String:

  • 0 corresponds to "R"
  • 1 corresponds to "P"
  • 2 corresponds to "S"
Then, the winner should be decided, based on the following rules:
  • Rock breaks Scissors, so Rock wins.
  • Scissors cut Paper, so Scissors win.
  • Paper covers Rock, so Paper wins.