Guest User

Untitled

a guest
Mar 30th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. //1. Write an example program to demonstrate that
  2. //package com.horstmann.impatient
  3. //is not the same as
  4. //package com
  5. //package horstmann
  6. //package impatient
  7.  
  8. // File: Demo1.scala
  9. package com {
  10. package horstmann {
  11. object Utils1 {
  12. def percentOf(value: Double, rate: Double) = value * rate / 100
  13. }
  14. package impatient {
  15. class Employee1 {
  16. def raise(salary: Double, rate: Double) = salary * Utils1.percentOf(salary, rate)
  17. }
  18. }
  19. }
  20. }
  21.  
  22. // File: Demo2.scala
  23. package com.horstmann.impatient
  24.  
  25. class Manager1 {
  26. def raise(salary: Double, rate: Double) =
  27. salary * com.horstmann.Utils1.percentOf(salary, rate) // Can't refer to Utils directly
  28. }
  29.  
  30. //3. Write a package random with functions nextInt(): Int, nextDouble(): Double, and
  31. //setSeed(seed: Int): Unit. To generate random numbers, use the linear
  32. //congruential generator
  33. //next = previous * a + b mod 2n,
  34. //where a = 1664525, b = 1013904223, and n = 32.
  35.  
  36. package object random {
  37. private var next = 1
  38. def nextInt(): Int = {next = (next * 1664525 + 1013904223) % math.pow(2, 32).toInt; next}
  39. def nextDouble(): Double = nextInt
  40. def setSeed(seed: Int) = {next = seed;}
  41. }
  42.  
  43. package random {
  44. }
  45.  
  46. // 4. Why do you think the Scala language designers provided the package object syntax
  47. // instead of simply letting you add functions and variables to a package?”
  48. // => My guess is that since everything in Scala is an object, letting you add functions
  49. // and variables to a package would have been an aberration
  50.  
  51.  
  52. // 5. What is the meaning of private[com] def giveRaise(rate: Double)? Is it useful?
  53. // => It means that the function giveRaise is visible up to the enclosing package named com.
  54. // Since com is a very common top level package name, this will not be very useful.
  55.  
  56. // 6. Write a program that copies all elements from a Java hash map into a
  57. // Scala hash map. Use imports to rename both classes.
  58. import java.util.{HashMap => JavaMap}
  59. import scala.collection.mutable.{HashMap => ScalaMap}
  60.  
  61. object Hashmaps extends App {
  62. val jMap = new JavaMap[String, String]
  63. jMap.put("1", "One")
  64. jMap.put("2", "Two")
  65. jMap.put("3", "Three")
  66.  
  67. val sMap = new ScalaMap[String, String]
  68.  
  69. val entryIter = jMap.entrySet().iterator()
  70. while (entryIter.hasNext()) {
  71. val entry = entryIter.next()
  72. sMap += ((entry.getKey(), entry.getValue()))
  73. }
  74.  
  75. println(sMap)
  76. }
  77.  
  78. // 7. In the preceding exercise, move all imports into the innermost scope possible.
  79. object Hashmaps extends App {
  80. import java.util.{HashMap => JavaMap}
  81. import scala.collection.mutable.{HashMap => ScalaMap}
  82.  
  83. val jMap = new JavaMap[String, String]
  84. jMap.put("1", "One")
  85. jMap.put("2", "Two")
  86. jMap.put("3", "Three")
  87.  
  88. val sMap = new ScalaMap[String, String]
  89.  
  90. val entryIter = jMap.entrySet().iterator()
  91. while (entryIter.hasNext()) {
  92. val entry = entryIter.next()
  93. sMap += ((entry.getKey(), entry.getValue()))
  94. }
  95.  
  96. println(sMap)
  97. }
  98.  
  99. // 8. What is the effect of
  100. // import java._
  101. // import javax._
  102. // Is this a good idea?
  103. // => Brings all packages and classes under the java and javax package in scope
  104. // Since there are a lot of classes and subpackages under them, bringing them into scope
  105. // may not be very useful. It might be a better idea to import only those subpackages that
  106. // you are using a lot in your program. e.g. javax.swing._ or java.awt._
  107.  
  108. //9. Write a program that imports the java.lang.System class, reads the user name
  109. //from the user.name system property, reads a password from the Console object,
  110. //and prints a message to the standard error stream if the password is not
  111. //"secret". Otherwise, print a greeting to the standard output stream. Do not use
  112. //any other imports, and do not use any qualified names (with dots).
  113.  
  114. import java.lang.System
  115.  
  116. object Greeting extends App {
  117. val username = System.getProperty("user.name")
  118. print("Password for " + username + ": ");
  119. val password = Console.readLine
  120. if(password.equals("secret")) System.out.println("Hello " + username)
  121. else System.err.println("Invalid password")
  122. }
Add Comment
Please, Sign In to add comment