Objects First With Java - Solutions - Easy semester!

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 almost completely the same. Only the price on the ticket is
different. Exercise 2.6. The outer part of the student class: public class Student

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 almost completely 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 |
|It is not possible to leave out the word class. |
|Exercise 2.10 |
| |
|Fields: |
|price |
|balance |
|total |
|Constructors: |
|TicketMachine |
|Methods: |
|getPrice |
|getBalance |
|insertMoney |
|printTicket |
|Exercise 2.11 |
| |
|It does not have any return type. The name of the constructor is |
|the same as the name of the class. |
|Exercise 2.12 |
| |
|int |
|Student |
|Server |
|Exercise 2.13 |
| |
|alive |
|tutor |
|game |
|Exercise 2.14 |
|Student, Server, Person and Game |
|Exercise 2.15 |
| |
|The exact order matters. |
|Exercise 2.16 |
| |
|Yes, it always necessary to have a semicolon after a field |
|declaration. |
|Exercise 2.17 |
|private int status; |
|Exercise 2.18 |
|It belongs to the class Student. |
|Exercise 2.19 |
|It has two parameters. One is of type String and the other of type|
|double. |
|Exercise 2.20 |
|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.21 |
|name = petsName; |
|Exercise 2.22 |
|public Date(String month, int day, int year) |
|Exercise 2.23 |
| |
|Aside from their names, the only difference is that getPrice() |
|returns the value of the price field whereas getBalance() returns |
|the value of the balance field. |
|Exercise 2.24 |
| |
|How much money have I inserted into the machine? |
|Exercise 2.25 |
| |
|No. There is no direct link between the name of a method and the |
|name of the field. However, it is a convention to use a name for |
|the method that clearly links it to the field. |
|Exercise 2.26 |
|public int getTotal() |
|{ |
|return total; |
|} |
|Exercise 2.27 |
| |
|Missing return statement. |
|Exercise 2.28 |
| |
|The header for getPrice() has an int as return type. |
|The header for printTicket() has void as return type. |
|Exercise 2.29 |
| |
|No. |
|Because they don't need to return anything. |
|Yes. Both headers have void as return types. |
|Exercise 2.31 |
|It has a return type (void) and constructors do not have return |
|types. It also does not have the same name as the class it is in. |
|Exercise 2.32 |
|price = cost; |
|Exercise 2.33 |
|score = score + points; |
|Exercise 2.34 |
|It is a mutator. |
|Use an inspector to view the current score, then call the increase|
|method with a positive parameter value and observe that score |
|increases by that value. |
|Alternatively, if score has an accessor method; call the accessor,|
|then call increase, and then call the accessor once again to |
|verify that it returns the updated value, indicating that the |
|field has been modified. |
|Exercise 2.35 |
|price = price - amount; |
|Exercise 2.36 |
|Note that no quote marks are printed, just the following: |
| |
|My cat has green eyes. |
|Exercise 2.37 |
|public void prompt() |
|{ |
|System.out.println("Please insert the correct amount of money."); |
|} |
|Exercise 2.38 |
| |
|Instead of printing out the actual price of the ticket, it |
|displays the word "price": |
| |
|# price cents. |
|Exercise 2.39 |
|