|
@@ -19,18 +19,27 @@ 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
|
21
|
has printed a ticket?
|
|
22
|
+ - 0 is returned as the balance
|
|
23
|
+
|
22
|
24
|
* Exercise 2.3 Experiment with inserting different amounts of money before printing
|
23
|
25
|
tickets. Do you notice anything strange about the machine’s behavior? What happens
|
24
|
26
|
if you insert too much money into the machine – do you receive any refund? What
|
25
|
27
|
happens if you do not insert enough and then try to print a ticket?
|
|
28
|
+ - no refund when I insert too much.
|
|
29
|
+ - ticket still prints when you insert too little.
|
|
30
|
+
|
26
|
31
|
* Exercise 2.4 Try to obtain a good understanding of a ticket machine’s behavior by
|
27
|
32
|
interacting with it on the object bench before we start looking at how the
|
28
|
33
|
TicketMachine class is implemented in the next section.
|
29
|
34
|
* Exercise 2.5 Create another ticket machine for tickets of a different price. Buy a
|
30
|
35
|
ticket from that machine. Does the printed ticket look different?
|
|
36
|
+ - it acts exactly the same, except the price has changed, thus printing the new price when I print ticket.
|
31
|
37
|
|
32
|
38
|
* Exercise 2.6 Write out what you think the outer wrappers of the Student and
|
33
|
39
|
LabClass classes might look like – do not worry about the inner part.
|
|
40
|
+ - public class Student
|
|
41
|
+ - public class LabClass
|
|
42
|
+
|
34
|
43
|
* Exercise 2.7 Does it matter whether we write
|
35
|
44
|
`public class TicketMachine`
|
36
|
45
|
or
|
|
@@ -38,10 +47,16 @@ or
|
38
|
47
|
in the outer wrapper of a class? Edit the source of the TicketMachine class to
|
39
|
48
|
make the change and then close the editor window. Do you notice a change in the
|
40
|
49
|
class diagram?
|
|
50
|
+ - yes, it does matter. I received an error when compiling.
|
|
51
|
+
|
41
|
52
|
What error message do you get when you now press the Compile button? Do you think
|
42
|
53
|
this message clearly explains what is wrong?
|
|
54
|
+ - "<identifier> expected"
|
|
55
|
+ - the error doesn't specifically say, "you switched it around and you need to switch it back", but it's letting me know that it's expecting something that's not there - because i put it where it shouldn't be
|
|
56
|
+
|
43
|
57
|
* Exercise 2.8 Check whether or not it is possible to leave out the word public
|
44
|
58
|
from the outer wrapper of the TicketMachine class.
|
|
59
|
+ - yes, it is possible
|
45
|
60
|
|
46
|
61
|
* Exercise 2.9 From your earlier experimentation with the ticket machine objects
|
47
|
62
|
within BlueJ you can probably remember the names of some of the methods –
|
|
@@ -49,14 +64,23 @@ printTicket, for instance. Look at the class definition in Code 2.1 and use this
|
49
|
64
|
knowledge, along with the additional information about ordering we have given you,
|
50
|
65
|
to try to make a list of the names of the fields, constructors, and methods in the
|
51
|
66
|
TicketMachine class. Hint: There is only one constructor in the class.
|
|
67
|
+ - Fields: price, balance, total
|
|
68
|
+ - Constructors: TicketMachine(ticketCost)
|
|
69
|
+ - Methods: getPrice(), getBalance(), insertMoney(amount), printTicket()
|
|
70
|
+
|
52
|
71
|
Exercise 2.10 Do you notice any features of the constructor that make it significantly
|
53
|
72
|
different from the other methods of the class?
|
|
73
|
+ - constructor carries the same name as the class (by definition)
|
|
74
|
+ - constructor is the only method to initialize variables
|
54
|
75
|
|
55
|
76
|
* Exercise 2.11 What do you think is the type of each of the following fields?
|
56
|
77
|
```
|
57
|
78
|
private int count;
|
|
79
|
+ - integer
|
58
|
80
|
private Student representative;
|
|
81
|
+ - object of class Student, titled "representative"
|
59
|
82
|
private Server host;
|
|
83
|
+ - object of class Server, titled "host"
|
60
|
84
|
```
|
61
|
85
|
* Exercise 2.12 What are the names of the following fields?
|
62
|
86
|
```
|
|
@@ -64,6 +88,10 @@ private boolean alive;
|
64
|
88
|
private Person tutor;
|
65
|
89
|
private Game game;
|
66
|
90
|
```
|
|
91
|
+ - "alive"
|
|
92
|
+ - "tutor"
|
|
93
|
+ - "game"
|
|
94
|
+
|
67
|
95
|
* Exercise 2.13 In the following field declaration from the TicketMachine class
|
68
|
96
|
```
|
69
|
97
|
private int price;
|
|
@@ -73,20 +101,80 @@ try different orderings. After each change, close the editor. Does the appearanc
|
73
|
101
|
class diagram after each change give you a clue as to whether or not other orderings are
|
74
|
102
|
possible? Check by pressing the Compile button to see if there is an error message.
|
75
|
103
|
Make sure that you reinstate the original version after your experiments!
|
|
104
|
+ - yes, it matters. the access modifier must come before the type, which comes before the field name.
|
|
105
|
+
|
76
|
106
|
* Exercise 2.14 Is it always necessary to have a semicolon at the end of a field declaration?
|
77
|
107
|
Once again, experiment via the editor. The rule you will learn here is an
|
78
|
108
|
important one, so be sure to remember it.
|
|
109
|
+ - yes. IDE message says " \r;\r expected"
|
|
110
|
+
|
79
|
111
|
* Exercise 2.15 Write in full the declaration for a field of type int whose name is
|
80
|
112
|
status.
|
81
|
113
|
* Exercise 2.16 To what class does the following constructor belong?
|
82
|
114
|
```
|
83
|
115
|
public Student(String name)
|
84
|
116
|
```
|
|
117
|
+ - belongs to Student class. By definition, constructors are named after their classes
|
85
|
118
|
* Exercise 2.17 How many parameters does the following constructor have and what are their types?
|
86
|
119
|
```
|
87
|
120
|
public Book(String title, double price)
|
88
|
121
|
```
|
|
122
|
+ - two parameters of types String and double, respectively
|
89
|
123
|
* 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?
|
|
124
|
+ - String: book name, int: number of pages, String/int: serial number, string: author
|
90
|
125
|
|
91
|
126
|
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.
|
|
127
|
+- 2.19 Exercise
|
|
128
|
+ - publicPet(String petsName){
|
|
129
|
+ name = petsName;
|
|
130
|
+ }
|
|
131
|
+- 2.20 Exercise
|
|
132
|
+ - what's wrong with the code below is that it declares price <strong> within</strong> the constructor. price will no longer be available for use when the constructor completes its code.
|
|
133
|
+ <pre><code>public TicketMachine(int ticketCost) {
|
|
134
|
+ int price = ticketCost;
|
|
135
|
+ balance = 0;
|
|
136
|
+ total = 0;
|
|
137
|
+ </code></pre>
|
|
138
|
+ }
|
|
139
|
+ - in the case of implementing this constructor within <em>naive-ticket-machine</em>, it appears to be <strong>re</strong>declaring <q>price</q>. Thus, perhaps, a new price variable is created and within the scope of the constructor. While the global price that is called by <em>getPrice()</em> remains 0.
|
|
140
|
+ - 2.21 Exercise
|
|
141
|
+ -one returns balance one returns price;
|
|
142
|
+ - 2.22
|
|
143
|
+ - "How much money has been inserted thus far"
|
|
144
|
+ - 2.23
|
|
145
|
+ - no
|
|
146
|
+ - 2.25
|
|
147
|
+ - "Missing return statement"
|
|
148
|
+ - 2.26
|
|
149
|
+ - getPrice() has return type <em>int</em>, whereas printTicket() has no return type (void).
|
|
150
|
+ - 2.27
|
|
151
|
+ - they have a return type of <em>void</em>
|
|
152
|
+ - 2.29
|
|
153
|
+ - because it doesn't have the same name as the class
|
|
154
|
+ - 2.30
|
|
155
|
+ <pre><code>public void setPrice(int ticketCost){
|
|
156
|
+ price = ticketCost;
|
|
157
|
+ }</code></pre>
|
|
158
|
+ - 2.31
|
|
159
|
+ <pre><code> public void increase(int points){
|
|
160
|
+ score = points;
|
|
161
|
+ }</code></pre>
|
|
162
|
+ - 2.32
|
|
163
|
+ <pre><code> pbulic void discount(int amount){
|
|
164
|
+ price = price - amount;
|
|
165
|
+ }</code></pre>
|
|
166
|
+ - 2.36
|
|
167
|
+ - <code>System.out.println("# \"" + price + "\" cents.");</code>
|
|
168
|
+ - 2.37
|
|
169
|
+ - <code>System.out.println("# price cents.");</code>
|
|
170
|
+ - the above will print everything within the quotes as is.
|
|
171
|
+ - <code>System.out.println("# " + "price" + " cents.");</code>
|
|
172
|
+ - this <strong>HERE</strong> will print the same as the coded bullet above it.
|
|
173
|
+ - 2.38
|
|
174
|
+ - neither of them, because <em>price</em> is not inserted as a variable, but rather a <strong><em>String</em></strong>
|
|
175
|
+ - 2.39
|
|
176
|
+ - by not having a parameter in the constructor, there is no dialog upon calling the constructor to set the ticket price.
|
|
177
|
+ - 2.4
|
|
178
|
+ - <em>empty()</em> method is a mutator
|
|
179
|
+
|
|
180
|
+READ upto and INCLUDING section 2.15 of this chapter.
|