|
@@ -1,4 +1,5 @@
|
1
|
1
|
# NaiveTicket
|
|
2
|
+.
|
2
|
3
|
|
3
|
4
|
The second Objects lab,from the BlueJ book's second chapter.
|
4
|
5
|
|
|
@@ -17,20 +18,34 @@ insert multiple coins or notes into a real machine. Try inserting the exact amou
|
17
|
18
|
required for a ticket. As this is a simple machine, a ticket will not be issued automatically,
|
18
|
19
|
so once you have inserted enough money, call the printTicket method. A
|
19
|
20
|
facsimile ticket should be printed in the BlueJ terminal window.
|
|
21
|
+
|
20
|
22
|
* Exercise 2.2 What value is returned if you check the machine’s balance after it
|
21
|
23
|
has printed a ticket?
|
|
24
|
+
|
|
25
|
+*0*
|
|
26
|
+
|
22
|
27
|
* Exercise 2.3 Experiment with inserting different amounts of money before printing
|
23
|
28
|
tickets. Do you notice anything strange about the machine’s behavior? What happens
|
24
|
29
|
if you insert too much money into the machine – do you receive any refund? What
|
25
|
30
|
happens if you do not insert enough and then try to print a ticket?
|
|
31
|
+
|
|
32
|
+*You can put any amount including nothing and you get a ticket and you also get no change.*
|
|
33
|
+
|
26
|
34
|
* Exercise 2.4 Try to obtain a good understanding of a ticket machine’s behavior by
|
27
|
35
|
interacting with it on the object bench before we start looking at how the
|
28
|
36
|
TicketMachine class is implemented in the next section.
|
|
37
|
+
|
29
|
38
|
* Exercise 2.5 Create another ticket machine for tickets of a different price. Buy a
|
30
|
39
|
ticket from that machine. Does the printed ticket look different?
|
31
|
40
|
|
|
41
|
+*Just the price is different.*
|
|
42
|
+
|
32
|
43
|
* Exercise 2.6 Write out what you think the outer wrappers of the Student and
|
33
|
44
|
LabClass classes might look like – do not worry about the inner part.
|
|
45
|
+
|
|
46
|
+*public class Student
|
|
47
|
+public class LabClass*
|
|
48
|
+
|
34
|
49
|
* Exercise 2.7 Does it matter whether we write
|
35
|
50
|
`public class TicketMachine`
|
36
|
51
|
or
|
|
@@ -38,31 +53,67 @@ or
|
38
|
53
|
in the outer wrapper of a class? Edit the source of the TicketMachine class to
|
39
|
54
|
make the change and then close the editor window. Do you notice a change in the
|
40
|
55
|
class diagram?
|
41
|
|
-What error message do you get when you now press the Compile button? Do you think
|
|
56
|
+
|
|
57
|
+*Red error messages pop up and there are grid lines in the box of the class*
|
|
58
|
+
|
|
59
|
+What error message do you get when you now press the Compile button?
|
|
60
|
+
|
|
61
|
+*<identifier> expected*
|
|
62
|
+
|
|
63
|
+Do you think
|
42
|
64
|
this message clearly explains what is wrong?
|
|
65
|
+
|
|
66
|
+*Yes, it states that the public identifier is missing*
|
|
67
|
+
|
43
|
68
|
* Exercise 2.8 Check whether or not it is possible to leave out the word public
|
44
|
69
|
from the outer wrapper of the TicketMachine class.
|
45
|
70
|
|
|
71
|
+*It is, no error message.*
|
|
72
|
+
|
46
|
73
|
* Exercise 2.9 From your earlier experimentation with the ticket machine objects
|
47
|
74
|
within BlueJ you can probably remember the names of some of the methods –
|
48
|
75
|
printTicket, for instance. Look at the class definition in Code 2.1 and use this
|
49
|
76
|
knowledge, along with the additional information about ordering we have given you,
|
50
|
77
|
to try to make a list of the names of the fields, constructors, and methods in the
|
51
|
78
|
TicketMachine class. Hint: There is only one constructor in the class.
|
52
|
|
-Exercise 2.10 Do you notice any features of the constructor that make it significantly
|
|
79
|
+
|
|
80
|
+*Fields: price, balance, total
|
|
81
|
+Constructor: TicketMachine
|
|
82
|
+Methods: getPrice, getBalance, insertMoney, printTicket*
|
|
83
|
+
|
|
84
|
+* Exercise 2.10 Do you notice any features of the constructor that make it significantly
|
53
|
85
|
different from the other methods of the class?
|
54
|
86
|
|
|
87
|
+*It is the same name as the class and it does not have a return type listed.*
|
|
88
|
+
|
55
|
89
|
* Exercise 2.11 What do you think is the type of each of the following fields?
|
56
|
90
|
```
|
57
|
91
|
private int count;
|
|
92
|
+
|
|
93
|
+*an int variable*
|
|
94
|
+
|
58
|
95
|
private Student representative;
|
|
96
|
+
|
|
97
|
+*an object variable of class Student*
|
|
98
|
+
|
59
|
99
|
private Server host;
|
|
100
|
+
|
|
101
|
+*an object variable of class Server*
|
60
|
102
|
```
|
61
|
103
|
* Exercise 2.12 What are the names of the following fields?
|
62
|
104
|
```
|
63
|
105
|
private boolean alive;
|
|
106
|
+
|
|
107
|
+*alive*
|
|
108
|
+
|
64
|
109
|
private Person tutor;
|
|
110
|
+
|
|
111
|
+*tutor*
|
|
112
|
+
|
65
|
113
|
private Game game;
|
|
114
|
+
|
|
115
|
+*game*
|
|
116
|
+
|
66
|
117
|
```
|
67
|
118
|
* Exercise 2.13 In the following field declaration from the TicketMachine class
|
68
|
119
|
```
|
|
@@ -73,20 +124,246 @@ try different orderings. After each change, close the editor. Does the appearanc
|
73
|
124
|
class diagram after each change give you a clue as to whether or not other orderings are
|
74
|
125
|
possible? Check by pressing the Compile button to see if there is an error message.
|
75
|
126
|
Make sure that you reinstate the original version after your experiments!
|
|
127
|
+
|
|
128
|
+*int private price; it is clear there is an error before compiling.*
|
|
129
|
+
|
|
130
|
+
|
76
|
131
|
* Exercise 2.14 Is it always necessary to have a semicolon at the end of a field declaration?
|
77
|
132
|
Once again, experiment via the editor. The rule you will learn here is an
|
78
|
133
|
important one, so be sure to remember it.
|
|
134
|
+
|
|
135
|
+*yes, you need a ; at the end of a field declaration.*
|
|
136
|
+
|
79
|
137
|
* Exercise 2.15 Write in full the declaration for a field of type int whose name is
|
80
|
138
|
status.
|
|
139
|
+
|
|
140
|
+*private int status;*
|
|
141
|
+
|
81
|
142
|
* Exercise 2.16 To what class does the following constructor belong?
|
82
|
143
|
```
|
83
|
144
|
public Student(String name)
|
84
|
145
|
```
|
|
146
|
+*Class: Student*
|
|
147
|
+
|
85
|
148
|
* Exercise 2.17 How many parameters does the following constructor have and what are their types?
|
86
|
149
|
```
|
87
|
150
|
public Book(String title, double price)
|
88
|
151
|
```
|
|
152
|
+
|
|
153
|
+*2 parameters: a string and a double*
|
|
154
|
+
|
89
|
155
|
* 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
|
156
|
|
|
157
|
+*there will most likely be a string and a double field that have names similar to title and price.*
|
|
158
|
+
|
91
|
159
|
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.
|
|
160
|
+
|
|
161
|
+* 2.19
|
|
162
|
+*```
|
|
163
|
+public Pet(String petsName) {
|
|
164
|
+ name = petsName;
|
|
165
|
+}
|
|
166
|
+```*
|
|
167
|
+
|
|
168
|
+* 2.20
|
|
169
|
+*price has int in front of it, it's unnecessary since ticketCost is already defined as an int. This version will still compile, but when you create an object the price is not stored because adding int to price created a new different local variable than the original price variable.*
|
|
170
|
+
|
|
171
|
+* 2.21
|
|
172
|
+*the only difference is what they're returning, price vs balance.*
|
|
173
|
+
|
|
174
|
+* 2.22
|
|
175
|
+*How much money have I inserted?*
|
|
176
|
+
|
|
177
|
+* 2.23
|
|
178
|
+*No, but it might be confusing to read later since amount is referred to in a different method.*
|
|
179
|
+
|
|
180
|
+* 2.24
|
|
181
|
+*public int getTotal()
|
|
182
|
+ {
|
|
183
|
+ return total;
|
|
184
|
+ }*
|
|
185
|
+
|
|
186
|
+* 2.25
|
|
187
|
+*Error message: Missing return statement.*
|
|
188
|
+
|
|
189
|
+* 2.26
|
|
190
|
+*the return type: void vs int*
|
|
191
|
+
|
|
192
|
+* 2.27
|
|
193
|
+*No, because their return type is void which means they will not return anything.*
|
|
194
|
+
|
|
195
|
+* 2.29
|
|
196
|
+*We know it's a method and not a constructor because of the return value of void. Constructors don't have return values.*
|
|
197
|
+
|
|
198
|
+* 2.30
|
|
199
|
+```
|
|
200
|
+public void setPrice(int newPrice)
|
|
201
|
+ {
|
|
202
|
+ price = newPrice;
|
|
203
|
+ }
|
|
204
|
+ ```
|
|
205
|
+
|
|
206
|
+* 2.31
|
|
207
|
+```
|
|
208
|
+public void increase(int points)
|
|
209
|
+{
|
|
210
|
+ score = score + points;
|
|
211
|
+}
|
|
212
|
+```
|
|
213
|
+
|
|
214
|
+* 2.32
|
|
215
|
+```
|
|
216
|
+public void discount(int amount)
|
|
217
|
+{
|
|
218
|
+ price = price - discount;
|
|
219
|
+}
|
|
220
|
+```
|
|
221
|
+
|
|
222
|
+* 2.33
|
|
223
|
+```
|
|
224
|
+public void prompt()
|
|
225
|
+ {
|
|
226
|
+ System.out.println("Please insert the correct amount of money.");
|
|
227
|
+ }
|
|
228
|
+ ```
|
|
229
|
+
|
|
230
|
+* 2.34
|
|
231
|
+```
|
|
232
|
+public void showPrice()
|
|
233
|
+ {
|
|
234
|
+ System.out.println("The price of a ticket is " + price + " cents.");
|
|
235
|
+ }
|
|
236
|
+ ```
|
|
237
|
+
|
|
238
|
+* 2.35
|
|
239
|
+*Different outputs because each object has it's own price.*
|
|
240
|
+
|
|
241
|
+* 2.36
|
|
242
|
+*It would just write the word price instead of listing the value of the price since putting it in double quotations makes it a string.*
|
|
243
|
+
|
|
244
|
+* 2.37
|
|
245
|
+*same as above.*
|
|
246
|
+
|
|
247
|
+* 2.38
|
|
248
|
+*No, because both versions would not show a price value it would just say the word price.*
|
|
249
|
+
|
|
250
|
+* 2.39
|
|
251
|
+*Initially all objects you make will be the exact same (same state), but you could change the price of objects using newPrice which would make the objects different.*
|
|
252
|
+
|
|
253
|
+* 2.40
|
|
254
|
+```
|
|
255
|
+public void empty()
|
|
256
|
+ {
|
|
257
|
+ total = 0;
|
|
258
|
+ }
|
|
259
|
+ ```
|
|
260
|
+ *It does not need any parameters. It is a mutator.*
|
|
261
|
+
|
|
262
|
+* 2.41
|
|
263
|
+*This is a mutator.*
|
|
264
|
+
|
|
265
|
+* 2.42
|
|
266
|
+```
|
|
267
|
+public TicketMachine()
|
|
268
|
+{
|
|
269
|
+ price = 1000;
|
|
270
|
+ balance = 0;
|
|
271
|
+ total = 0;
|
|
272
|
+}
|
|
273
|
+
|
|
274
|
+public TicketMachine(int ticketPrice)
|
|
275
|
+{
|
|
276
|
+ price = ticketPrice;
|
|
277
|
+ balance = 0;
|
|
278
|
+ total = 0;
|
|
279
|
+}
|
|
280
|
+```
|
|
281
|
+
|
|
282
|
+* 2.43
|
|
283
|
+*No, the balance doesn't change when an error message is issued. If you input 0 there would be an error message because it's > than not >=.*
|
|
284
|
+
|
|
285
|
+* 2.44
|
|
286
|
+*If you input 0 you will no longer get an error, the balance will simply remain at 0.*
|
|
287
|
+
|
|
288
|
+* 2.45
|
|
289
|
+*isVisible was the boolean, which is appropriate since there are only two options, visible and not visible.*
|
|
290
|
+
|
|
291
|
+* 2.46
|
|
292
|
+*Before printing a ticket it confirms enough money has been paid otherwise it lets them know how much more needs to be paid*
|
|
293
|
+
|
|
294
|
+* 2.47
|
|
295
|
+*No, the balance could not be negative because the conditional statement does not allow a ticket to be printed unless there is sufficient funds.*
|
|
296
|
+
|
|
297
|
+* 2.49
|
|
298
|
+```
|
|
299
|
+int discount;
|
|
300
|
+int saving = price * discount;
|
|
301
|
+```
|
|
302
|
+
|
|
303
|
+* 2.50
|
|
304
|
+```
|
|
305
|
+int count;
|
|
306
|
+int mean = total / count;
|
|
307
|
+```
|
|
308
|
+
|
|
309
|
+* 2.51 & 2.52
|
|
310
|
+```
|
|
311
|
+public void checkBudget(int budget) {
|
|
312
|
+ if(price > budget) {
|
|
313
|
+ System.out.println("Too expensive! Your budget is only " + budget);
|
|
314
|
+ } else {
|
|
315
|
+ System.out.println("Just Right!");
|
|
316
|
+ }
|
|
317
|
+}
|
|
318
|
+```
|
|
319
|
+
|
|
320
|
+* 2.53
|
|
321
|
+*It would always return 0 since it is returning the balance which is set to 0 at the start of the method.*
|
|
322
|
+
|
|
323
|
+* 2.54
|
|
324
|
+*return has to be the final command.*
|
|
325
|
+
|
|
326
|
+* 2.55
|
|
327
|
+```
|
|
328
|
+public int emptyMachine()
|
|
329
|
+ {
|
|
330
|
+ int finalTotal = total;
|
|
331
|
+ total = 0;
|
|
332
|
+ return finalTotal;
|
|
333
|
+ }
|
|
334
|
+ ```
|
|
335
|
+
|
|
336
|
+* 2.56
|
|
337
|
+*It's both, it returns the value of the total as well as then resetting the total.*
|
|
338
|
+
|
|
339
|
+* 2.57
|
|
340
|
+```
|
|
341
|
+public void printTicket()
|
|
342
|
+ {
|
|
343
|
+ //New local variable
|
|
344
|
+ int amountLeftToPay = price - balance;
|
|
345
|
+
|
|
346
|
+ if(amountLeftToPay <= 0) {
|
|
347
|
+ System.out.println("##################");
|
|
348
|
+ System.out.println("# The BlueJ Line");
|
|
349
|
+ System.out.println("# Ticket");
|
|
350
|
+ System.out.println("# " + price + " cents.");
|
|
351
|
+ System.out.println("##################");
|
|
352
|
+ System.out.println();
|
|
353
|
+
|
|
354
|
+ // Update the total collected with the balance.
|
|
355
|
+ total = total + balance;
|
|
356
|
+ // Update the balance.
|
|
357
|
+ balance = balance - price;
|
|
358
|
+ } else {
|
|
359
|
+ System.out.println("You must insert at least: " + amountLeftToPay + " cents.");
|
|
360
|
+
|
|
361
|
+ }
|
|
362
|
+}
|
|
363
|
+```
|
|
364
|
+
|
|
365
|
+* 2.58
|
|
366
|
+*It depends how many ticket options you want. The more ticket options the more that would need to be changed.*
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+READ upto and INCLUDING section 2.15 of this chapter.
|