12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- Responses
-
- Exercise 2.29 How can we tell from just its header that setPrice is a method and not a constructor?
- public void setPrice(int ticketCost)
-
- Has a void.
- Takes a parameter.
-
-
-
-
- Exercise 2.30 Complete the body of the setPrice method so that it assigns the
- value of its parameter to the price field.
-
- public void setPrice(int ticketCost){
- price = ticketCost;
- }
-
-
-
-
- Exercise 2.31 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)
- {
- points += points;
-
- }
-
-
-
-
- Exercise 2.32 Can you complete the following method, whose purpose is to sub- tract the value of its parameter from a field named price?
- /**
- * Reduce price by the given amount.
- */
- public void discount(int amount)
- {
-
- price -= amount;
-
- ... }
-
-
- Exercise 2.33 Add a method called prompt to the TicketMachine class. This should have a void return type and take no parameters. The body of the method should print something like:
- Please insert the correct amount of money.
-
-
-
- Exercise 2.34 Add a showPrice method to the TicketMachine class. This should have a void return type and take no parameters. The body of the method should print something like:
- The price of a ticket is xyz cents.
- where xyz should be replaced by the value held in the price field when the method
- is called.
-
-
-
- Exercise 2.35 Create two ticket machines with differently priced tickets. Do calls to their showPrice methods show the same output, or different? How do you explain this effect?
-
-
- Exercise 2.36 What do you think would be printed if you altered the fourth state- ment of printTicket so that price also has quotes around it, as follows?
- System.out.println("# " + "price" + " cents.");
-
-
-
-
- Exercise 2.37 What about the following version? System.out.println("# price cents.");
-
-
-
- Exercise 2.38 Could either of the previous two versions be used to show the price of tickets in different ticket machines? Explain your answer.
-
-
|