Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. /**
  2. * Write an example program to demonstrate that
  3. * package com.horstmann.impatient
  4. *
  5. * is not the same as
  6. * package com
  7. * package horstmann
  8. * package impatient
  9. *
  10. * About visibility.
  11. */
  12.  
  13. /**
  14. * Write a puzzler that baffles your Scala friends, using a package com that isn’t at the top level.
  15. */
  16. package com.xilan.scala {
  17.  
  18. object A {
  19. val a = 2
  20. }
  21.  
  22. package com.xilan.scala {
  23. object A {
  24. private val a = 2
  25.  
  26. def main(args: Array[String]): Unit = {
  27. println(_root_.com.xilan.scala.A.a == com.xilan.scala.A.a)
  28. }
  29. }
  30. }
  31. }
  32.  
  33. /**
  34. * Write a package random with functions nextInt(): Int, nextDouble(): Double, and setSeed(seed: Int): Unit. To generate random numbers, use the linear congruential generator
  35. * next = (previous Γ— a + b) mod 2^n,
  36. * where a = 1664525, b = 1013904223, n = 32, and the initial value of previous is seed.
  37. */
  38. package object random {
  39. val a = 1664525
  40. val b = 1013904223
  41. val n = 32
  42. var previous = 0
  43.  
  44. def setSeed(seed: Int) {
  45. previous = seed
  46. }
  47.  
  48. def nextInt(): Int = (previous * a + b) % (math.pow(2, n).toInt)
  49.  
  50. def nextDouble() = nextInt.toDouble
  51. }
  52.  
  53. object Ex03 {
  54. def main(args: Array[String]): Unit = {
  55. random.setSeed(123456789)
  56. println(random.nextInt)
  57. println(random.nextDouble)
  58. }
  59. }
  60.  
  61.  
  62.  
  63. // What is the meaning of private[com] def giveRaise(rate: Double)? Is it useful?
  64. // It means getRaise is visible up to com. It is not very useful since almost all packages start with com.
  65.  
  66. /**
  67. * Write a program that copies all elements from a Java hash map into a Scala hash map. Use imports to rename both classes.
  68. */
  69.  
  70. import java.util.{HashMap => JavaHashMap}
  71. import scala.collection.mutable.{HashMap => ScalaHashMap}
  72.  
  73. object Ex6 {
  74. def main(args: Array[String]): Unit = {
  75. val javaHashMap = new JavaHashMap[Int, String]()
  76. javaHashMap.put(1, "one")
  77. javaHashMap.put(2, "two")
  78.  
  79. val scalaHashMap = ScalaHashMap[Int, String]()
  80. val itr = javaHashMap.entrySet().iterator()
  81. while (itr.hasNext) {
  82. val e = itr.next()
  83. scalaHashMap += (e.getKey() -> e.getValue())
  84. }
  85.  
  86. scalaHashMap.foreach(println)
  87. }
  88. }
  89.  
  90. /**
  91. * In the preceding exercise, move all imports into the innermost scope possible.
  92. */
  93. object Ex7 {
  94. def main(args: Array[String]): Unit = {
  95. import java.util.{HashMap => JavaHashMap}
  96. val javaHashMap = new JavaHashMap[Int, String]()
  97. javaHashMap.put(1, "one")
  98. javaHashMap.put(2, "two")
  99.  
  100. import scala.collection.mutable.{HashMap => ScalaHashMap}
  101. val scalaHashMap = ScalaHashMap[Int, String]()
  102. val itr = javaHashMap.entrySet().iterator()
  103. while (itr.hasNext) {
  104. val e = itr.next()
  105. scalaHashMap += (e.getKey -> e.getValue)
  106. }
  107.  
  108. scalaHashMap.foreach(println)
  109. }
  110. }
  111.  
  112. /**
  113. What is the effect of
  114. import java._
  115. import javax._
  116.  
  117. Is this a good idea?
  118.  
  119. Import all classes under java, javax and thire sub-packages. This will import lots of unused classes, it maybe better to
  120. import only those classes will be used or some sub-packages.
  121. */
  122.  
  123. /**
  124. * Write a program that imports the java.lang.System class, reads the user name from the user.name system property,
  125. * reads a password from the StdIn object, and prints a message to the standard error stream if the password is not
  126. * "secret". Otherwise, print a greeting to the standard output stream. Do not use any other imports, and do not
  127. * use any qualified names (with dots).
  128. */
  129. object Ex9 {
  130. def main(args: Array[String]): Unit = {
  131. val username = System.getProperty("user.name")
  132. val password = io.StdIn.readLine()
  133. if (!password.equals("secret")) {
  134. System.err.println("Password is invalid")
  135. } else {
  136. System.out.println("Welcome " + username)
  137. }
  138. }
  139. }
  140.  
  141. /**
  142. * Apart from StringBuilder, what other members of java.lang does the scala package override?
  143. *
  144. * Every Scala program implicitly starts with:
  145. * import java.lang._
  146. * import scala._
  147. * import Predef._
  148. *
  149. * Byte, Char, Double, Float, Long, Boolean
  150. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement