Chapter 2 of the BlueJ book. Walk through many of the chapters exercises.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. Responses
  2. Exercise 2.29 How can we tell from just its header that setPrice is a method and not a constructor?
  3. public void setPrice(int ticketCost)
  4. Has a void.
  5. Takes a parameter.
  6. Exercise 2.30 Complete the body of the setPrice method so that it assigns the
  7. value of its parameter to the price field.
  8. public void setPrice(int ticketCost){
  9. price = ticketCost;
  10. }
  11. Exercise 2.31 Complete the body of the following method, whose purpose is to
  12. add the value of its parameter to a field named score.
  13. /**
  14. * Increase score by the given number of points.
  15. */
  16. public void increase(int points)
  17. {
  18. points += points;
  19. }
  20. 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?
  21. /**
  22. * Reduce price by the given amount.
  23. */
  24. public void discount(int amount)
  25. {
  26. price -= amount;
  27. ... }
  28. 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:
  29. Please insert the correct amount of money.
  30. 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:
  31. The price of a ticket is xyz cents.
  32. where xyz should be replaced by the value held in the price field when the method
  33. is called.
  34. 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?
  35. 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?
  36. System.out.println("# " + "price" + " cents.");
  37. Exercise 2.37 What about the following version? System.out.println("# price cents.");
  38. 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.