Advertisement
Guest User

Untitled

a guest
Jan 7th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. /* sfti_ch07_ex01
  2.  
  3. package com {
  4. package object horstmann {
  5. def hello() = println("HI")
  6. }
  7. package horstmann {
  8. package object impatient {
  9. def hi() = hello()
  10. }
  11. }
  12. }
  13.  
  14. package com.horstmann.impatient {
  15. object Obj {
  16. // hello() // compile error
  17. com.horstmann.hello() // OK
  18. }
  19. }
  20. */
  21. /* sfti_ch07_ex03.scala
  22.  
  23. package object random {
  24. var Array(next, a, b, n) = Array(0, 1664525, 1013904223, 32)
  25. def nextInt(): Int = {
  26. next = (next * a + b) % 2*n
  27. next
  28. }
  29. def nextDouble(): Double = {
  30. next = (next * a + b) % 2*n
  31. next.toDouble
  32. }
  33. def setSeed(seed: Int) {
  34. next = seed
  35. }
  36. }
  37. package random {}
  38. */
  39.  
  40. object sfti extends App {
  41. // 2. skip
  42. // 3. Write a package random with functions nextInt(): Int, nextDouble(): Double, and
  43. // setSeed(seed: Int): Unit. To generate random numbers, use the linear
  44. // congruential generator
  45. // next = previous * a + b mod 2n,
  46. // where a = 1664525, b = 1013904223, and n = 32.
  47. random.setSeed(10)
  48. println(s"random.nextInt ${random.nextInt}")
  49.  
  50. // 6, 7
  51. import java.util.{ HashMap => JavaMap }
  52. var javaMap = new JavaMap[String, String]
  53. javaMap.put("1", "a")
  54. javaMap.put("2", "b")
  55. javaMap.put("3", "c")
  56.  
  57. import scala.collection.JavaConverters._
  58. var scalaMap = javaMap.asScala
  59. println(s"scalaMap $scalaMap")
  60.  
  61. // 9.
  62. import java.lang.System._
  63. var username = getProperty("user.name")
  64. var password = readLine()
  65.  
  66. if (password == "secret")
  67. println("CORRECT")
  68. else {
  69. // impossible;
  70. err.println("WRONG")
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement