All of these questions deal with the ticket machine example bundled in this repo. You should fork this repo and clone the fork to work on the code locally.
public void setPrice(int cost)
Complete the body of the setPrice
method so that it assigns the value of its parameter to the price field. Write your new method in the lab04-ticket-machine
.
Complete the body of the following method, whose purpose is to add the value of its parameter to a field named score
.
/**
* Increase score by the given number of points.
*/
public void increase(int points)
{
...
}
Complete the following method, whose purpose is to subtract the value of its parameter from a field named price
. Add your new method to the lab04-ticket-machine
.
/**
* Reduce price by the given amount.
*/
public void discount(int amount)
{
...
}
System.out.println("My cat has green eyes.");
Add a method called prompt
to the TicketMachine
class in the lab04-ticket-machine
. This should have a void
return type and take no parameters. The body of the method should print the following single line of output:
Please insert the correct amount of money.
What do you think would be printed if you altered the fourth statement of printTicket
so that price
also has quotes around it, as follows?
System.out.println("# " + "price" + " cents.");
System.out.println("# price cents.");
Could either of the previous two versions be used to show the price of tickets in different ticket machines? Explain your answer.
Add a showPrice
method to the TicketMachine
class in the lab04-ticket-machine
. This should have a void return type and take no parameters. The body of the method should print (here xyz
should be replaced by the value held in the price
field when the method is called):
The price of a ticket is xyz cents.