|
@@ -1,92 +1,267 @@
|
1
|
|
-# NaiveTicket
|
2
|
|
-
|
3
|
|
-The second Objects lab,from the BlueJ book's second chapter.
|
4
|
|
-
|
5
|
|
-Look for the [Chapter 2 file](./doc/BlueJ-objects-first-ch2.pdf) you need in the [doc](./doc) folder. There is 35 pages of reading and exercises in the chapter.
|
6
|
|
-
|
7
|
|
-Work through all these exercises. You edit this file with your answers for these exercises.
|
8
|
|
-
|
9
|
|
-* Exercise 2.1 Create a TicketMachine object on the object bench and take a look
|
10
|
|
-at its methods. You should see the following: getBalance, getPrice, insertMoney,
|
11
|
|
-and printTicket. Try out the getPrice method. You should see a return value containing
|
12
|
|
-the price of the tickets that was set when this object was created. Use the
|
13
|
|
-insertMoney method to simulate inserting an amount of money into the machine and
|
14
|
|
-then use getBalance to check that the machine has a record of the amount inserted.
|
15
|
|
-You can insert several separate amounts of money into the machine, just like you might
|
16
|
|
-insert multiple coins or notes into a real machine. Try inserting the exact amount
|
17
|
|
-required for a ticket. As this is a simple machine, a ticket will not be issued automatically,
|
18
|
|
-so once you have inserted enough money, call the printTicket method. A
|
19
|
|
-facsimile ticket should be printed in the BlueJ terminal window.
|
20
|
|
-* Exercise 2.2 What value is returned if you check the machine’s balance after it
|
21
|
|
-has printed a ticket?
|
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?
|
26
|
|
-* Exercise 2.4 Try to obtain a good understanding of a ticket machine’s behavior by
|
27
|
|
-interacting with it on the object bench before we start looking at how the
|
28
|
|
-TicketMachine class is implemented in the next section.
|
29
|
|
-* 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?
|
31
|
|
-
|
32
|
|
-* Exercise 2.6 Write out what you think the outer wrappers of the Student and
|
33
|
|
-LabClass classes might look like – do not worry about the inner part.
|
34
|
|
-* Exercise 2.7 Does it matter whether we write
|
35
|
|
-`public class TicketMachine`
|
36
|
|
-or
|
37
|
|
-`class public TicketMachine`
|
38
|
|
-in the outer wrapper of a class? Edit the source of the TicketMachine class to
|
39
|
|
-make the change and then close the editor window. Do you notice a change in the
|
40
|
|
-class diagram?
|
41
|
|
-What error message do you get when you now press the Compile button? Do you think
|
42
|
|
-this message clearly explains what is wrong?
|
43
|
|
-* Exercise 2.8 Check whether or not it is possible to leave out the word public
|
44
|
|
-from the outer wrapper of the TicketMachine class.
|
45
|
|
-
|
46
|
|
-* Exercise 2.9 From your earlier experimentation with the ticket machine objects
|
47
|
|
-within BlueJ you can probably remember the names of some of the methods –
|
48
|
|
-printTicket, for instance. Look at the class definition in Code 2.1 and use this
|
49
|
|
-knowledge, along with the additional information about ordering we have given you,
|
50
|
|
-to try to make a list of the names of the fields, constructors, and methods in the
|
51
|
|
-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
|
53
|
|
-different from the other methods of the class?
|
54
|
|
-
|
55
|
|
-* Exercise 2.11 What do you think is the type of each of the following fields?
|
56
|
|
-```
|
57
|
|
-private int count;
|
58
|
|
-private Student representative;
|
59
|
|
-private Server host;
|
60
|
|
-```
|
61
|
|
-* Exercise 2.12 What are the names of the following fields?
|
62
|
|
-```
|
63
|
|
-private boolean alive;
|
64
|
|
-private Person tutor;
|
65
|
|
-private Game game;
|
66
|
|
-```
|
67
|
|
-* Exercise 2.13 In the following field declaration from the TicketMachine class
|
68
|
|
-```
|
69
|
|
-private int price;
|
70
|
|
-```
|
71
|
|
-does it matter which order the three words appear in? Edit the TicketMachine class to
|
72
|
|
-try different orderings. After each change, close the editor. Does the appearance of the
|
73
|
|
-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.
|
75
|
|
-Make sure that you reinstate the original version after your experiments!
|
76
|
|
-* Exercise 2.14 Is it always necessary to have a semicolon at the end of a field declaration?
|
77
|
|
-Once again, experiment via the editor. The rule you will learn here is an
|
78
|
|
-important one, so be sure to remember it.
|
79
|
|
-* Exercise 2.15 Write in full the declaration for a field of type int whose name is
|
80
|
|
-status.
|
81
|
|
-* Exercise 2.16 To what class does the following constructor belong?
|
82
|
|
-```
|
83
|
|
-public Student(String name)
|
84
|
|
-```
|
85
|
|
-* Exercise 2.17 How many parameters does the following constructor have and what are their types?
|
86
|
|
-```
|
87
|
|
-public Book(String title, double price)
|
88
|
|
-```
|
89
|
|
-* 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
|
|
-
|
91
|
|
-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.
|
|
1
|
+Naive Ticket Exercise's
|
|
2
|
+
|
|
3
|
+Exercise 2.1
|
|
4
|
+Requested price as argument for parameter upon creating new instance of TicketMachine
|
|
5
|
+getPrice returns price entered upon initialization of object
|
|
6
|
+insertMoney requests int argument for its parameter
|
|
7
|
+getBalance returns int equal to amount entered into insertMoney's parameter
|
|
8
|
+printTicket prints price of ticket and sets balance field to 0
|
|
9
|
+
|
|
10
|
+Exercise 2.2
|
|
11
|
+getBalance method returns 0 after printTicket method is called
|
|
12
|
+
|
|
13
|
+Exercise 2.3
|
|
14
|
+Naive TicketMachine does not validate correct amount entered, each call to printTicket will print a ticket with the listed price and set balance to 0
|
|
15
|
+
|
|
16
|
+Exercise 2.4
|
|
17
|
+The constructor requires input to set price = ticketCost
|
|
18
|
+Balance and total are set to 0 upon initialization
|
|
19
|
+
|
|
20
|
+Exercise 2.5
|
|
21
|
+The only difference is the price printed on the ticket
|
|
22
|
+
|
|
23
|
+Exercise 2.6
|
|
24
|
+?
|
|
25
|
+
|
|
26
|
+Exercise 2.7
|
|
27
|
+Yes, order matters. Public modifier must come before class, will not successfully compile if reversed.
|
|
28
|
+
|
|
29
|
+Exercise 2.8
|
|
30
|
+Yes, there are no syntax errors if public modifier is not present
|
|
31
|
+
|
|
32
|
+Exercise 2.9
|
|
33
|
+Constructors have no return type, they do not require a public modifier and can only be run when object is created
|
|
34
|
+
|
|
35
|
+Public class TicketMachine
|
|
36
|
+{
|
|
37
|
+ private int balance;
|
|
38
|
+ private int price;
|
|
39
|
+ private int deposit;
|
|
40
|
+
|
|
41
|
+ public TicketMachine(int ticketCost)
|
|
42
|
+ {
|
|
43
|
+ price = ticketCost;
|
|
44
|
+ balance = 0;
|
|
45
|
+ deposit = 0;
|
|
46
|
+ }
|
|
47
|
+ public void printTicket()
|
|
48
|
+ {
|
|
49
|
+ System.out.println(*****);
|
|
50
|
+ System.out.println("Your ticket price is:");
|
|
51
|
+ System.out.println(price + " cents.");
|
|
52
|
+
|
|
53
|
+ balance = 0;
|
|
54
|
+ }
|
|
55
|
+ public void setBalance(int newBalance)
|
|
56
|
+ {
|
|
57
|
+ balance = newBalance;
|
|
58
|
+ }
|
|
59
|
+ public int getBalance()
|
|
60
|
+ {
|
|
61
|
+ return balance;
|
|
62
|
+ }
|
|
63
|
+ public int getPrice()
|
|
64
|
+ {
|
|
65
|
+ return price;
|
|
66
|
+ }
|
|
67
|
+}
|
|
68
|
+
|
|
69
|
+Exercise 2.11
|
|
70
|
+Int
|
|
71
|
+Student
|
|
72
|
+Server
|
|
73
|
+
|
|
74
|
+Exercise 2.12
|
|
75
|
+Alive
|
|
76
|
+Tutor
|
|
77
|
+game
|
|
78
|
+
|
|
79
|
+Exercise 2.13
|
|
80
|
+Yes order matters, modifier + type + name
|
|
81
|
+
|
|
82
|
+Exercise 2.14
|
|
83
|
+It is not always necessary to have a semicolon at the end of a statement but is bad syntax to not include
|
|
84
|
+
|
|
85
|
+Exercise 2.15
|
|
86
|
+Private int name;
|
|
87
|
+
|
|
88
|
+Exercise 2.16
|
|
89
|
+Student
|
|
90
|
+
|
|
91
|
+Exercise 2.17
|
|
92
|
+Two parameters of type String and double
|
|
93
|
+
|
|
94
|
+Exercise 2.18
|
|
95
|
+?
|
|
96
|
+
|
|
97
|
+Work all Exercises from 2.19 to 2.58 that are NOT marked Challenge exercise. READ upto and INCLUDING section 2.15 of this chapter.
|
|
98
|
+
|
|
99
|
+Exercise 2.19
|
|
100
|
+Two parameters of type String and double
|
|
101
|
+
|
|
102
|
+Exercise 2.20
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+Exercise 2.21
|
|
106
|
+Public Pet(String petsName)
|
|
107
|
+{
|
|
108
|
+ name = petsName;
|
|
109
|
+}
|
|
110
|
+
|
|
111
|
+Exercise 2.23
|
|
112
|
+Only difference is the field they interact with
|
|
113
|
+
|
|
114
|
+Exercise 2.24
|
|
115
|
+Tickets cost value set when object created
|
|
116
|
+getBalance is total amount entered into machine before calling getTicket
|
|
117
|
+
|
|
118
|
+Exercise 2.25
|
|
119
|
+No, name of method being changed does not effect return statement
|
|
120
|
+
|
|
121
|
+Exercise 2.26
|
|
122
|
+Public int getTotal()
|
|
123
|
+{
|
|
124
|
+ return total;
|
|
125
|
+}
|
|
126
|
+
|
|
127
|
+Exercise 2.27
|
|
128
|
+Must return value of type int
|
|
129
|
+
|
|
130
|
+Exercise 2.28
|
|
131
|
+Return type is main difference
|
|
132
|
+
|
|
133
|
+Exercise 2.29
|
|
134
|
+No return statements, these methods complete their statements without returning a value
|
|
135
|
+
|
|
136
|
+Exercise 2.30
|
|
137
|
+The value of the balance field changed as expected after each call
|
|
138
|
+
|
|
139
|
+Exercise 2.31
|
|
140
|
+Constructor's do not have return types
|
|
141
|
+
|
|
142
|
+Exercise 2.32
|
|
143
|
+Public void setPrice(int newPrice)
|
|
144
|
+{
|
|
145
|
+ price = newPrice;
|
|
146
|
+}
|
|
147
|
+
|
|
148
|
+Exercise 2.33
|
|
149
|
+Score = points;
|
|
150
|
+
|
|
151
|
+Exercise 2.35
|
|
152
|
+Yes, if changes field value, is a mutator
|
|
153
|
+
|
|
154
|
+Exercise 2.36
|
|
155
|
+My cat has green eyes.
|
|
156
|
+
|
|
157
|
+Exercise 2.37
|
|
158
|
+Public void prompt()
|
|
159
|
+{
|
|
160
|
+ System.out.println("Please insert the correct amount of money.");
|
|
161
|
+}
|
|
162
|
+
|
|
163
|
+Exercise 2.38
|
|
164
|
+The word price rather than the value of the variable price
|
|
165
|
+
|
|
166
|
+Exercise 2.39
|
|
167
|
+Would only print a string
|
|
168
|
+
|
|
169
|
+Exercise 2.40
|
|
170
|
+No, would only print string
|
|
171
|
+
|
|
172
|
+Exercise 2.41
|
|
173
|
+Public void showPrice()
|
|
174
|
+{
|
|
175
|
+ System.out.println("The price of the ticket is " + price + " cents.");
|
|
176
|
+}
|
|
177
|
+
|
|
178
|
+Exercise 2.42
|
|
179
|
+Each object has it's own copy of the price field and will display it's own assigned value
|
|
180
|
+
|
|
181
|
+Exercise 2.43
|
|
182
|
+You are no longer prompted for input, price variable is set at 1000 when object created
|
|
183
|
+
|
|
184
|
+Exercise 2.44
|
|
185
|
+TicketMachine()
|
|
186
|
+{
|
|
187
|
+ price = 1000;
|
|
188
|
+ balance = 0;
|
|
189
|
+ total = 0;
|
|
190
|
+}
|
|
191
|
+
|
|
192
|
+Exercise 2.45
|
|
193
|
+This method needs no parameters or return statement
|
|
194
|
+
|
|
195
|
+Exercise 2.46
|
|
196
|
+Balance is not changed if error message is printed
|
|
197
|
+
|
|
198
|
+Exercise 2.47
|
|
199
|
+No error will be thrown if 0 is entered and balance will not be changed as 0 is being added to current balance
|
|
200
|
+
|
|
201
|
+Exercise 2.48
|
|
202
|
+if(amount < )
|
|
203
|
+{
|
|
204
|
+ System.out.println("Use a positive amount rather than: " + amount);
|
|
205
|
+}
|
|
206
|
+else
|
|
207
|
+{
|
|
208
|
+ balance += amount;
|
|
209
|
+}
|
|
210
|
+
|
|
211
|
+Exercise 2.49
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+Exercise 2.50
|
|
215
|
+We use conditional statements to check input and then choose an outcome base on boolean expression
|
|
216
|
+
|
|
217
|
+Exercise 2.51
|
|
218
|
+It still compiles but no action is taken if conditional statement does not evaluate to true for the if statement to run
|
|
219
|
+
|
|
220
|
+Exercise 2.52
|
|
221
|
+No, boolean expression evaluates balance and price fields to be sure balance is <= price before making changes (subtracting) from balance field
|
|
222
|
+
|
|
223
|
+Exercise 2.53
|
|
224
|
+* / %
|
|
225
|
+
|
|
226
|
+Exercise 2.54
|
|
227
|
+Saving = price * discount;
|
|
228
|
+
|
|
229
|
+Exercise 2.55
|
|
230
|
+Mean = total / count;
|
|
231
|
+
|
|
232
|
+Exercise 2.56
|
|
233
|
+if(price > budget)
|
|
234
|
+{
|
|
235
|
+ System.out.println("Too expensive");
|
|
236
|
+}
|
|
237
|
+Else
|
|
238
|
+{
|
|
239
|
+ System.out.println("Just right");
|
|
240
|
+}
|
|
241
|
+
|
|
242
|
+Exercise 2.57
|
|
243
|
+if(price > budget)
|
|
244
|
+{
|
|
245
|
+ System.out.println("Too expensive, budget is: " + budget);
|
|
246
|
+}
|
|
247
|
+Else
|
|
248
|
+{
|
|
249
|
+ System.out.println("Just right");
|
|
250
|
+}
|
|
251
|
+
|
|
252
|
+Exercise 2.58
|
|
253
|
+Balance is set to 0 before returning
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|