Objects First With Java - Solutions

Jun 16, 2006 ... If too much money is inserted the machine takes it all - no refund. If there isn't
enough money inserted, it still prints out the ticket. Exercise 2.5. It looks the same.
Only the price on the ticket is different. Exercise 2.6. The outer part of the student
class: public class Student {}. The outer part of the LabClass class:.

Part of the document



|Exercise 2.2 |
|Zero. |
|Exercise 2.3 |
|If too much money is inserted the machine takes it all - no |
|refund. |
|If there isn't enough money inserted, it still prints out the |
|ticket. |
|Exercise 2.5 |
|It looks the same. Only the price on the ticket is different. |
|Exercise 2.6 |
|The outer part of the student class: |
|public class Student {} |
|The outer part of the LabClass class: |
|public class LabClass{} |
|Exercise 2.7 |
|Yes, the order of public and class matters. |
|Exercise 2.8 |
|It is possible to leave out the word public. |
|Exercise 2.9 |
| |
|Fields: |
|price |
|balance |
|total |
|Constructors: |
|TicketMachine |
|Methods: |
|getPrice |
|getBalance |
|insertMoney |
|printTicket |
|Exercise 2.10 |
| |
|It does not have any return type. The name of the constructor is |
|the same as the name of the class. |
|Exercise 2.11 |
| |
|int |
|Student |
|Server |
|Exercise 2.12 |
| |
|alive |
|tutor |
|game |
|Exercise 2.13 |
| |
|The order matters. |
|Exercise 2.14 |
| |
|Yes, it always necessary to have a semocolon after a field |
|declaration. |
|Exercise 2.15 |
|private int status; |
|Exercise 2.16 |
|It belongs to the class Student. |
|Exercise 2.17 |
|2 |
|Exercise 2.18 |
|It would be reasonable to expect the types to be the same as the |
|two parameters (String and double). |
|Can't really assume anything about the names, but probably |
|something like title and price. |
|Exercise 2.19 |
|name = petsName; |
|Exercise 2.20 |
|The price variable is declared in the constructor (it has an int |
|in front). This means that we assign the ticketCost to the local |
|variable price instead of the field price. |
|Exercise 2.21 |
| |
|The only difference is that getPrice() returns the price and |
|getBalance returns the balance |
|Exercise 2.22 |
| |
|How much money have I inserted into the machine? |
|Exercise 2.23 |
| |
|No. |
|Exercise 2.24 |
|public int getTotal() { return total;} |
|Exercise 2.25 |
| |
|Missing return statement. |
|Exercise 2.26 |
| |
|The signature for getPrice() has an int as return type. |
|The signature for printTicket has void as return type. |
|Exercise 2.27 |
| |
|No. |
|Because they don't need to return anything. |
|Yes. Both headers has void as return types. |
|Exercise 2.29 |
|It has a return type (void) and constructors do not have return |
|types. |
|Exercise 2.30 |
|price = ticketPrice; |
|Exercise 2.31 |
|score = score + points; |
|Exercise 2.32 |
|price = price - amount; |
|Exercise 2.33 |
|public void prompt() { |
|System.out.println("Please insert the correct amount of money."); |
|} |
|Exercise 2.34 |
|public void showPrice() { |
|System.out.println("The price of a ticket is " + price + " |
|cents"); |
|} |
|Exercise 2.35 |
| |
|They display different prices. This is because each ticketmachine |
|object has its own price. |
|The price that was set in one ticketmachine does not affect the |
|other ticketmachines price. |
|Exercise 2.36 |
| |
|Instead of printing out the actual price of the ticket, it just |
|displays the word "price". |
|Exercise 2.37 |
| |
|Prints out the exact same string as in exercise 2.36 |
|Exercise 2.38 |
| |
|No. None of them actually displays the price of the ticket. |
|Exercise 2.39 |
|public TicketMachine(){ |
|price = 1000; |
|balance = 0; |
|total = 0; |
|} |
| |
|The tickets always have a price of 1000. |
|Exercise 2.40 |
|public void empty() { |
|total = 0; |
|} |
| |
|It needs no parameters. |
|It is a mutator. |
|Exercise 2.41 |
|public void setPrice(int newPrice) { |
|price = newPrice;