-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathChapter08.scala
320 lines (255 loc) · 7.96 KB
/
Chapter08.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import scala.collection.mutable.ListBuffer
/**
* Task 1:
*
* Extend the following `BankAccount` class to a `CheckingAccount` class that charges $1
* for every deposit and withdrawal.
* {{{
* class BankAccount(initialBalance: Double) {
* private var balance = initialBalance
* def deposit(amount: Double) = { balance += amount; balance }
* def withdraw(amount: Double) = { balance -= amount; balance }
* }
* }}}
*/
class BankAccount(initialBalance: Double) {
private var balance = initialBalance
def deposit(amount: Double) = {
balance += amount
balance
}
def withdraw(amount: Double) = {
balance -= amount
balance
}
}
class CheckingAccount(initialBalance: Double) extends BankAccount(initialBalance) {
private val charge: Double = 1
override def deposit(amount: Double) = super.deposit(amount - charge)
override def withdraw(amount: Double) = super.withdraw(amount + charge)
}
/**
* Task 2:
*
* Extend the `BankAccount` class of the preceding exercise into a class `SavingsAccount`
* that earns interest every month (when a method `earnMonthlyInterest` is called)
* and has three free deposits or withdrawals every month. Reset the transaction
* count in the `earnMonthlyInterest` method.
*/
class SavingsAccount(initialBalance: Double) extends BankAccount(initialBalance) {
private val monthlyInterest: Double = 0.01
private val maxFreeTransactions: Int = 3
private var transactionsCount: Int = 0
private var balance: Double = initialBalance
override def deposit(amount: Double) = {
balance = super.deposit(amount - charge)
balance
}
override def withdraw(amount: Double) = {
balance = super.withdraw(amount + charge)
balance
}
def getBalance: Double = {
balance
}
def earnMonthlyInterest(): Unit = {
transactionsCount = 0
balance = super.deposit(balance * monthlyInterest)
}
private def charge: Double = {
if (transactionsCount < maxFreeTransactions) {
transactionsCount += 1
return 0.0
}
1.0
}
}
package task0803 {
/**
* Task 3:
*
* Consult your favorite Java or C++ textbook that is sure to have an example
* of a toy inheritance hierarchy, perhaps involving employees, pets, graphical
* shapes, or the like. Implement the example in Scala.
*/
abstract class Shape {
def draw(): Unit
def erase(): Unit
}
class Circle extends Shape {
override def draw(): Unit = {
println("drawing Circle...")
}
override def erase(): Unit = {
println("erasing Circle...")
}
}
class Square extends Shape {
override def draw(): Unit = {
println("drawing Square...")
}
override def erase(): Unit = {
println("erasing Square...")
}
}
object Shapes extends App {
cleanAndPaint(new Circle)
cleanAndPaint(new Square)
def cleanAndPaint(shape: Shape): Unit = {
shape.erase()
shape.draw()
}
}
}
package task0804 {
/**
* Task 4:
*
* Define an abstract class `Item` with methods `price` and `description`. A `SimpleItem`
* is an item whose `price` and `description` are specified in the constructor. Take advantage
* of the fact that a val can override a def. A `Bundle` is an item that contains other items.
* Its price is the sum of the prices in the bundle. Also provide a mechanism for adding items
* to the bundle and a suitable description method.
*/
abstract class Item {
/** Price in minor units */
def price: Int
def description: String
}
class SimpleItem(override val price: Int, override val description: String) extends Item
class Bundle extends Item {
private val items = new ListBuffer[Item]
def addItem(item: Item): Bundle = {
items += item
this
}
override def price = items.foldLeft(0)((sum, item) => sum + item.price)
override def description = items.map(_.description).mkString("\n\n")
}
}
package task0805 {
/**
* Task 5:
*
* Design a class `Point` whose x and y coordinate values can be provided in a constructor.
* Provide a subclass `LabeledPoint` whose constructor takes a `label` value and `x` and `y`
* coordinates, such as
* {{{
* new LabeledPoint("Black Thursday", 1929, 230.07)
* }}}
*/
class Point(val x: Double, val y: Double)
class LabeledPoint(val label: String, x: Double, y: Double) extends Point(x, y)
}
package task0806 {
import task0805.Point
/**
* Task 6:
*
* Define an abstract class `Shape` with an abstract method `centerPoint` and subclasses
* `Rectangle` and `Circle`. Provide appropriate constructors for the subclasses and
* override the `centerPoint` method in each subclass.
*/
abstract class Shape {
def centerPoint: Point
}
class Circle(override val centerPoint: Point, val radius: Double) extends Shape
class Rectangle(val x1: Double, val y1: Double, val x2: Double, val y2: Double) extends Shape {
override def centerPoint = new Point((x1 + x2) / 2, (y1 + y2) / 2)
}
}
package task0807 {
import java.awt.Rectangle
/**
* Task 7:
*
* Provide a class `Square` that extends `java.awt.Rectangle` and has three constructors:
* one that constructs a square with a given corner point and width,
* one that constructs a square with corner (0, 0) and a given width,
* and one that constructs a square with corner (0, 0) and width 0.
*/
class Square(x: Int = 0, y: Int = 0, width: Int = 0) extends Rectangle(x, y, width, width) {
def this(width: Int) {
this(0, 0, width)
}
}
}
package task0808 {
/**
* Task 8:
*
* Compile the `Person` and `SecretAgent` classes in Section 8.6, “Overriding Fields,” on page 91
* and analyze the class files with `javap`.
* How many name fields are there?
* How many name getter methods are there?
* What do they get?
* (Hint: Use the -c and -private options.)
*/
class Person(val name: String) {
override def toString = getClass.getName + "[name=" + name + "]"
}
/**
* Solution:
*
* There are two name fields, one in Person class, and one in SecretAgent class.
* There are two name getter methods, one in each class as well. The second one overrides the first.
* They get/return corresponding name field from their class.
*
* Additionally, there is separate toString field and overrided toString method, which returns
* that field.
*/
class SecretAgent(codename: String) extends Person(codename) {
// Don’t want to reveal name...
override val name = "secret"
// ...or class name
override val toString = "secret"
}
}
/**
* Task 9:
*
* In the `Creature` class of Section 8.10, "Construction Order and Early Definitions,"
* on page 94, replace `val range` with a `def`. What happens when you also use a
* `def` in the `Ant` subclass? What happens when you use a `val` in the subclass?
* Why?
*/
class Creature {
def range: Int = 10
val env: Array[Int] = new Array[Int](range)
}
class Ant extends Creature {
override def range = 2
}
/**
* Solution:
*
* When we use def in both superclass and subclass the array length is initialized with expected,
* overrided value 2. This happens because there is no field to hold the value, and we don't have
* initialization order problem.
*
* But when we use def in superclass and val in subclass initialization order problem is back
* again. Since now the range value in subclass is backed by field and at the moment when
* corresponding generated range getter method is called from superclass constructor the value is
* not initialized yet and equals to 0 by default.
*/
object Creatures extends App {
println(new Ant().env.length)
}
/**
* Task 10:
*
* The file `scala/collection/immutable/Stack.scala` contains the definition
* {{{
* class Stack[A] protected (protected val elems: List[A])
* }}}
* Explain the meanings of the `protected` keywords. (Hint: Review the discussion
* of private constructors in Chapter 5.)
*
*
* Solution:
*
* The first `protected` keyword defines protected primary constructor, which is accessible only
* from auxiliary constructor or from subclass primary constructor.
* The second `protected` keyword defines protected field `elems` with corresponding protected
* getter method, they are accessible within class and subclasses.
*/