-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathChapter07.scala
218 lines (183 loc) · 4.84 KB
/
Chapter07.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
/**
* Task 1:
*
* <p>Write an example program to demonstrate that
* <blockquote><code>
* package com.horstmann.impatient
* </code></blockquote>
* is not the same as
* <blockquote><code>
* package com <br/>
* package horstmann <br/>
* package impatient <br/>
* </code></blockquote>
*
* @see Chapter0701a.scala for solution part A
* @see Chapter0701b.scala for solution part B
*/
package com {
object FromCom {
val value = 1
}
package horstmann {
object FromHorstmann {
val value = 2
}
package impatient {
object FromImpatient {
val value = 3
}
}
}
}
/**
* Task 2:
*
* <p>Write a puzzler that baffles your Scala friends, using a package <code>com</code>
* that isn’t at the top level.
*/
package puzzler {
package com {
object FromCom {
val value = 21
}
}
}
/**
* Task 3:
*
* <p>Write a package random with functions
* nextInt(): Int,
* nextDouble(): Double,
* and setSeed(seed: Int): Unit.
*
* <p>To generate random numbers, use the linear congruential generator
* next = previous × a + b mod 2n,
* where a = 1664525, b = 1013904223, and n = 32.
*/
package object random {
private val addition: Int = (1013904223 % (1L << 32)).toInt
private var seed : Int = 0
def nextInt(): Int = {
seed = (seed * 1664525) + addition
if (seed < 0) ~seed
else seed
}
def nextDouble(): Double = {
nextInt() / (Int.MaxValue + 1.0)
}
def setSeed(seed: Int): Unit = this.seed = seed
}
/**
* Task 4:
*
* <p>Why do you think the Scala language designers provided the package object syntax instead
* of simply letting you add functions and variables to a package?
*
* <p>Solution: <br/>
* They decided to make it explicit by adding just one word "object" to package declaration,
* in my opinion, for a couple of reasons:
* <ul>
* <li>since its possible to have package declarations in different files, it would be hard
* to maintain functions and variable in different places for the same package</li>
* <li>because variables in package object are global (singletons) they didn't want to make it
* available by default</li>
* </ul>
*/
/**
* Task 5:
*
* <p>What is the meaning of <code>private[com] def giveRaise(rate: Double)</code>?
* Is it useful?
*/
package com {
/**
* <code>private[com]</code> makes definition package-private, meaning it is visible within
* the same package and all sub-packages.
*/
object VisibilityDef {
private[com] def giveRaise(rate: Double): Double = rate * 0.5
}
object VisibilityUsage {
println(VisibilityDef.giveRaise(1))
}
package horstmann {
object VisibilityUsage {
println(VisibilityDef.giveRaise(1))
}
}
}
/**
* Task 6:
*
* <p>Write a program that copies all elements from a Java hash map into a Scala hash map.
* Use imports to rename both classes.
*/
object Chapter0706 {
/**
* Task 7:
*
* <p>In the preceding exercise, move all imports into the innermost scope possible.
*/
import java.util.{HashMap => JavaHashMap}
import scala.collection.mutable.{HashMap => ScalaHashMap}
def fromJavaHashMap(javaHashMap: JavaHashMap[String, Int]): ScalaHashMap[String, Int] = {
import scala.collection.JavaConversions.iterableAsScalaIterable
val result = new ScalaHashMap[String, Int]
for (entry <- javaHashMap.entrySet()) {
result(entry.getKey) = entry.getValue
}
result
}
}
/**
* Task 8:
*
* <p>What is the effect of
* <blockquote><code>
* import java._ <br/>
* import javax._ <br/>
* </code></blockquote>
* Is this a good idea?
*/
object Chapter0708 {
import java._
/**
* Since we imported everything from java package, we can use sub-packages.
*/
def doSomething(list: util.List[String]) {
}
}
/**
* Task 9:
*
* <p>Write a program that imports the <code>java.lang.System</code> class,
* reads the user name from the <code>user.name</code> system property,
* reads a password from the <code>Console</code> object, and prints a message
* to the standard error stream if the password is not "secret".
* Otherwise, print a greeting to the standard output stream.
* Do not use any other imports, and do not use any qualified names (with dots).
*/
object Chapter0709 extends App {
//import java.lang.System
val userName = System.getProperty("user.name")
val password: String = readLine("Please, enter password: ")
if (password != "secret") error("Wrong password!")
else println("Welcome " + userName)
}
/**
* Task 10:
*
* <p>Apart from StringBuilder, what other members of java.lang does the scala package override?
*/
object JavaLangOverrides extends App {
val overrides = List[Class[_]](classOf[Boolean],
classOf[Byte],
classOf[Double],
classOf[Float],
classOf[Iterable[_]],
classOf[Long],
classOf[Short],
classOf[StringBuilder])
println(overrides.mkString("\n"))
}