Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write an example program to demonstrate that
- * package com.horstmann.impatient
- *
- * is not the same as
- * package com
- * package horstmann
- * package impatient
- *
- * About visibility.
- */
- /**
- * Write a puzzler that baffles your Scala friends, using a package com that isnβt at the top level.
- */
- package com.xilan.scala {
- object A {
- val a = 2
- }
- package com.xilan.scala {
- object A {
- private val a = 2
- def main(args: Array[String]): Unit = {
- println(_root_.com.xilan.scala.A.a == com.xilan.scala.A.a)
- }
- }
- }
- }
- /**
- * 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 2^n,
- * where a = 1664525, b = 1013904223, n = 32, and the initial value of previous is seed.
- */
- package object random {
- val a = 1664525
- val b = 1013904223
- val n = 32
- var previous = 0
- def setSeed(seed: Int) {
- previous = seed
- }
- def nextInt(): Int = (previous * a + b) % (math.pow(2, n).toInt)
- def nextDouble() = nextInt.toDouble
- }
- object Ex03 {
- def main(args: Array[String]): Unit = {
- random.setSeed(123456789)
- println(random.nextInt)
- println(random.nextDouble)
- }
- }
- // What is the meaning of private[com] def giveRaise(rate: Double)? Is it useful?
- // It means getRaise is visible up to com. It is not very useful since almost all packages start with com.
- /**
- * 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 => JavaHashMap}
- import scala.collection.mutable.{HashMap => ScalaHashMap}
- object Ex6 {
- def main(args: Array[String]): Unit = {
- val javaHashMap = new JavaHashMap[Int, String]()
- javaHashMap.put(1, "one")
- javaHashMap.put(2, "two")
- val scalaHashMap = ScalaHashMap[Int, String]()
- val itr = javaHashMap.entrySet().iterator()
- while (itr.hasNext) {
- val e = itr.next()
- scalaHashMap += (e.getKey() -> e.getValue())
- }
- scalaHashMap.foreach(println)
- }
- }
- /**
- * In the preceding exercise, move all imports into the innermost scope possible.
- */
- object Ex7 {
- def main(args: Array[String]): Unit = {
- import java.util.{HashMap => JavaHashMap}
- val javaHashMap = new JavaHashMap[Int, String]()
- javaHashMap.put(1, "one")
- javaHashMap.put(2, "two")
- import scala.collection.mutable.{HashMap => ScalaHashMap}
- val scalaHashMap = ScalaHashMap[Int, String]()
- val itr = javaHashMap.entrySet().iterator()
- while (itr.hasNext) {
- val e = itr.next()
- scalaHashMap += (e.getKey -> e.getValue)
- }
- scalaHashMap.foreach(println)
- }
- }
- /**
- What is the effect of
- import java._
- import javax._
- Is this a good idea?
- Import all classes under java, javax and thire sub-packages. This will import lots of unused classes, it maybe better to
- import only those classes will be used or some sub-packages.
- */
- /**
- * 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 StdIn 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 Ex9 {
- def main(args: Array[String]): Unit = {
- val username = System.getProperty("user.name")
- val password = io.StdIn.readLine()
- if (!password.equals("secret")) {
- System.err.println("Password is invalid")
- } else {
- System.out.println("Welcome " + username)
- }
- }
- }
- /**
- * Apart from StringBuilder, what other members of java.lang does the scala package override?
- *
- * Every Scala program implicitly starts with:
- * import java.lang._
- * import scala._
- * import Predef._
- *
- * Byte, Char, Double, Float, Long, Boolean
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement