question uploaded
CPS 132, Program #2 - Decisions, Decisions!
In chapter 2 of the textbook, a Bluej project named “naive-ticket-machine” is presented. The authors then describe some improvements to that program. For this, assignment, you will also start with the “naive-ticket-machine” code from the textbook. However, you will be improving this code in a different way, as described below.
Part 1: Replace the “insertMoney” method. (10 points)
Most vending machines won't accept just any amount of money, all at once. Instead, the customer will insert coins, one at a time, into the machine. In order to make this program a little more realistic, you are to replace the current “insertMoney” method with a method having the following signature:
public void insertCoin(String coin)
When calling this method, one the following parameters should be expected: “nickel”, “dime”, “quarter”, or “dollar”. This is to simulate a customer inserting 5, 10, 25, or 100 cent coins into the machine. In other words, calling the method with “dime” should cause the balance to increase by 10 cents, etc. Please note that strings are NOT compared the same way integers are. In other words, the line:
if (coin == “dime”)
is incorrect. The corresponding correct line of code would be:
if (coin.equals(“dime”))
The method call coin.equals(“dime”) will evaluate as true only if the parameter named coin contains exactly the string “dime”. ‘[Just so you know, the method call to check for inequality is !coin.equals(“dime”)]
If the method is called with an invalid string (like “dim” instead of “dime”), the balance should not be affected.
Note that you will be replacing the entire “insertMoney” method with this new one. When you are done, the “insertMoney” method will no longer exist. It will have been replaced by “insertCoin”.
Part 2: Automatically dispense ticket. (10 points)
Now, we're going to improve the “insertCoin” method even more as follows:
• If the coin inserted by the user makes the balance at least high enough to cover the price of a ticket, the following three things should happen: ◦ The ticket should be printed ◦ A message should be printed informing the user of the change returned. ◦ The balance should be set to 0.
• Otherwise, the user should be informed (by way of a message) how much more money needs to be inserted.
Note that all the changes you make here should be to the “insertCoin” method. Don't change anything in any of the other methods, or add any new methods. (Compared to other methods we have seen, this method may begin to look rather long.)
Below is an example of what might be seen in the Bluej terminal window when testing this. This list assumes that the “record method calls” option in the terminal window is turned on.
TicketMachine ticketMa1 = new TicketMachine(225); ticketMa1.insertCoin("dollar") Insert 125 cents more for a ticket. ticketMa1.insertCoin("quarter") Insert 100 cents more for a ticket. ticketMa1.insertCoin("quarter") Insert 75 cents more for a ticket. ticketMa1.insertCoin("quarter") Insert 50 cents more for a ticket. ticketMa1.insertCoin("quarter") Insert 25 cents more for a ticket. ticketMa1.insertCoin("dime") Insert 15 cents more for a ticket. ticketMa1.insertCoin("dime") Insert 5 cents more for a ticket. ticketMa1.insertCoin("dime") Your change is 5 cents. ################## # The BlueJ Line # Ticket # 225 cents. ##################
Part 3: Further Improvements (10 points)
You can further improve your “insertCoin” method as follows (one point each):
• Insure that the “Your change is ...” message is not printed if the change is zero cents. • Display an error message if an invalid string is provided as a parameter. • Have the remaining amount needed be displayed as dollars and cents. Example:
Insert 1 dollar(s) 25 cents more for a ticket.
or
Insert $1.25 cents more for a ticket.
Extra Credit! (5 points)
For an extra 5 points, make one more change. If the user inserts enough money to pay for more than one ticket, print as many tickets as the current balance can pay for, then print a message indicating the change. For example, if the cost of a ticket is 30 cents, and the user inserts a dollar, three tickets should be printed, and the user should be informed that there is 10 cents change.
What to Submit:
Since this assignment is just one class, I am not asking you to create a zip file. Rather, just find the file “TicketMachine.java” inside your project folder. Once you have completed your work, use Isidore to upload the file “TicketMachine.java”. Make sure you upload the file that ends in “.java”.
Note that in the Windows file explorer windows, you should go to the “File” menu and make sure the box labeled “file name extensions” is checked. Otherwise, you will not see the file name extensions such as “.java” or “.zip”.