|
@@ -18,28 +18,34 @@ required for a ticket. As this is a simple machine, a ticket will not be issued
|
18
|
18
|
so once you have inserted enough money, call the printTicket method. A
|
19
|
19
|
facsimile ticket should be printed in the BlueJ terminal window.
|
20
|
20
|
* Exercise 2.2 What value is returned if you check the machine’s balance after it
|
21
|
|
-has printed a ticket?
|
|
21
|
+has printed a ticket? 0
|
22
|
22
|
* Exercise 2.3 Experiment with inserting different amounts of money before printing
|
23
|
|
-tickets. Do you notice anything strange about the machine’s behavior? What happens
|
24
|
|
-if you insert too much money into the machine – do you receive any refund? What
|
25
|
|
-happens if you do not insert enough and then try to print a ticket?
|
|
23
|
+tickets. Do you notice anything strange about the machine’s behavior?
|
|
24
|
+It doesn't calculate change
|
|
25
|
+ What happens
|
|
26
|
+if you insert too much money into the machine – do you receive any refund? No What
|
|
27
|
+happens if you do not insert enough and then try to print a ticket? It prints the ticket regardless
|
26
|
28
|
* Exercise 2.4 Try to obtain a good understanding of a ticket machine’s behavior by
|
27
|
29
|
interacting with it on the object bench before we start looking at how the
|
28
|
30
|
TicketMachine class is implemented in the next section.
|
29
|
31
|
* Exercise 2.5 Create another ticket machine for tickets of a different price. Buy a
|
30
|
|
-ticket from that machine. Does the printed ticket look different?
|
|
32
|
+ticket from that machine. Does the printed ticket look different? only ticket price
|
31
|
33
|
|
32
|
34
|
* Exercise 2.6 Write out what you think the outer wrappers of the Student and
|
33
|
35
|
LabClass classes might look like – do not worry about the inner part.
|
|
36
|
+
|
|
37
|
+Public class Student () {}
|
|
38
|
+Public class LabClass() {}
|
|
39
|
+
|
34
|
40
|
* Exercise 2.7 Does it matter whether we write
|
35
|
41
|
`public class TicketMachine`
|
36
|
42
|
or
|
37
|
43
|
`class public TicketMachine`
|
38
|
44
|
in the outer wrapper of a class? Edit the source of the TicketMachine class to
|
39
|
45
|
make the change and then close the editor window. Do you notice a change in the
|
40
|
|
-class diagram?
|
|
46
|
+class diagram? Yes, the class shows a square pattern
|
41
|
47
|
What error message do you get when you now press the Compile button? Do you think
|
42
|
|
-this message clearly explains what is wrong?
|
|
48
|
+this message clearly explains what is wrong? Identifier expected, yes, it expected an identifier before the name of the class
|
43
|
49
|
* Exercise 2.8 Check whether or not it is possible to leave out the word public
|
44
|
50
|
from the outer wrapper of the TicketMachine class.
|
45
|
51
|
|
|
@@ -49,44 +55,65 @@ printTicket, for instance. Look at the class definition in Code 2.1 and use this
|
49
|
55
|
knowledge, along with the additional information about ordering we have given you,
|
50
|
56
|
to try to make a list of the names of the fields, constructors, and methods in the
|
51
|
57
|
TicketMachine class. Hint: There is only one constructor in the class.
|
|
58
|
+
|
|
59
|
+Fields : price, balance, total
|
|
60
|
+Constructor : Ticketmachine (ticket cost)
|
|
61
|
+Methods : getPrice, getBalance, insertMoney, printTicket
|
|
62
|
+
|
|
63
|
+
|
52
|
64
|
Exercise 2.10 Do you notice any features of the constructor that make it significantly
|
53
|
65
|
different from the other methods of the class?
|
54
|
66
|
|
|
67
|
+it doesn't have a return type in front of the constructor name
|
|
68
|
+
|
55
|
69
|
* Exercise 2.11 What do you think is the type of each of the following fields?
|
56
|
70
|
```
|
57
|
|
-private int count;
|
58
|
|
-private Student representative;
|
59
|
|
-private Server host;
|
|
71
|
+private int count; integer
|
|
72
|
+private Student representative; object
|
|
73
|
+private Server host; object
|
60
|
74
|
```
|
61
|
75
|
* Exercise 2.12 What are the names of the following fields?
|
62
|
76
|
```
|
63
|
|
-private boolean alive;
|
64
|
|
-private Person tutor;
|
65
|
|
-private Game game;
|
|
77
|
+private boolean alive; alive
|
|
78
|
+private Person tutor; tutor
|
|
79
|
+private Game game; game
|
66
|
80
|
```
|
67
|
81
|
* Exercise 2.13 In the following field declaration from the TicketMachine class
|
68
|
82
|
```
|
69
|
83
|
private int price;
|
70
|
84
|
```
|
71
|
|
-does it matter which order the three words appear in? Edit the TicketMachine class to
|
|
85
|
+does it matter which order the three words appear in? yes
|
|
86
|
+Edit the TicketMachine class to
|
72
|
87
|
try different orderings. After each change, close the editor. Does the appearance of the
|
73
|
88
|
class diagram after each change give you a clue as to whether or not other orderings are
|
74
|
|
-possible? Check by pressing the Compile button to see if there is an error message.
|
|
89
|
+possible? yes, only the original order works, the class diagram shows a chequered pattern and throws an error otherwise
|
|
90
|
+ Check by pressing the Compile button to see if there is an error message.
|
75
|
91
|
Make sure that you reinstate the original version after your experiments!
|
76
|
92
|
* Exercise 2.14 Is it always necessary to have a semicolon at the end of a field declaration?
|
|
93
|
+
|
|
94
|
+yes, because it identifies the statement as a complete java statement
|
|
95
|
+
|
77
|
96
|
Once again, experiment via the editor. The rule you will learn here is an
|
78
|
97
|
important one, so be sure to remember it.
|
79
|
98
|
* Exercise 2.15 Write in full the declaration for a field of type int whose name is
|
80
|
99
|
status.
|
81
|
|
-* Exercise 2.16 To what class does the following constructor belong?
|
|
100
|
+
|
|
101
|
+int status;
|
|
102
|
+
|
|
103
|
+* Exercise 2.16 To what class does the following constructor belong? Student because a constructor has the same name as the class in which they are defined
|
82
|
104
|
```
|
83
|
105
|
public Student(String name)
|
84
|
106
|
```
|
85
|
|
-* Exercise 2.17 How many parameters does the following constructor have and what are their types?
|
|
107
|
+* Exercise 2.17 How many parameters does the following constructor have and what are their types? 2, title of type String and price of type double
|
86
|
108
|
```
|
87
|
109
|
public Book(String title, double price)
|
88
|
110
|
```
|
89
|
111
|
* Exercise 2.18 Can you guess what types some of the Book class’s fields might be? Can you assume anything about the names of its fields?
|
90
|
112
|
|
|
113
|
+yes, there might be a field Material of type string, to distinguish between paperback, softcover or kindle
|
|
114
|
+another field Condition of type String could contain whether the book was used or new, available for sale or rental
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
91
|
118
|
Work all Exercises from 2.19 to 2.58 that are NOT marked *Challenge exercise*.
|
92
|
|
-READ upto and INCLUDING section 2.15 of this chapter.
|
|
119
|
+READ upto and INCLUDING section 2.15 of this chapter.
|