Control structures and functions |
1. Write a sample program to show why
Package Com.horstmann.impatient
differs from
Package com
Package Horstmann
Package impatient
Description: The difference is that the upper layer of the latter package is also visible, while the string is only visible in the current package scope
Program code: B.scala
- Package com{
- Package horstmann{
- Object a{
- def hi=println ("I am A")
- }
- Package impatient{
- Object B extends app{
- def Hi=a.hi
- Hi
- }
- }
- }
- }
Operation Result:
E:\test>scalac B.scala
E:\test>scala com.horstmann.impatient.b
I am A
E:\test>
Program code: C.scala
Package com.horstmann.impatient{
Object C extends app{
B.hi
A.hi
}
}
Run Results :
E:\test>scalac C.scala
C.scala:4: Error:not found:value A
A.hi
^
One error found
E:\test>
not found at compile time A, description Concatenation declaration does not include a superior declaration
Program code: D.scala
E:\test>scalac D.scala
E:\test>scala com.horstmann.impatient.c
I am A
I am A
E:\test>
2. Write a code that confuses your Scala friends, using a COM package that is not at the top
Program code: A1.scala
Package COM {
Package Horstmann {
Package COM {
Package Horstmann {
Object A {
def hi = println ("I am the Ghost A")
}
}
}
}
}
Program code: A2.scala
Package COM {
Package Horstmann {
Object A {
def Hi =println ("I am A")
}
Package Impatient {
Object B extends App {
def hi = com.horstmann.a.hi
Hi
}
}
}
}
Compiling the A2.scala before compiling the A1.scala results in the following:
E:\test>scalac A2.scala
E:\test>scalac A1.scala
E:\test>scala com.horstmann.impatient.b
I am A
E:\test>
Compiling the A1.scala before compiling the A1.scala results in the following:
E:\test>scalac A1.scala
E:\test>scalac A2.scala
E:\test>scala com.horstmann.impatient.b
I am the Ghost A
E:\test>
3. Write a package random, add the function nextlnt (): Int, Nextdouble (): Double and Setseed (seed:int): Unit. The algorithm that generates the random number uses the linear same generator:
Post value = (previous value xa+b) mod 2n
Where a = 1664525,b =1013904223,n = 32, the initial value of the previous value is seed.
Program code:
- Package random{
- Object Random {
- Private val A = 1664525
- private val B = 1013904223
- private val n = 32
- Private var seed=0
- Private var follow:bigint=0
- Private var previous:bigint=0
- Def nextint (): int={
- follow= (previous*a+b)%bigint (Math.pow (2, N). Tolong)
- Previous=follow
- (Follow%int. MaxValue). Intvalue ()
- }
- def nextdouble ():D ouble={
- Nextint.todouble
- }
- def setseed (Newseed:int) {
- Seed=newseed
- Previous=seed
- }
- }
- }
- Object Test extends app{
- var r =random. Random
- R.setseed (args (0). ToInt)
- for (I <-1 to Ten) println (R.nextint ())
- for (I <-1 to Ten) println (R.nextdouble ())
- }
Operation Result:
E:\test>scalac Random.scala
E:\test>scala Test 0
1013904223
1196435762
1372387050
720982837
1649599747
523159175
1476291629
601448361
33406696
351317787
1.27442629E9
1.020336477E9
4.8889166E8
1.654060783E9
2.8987765E7
6.3353937E7
8.92205936E8
1.338634754E9
1.649346937E9
6.21388933E8
E:\test>
4. In your opinion, why did Scala's designers provide the package object method instead of simply letting you add functions and variables to the packages?
Directly add functions and variables to the package, such as COM.A.B.C. This makes a level difference from class or object under C. They are actually the common ancestor definitions for all classes under C. This means that there is no encapsulation. And the implementation of the estimate is also more troublesome.
5. private[com] def giveraise (rate:double) What do you mean, useful?
The function is visible under the COM package and can enlarge the visible range of the function
6. Write a program that copies all elements of the Java hash map shot to the Scala hash map. Rename these two classes with an introduction statement
Program code:
- Import java.util. {Hashmap=>jhashmap}
- Import Scala.collection.mutable.HashMap
- Object Javamap {
- def transmapvalues (Javamap:jhashmap[any,any]): hashmap[any,any]={
- Val result=new Hashmap[any,any]
- for (k <-Javamap.keyset (). ToArray ()) {
- Result+=k->javamap.get (k)
- }
- Result
- }
- def main (args:array[string]): Unit = {
- Val jmap:jhashmap[any,any]=new Jhashmap[any,any]
- var smap=new Hashmap[any,any]
- for (I <-1 to 9)
- Jmap.put (i, "javamap" +i)
- Smap=transmapvalues (Jmap)
- Smap.foreach (println)
- }
- }
Operation Result:
(8,JAVAMAP8)
(2,JAVAMAP2)
(5,JAVAMAP5)
(4,JAVAMAP4)
(7,JAVAMAP7)
(1,JAVAMAP1)
(9,JAVAMAP9)
(3,JAVAMAP3)
(6,JAVAMAP6)
7. In the previous exercise, move all incoming statements to the smallest possible scope
Description: Import can be placed in any area, directly into the object structure, there is no problem
Program code:
- Object Javamap {
- Import java.util. {Hashmap=>jhashmap}
- Import Scala.collection.mutable.HashMap
- def transmapvalues (Javamap:jhashmap[any,any]): hashmap[any,any]={
- Val result=new Hashmap[any,any]
- for (k <-Javamap.keyset (). ToArray ()) {
- Result+=k->javamap.get (k)
- }
- Result
- }
- def main (args:array[string]): Unit = {
- Val jmap:jhashmap[any,any]=new Jhashmap[any,any]
- var smap=new Hashmap[any,any]
- for (I <-1 to 10)
- Jmap.put (i, "javamap" +i)
- Smap=transmapvalues (Jmap)
- Smap.foreach (println)
- }
- }
8. What is the function of the following code, is that a good idea?
Import Java._
Import Javax._
All the contents of Java and javax are introduced. Because Scala will automatically overwrite Java's class with the same name, there will be no conflict. Even so, it can be confusing to introduce too many packages. Besides, Scala's compiling is slow enough.
9. Write a program, inductive Java.lang.System class, read the user name from the User.Name system property, read a password from the console object, if the password is not " secret", A message is printed in the standard error stream , and a greeting message is printed in the standard output stream if the password is "secret". Do not use any other introduction, nor use any qualifiers, that is, the one with the period
Program code:
- Object sys{
- Import Scala.io.StdIn
- Import Java.lang.system._
- def main (args:array[string]): Unit = {
- Val Pass=stdin.readline ()
- if (pass== "secret") {
- Val Name=getproperty ("user.name")
- out.printf ("greetings,%s!", name)
- }Else{
- ERR.PRINTLN ("error")
- }
- }
- }
Operation Result:
Secret
greetings,hadoop!
In addition to StringBuilder, the members of the Java.lang are covered by the Scala package .
Console,math, there are basic types of packaging objects, long,double,char,short, etc. are covered by Scala.
If you think reading this blog gives you something to gain, you might want to click "recommend" in the lower right corner.
If you want to find my new blog more easily, click on "Follow Me" in the lower left corner.
If you are interested in what my blog is talking about, please keep following my follow-up blog, I am "sunddenly".
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Scala Learning (vii) practice