Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//1. Write an example program to demonstrate that
//package com.horstmann.impatient
//is not the same as
//package com
//package horstmann
//package impatient
// File: Demo1.scala
package com {
package horstmann {
object Utils1 {
def percentOf(value: Double, rate: Double) = value * rate / 100
}
package impatient {
class Employee1 {
def raise(salary: Double, rate: Double) = salary * Utils1.percentOf(salary, rate)
}
}
}
}
// File: Demo2.scala
package com.horstmann.impatient
class Manager1 {
def raise(salary: Double, rate: Double) =
salary * com.horstmann.Utils1.percentOf(salary, rate) // Can't refer to Utils directly
}
//3. Write a package random with functions nextInt(): Int, nextDouble(): Double, and
//setSeed(seed: Int): Unit. 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 var next = 1
def nextInt(): Int = {next = (next * 1664525 + 1013904223) % math.pow(2, 32).toInt; next}
def nextDouble(): Double = nextInt
def setSeed(seed: Int) = {next = seed;}
}
package random {
}
// 4. 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?”
// => My guess is that since everything in Scala is an object, letting you add functions
// and variables to a package would have been an aberration
// 5. What is the meaning of private[com] def giveRaise(rate: Double)? Is it useful?
// => It means that the function giveRaise is visible up to the enclosing package named com.
// Since com is a very common top level package name, this will not be very useful.
// 6. Write a program that copies all elements from a Java hash map into a
// Scala hash map. Use imports to rename both classes.
import java.util.{HashMap => JavaMap}
import scala.collection.mutable.{HashMap => ScalaMap}
object Hashmaps extends App {
val jMap = new JavaMap[String, String]
jMap.put("1", "One")
jMap.put("2", "Two")
jMap.put("3", "Three")
val sMap = new ScalaMap[String, String]
val entryIter = jMap.entrySet().iterator()
while (entryIter.hasNext()) {
val entry = entryIter.next()
sMap += ((entry.getKey(), entry.getValue()))
}
println(sMap)
}
// 7. In the preceding exercise, move all imports into the innermost scope possible.
object Hashmaps extends App {
import java.util.{HashMap => JavaMap}
import scala.collection.mutable.{HashMap => ScalaMap}
val jMap = new JavaMap[String, String]
jMap.put("1", "One")
jMap.put("2", "Two")
jMap.put("3", "Three")
val sMap = new ScalaMap[String, String]
val entryIter = jMap.entrySet().iterator()
while (entryIter.hasNext()) {
val entry = entryIter.next()
sMap += ((entry.getKey(), entry.getValue()))
}
println(sMap)
}
// 8. What is the effect of
// import java._
// import javax._
// Is this a good idea?
// => Brings all packages and classes under the java and javax package in scope
// Since there are a lot of classes and subpackages under them, bringing them into scope
// may not be very useful. It might be a better idea to import only those subpackages that
// you are using a lot in your program. e.g. javax.swing._ or java.awt._
//9. Write a program that imports the java.lang.System class, reads the user name
//from the user.name system property, reads a password from the Console 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).
import java.lang.System
object Greeting extends App {
val username = System.getProperty("user.name")
print("Password for " + username + ": ");
val password = Console.readLine
if(password.equals("secret")) System.out.println("Hello " + username)
else System.err.println("Invalid password")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment