Scala Learning (vii) practice

Source: Internet
Author: User

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

  1. Package com{
  2. Package horstmann{
  3. Object a{
  4. def hi=println ("I am A")
  5. }
  6. Package impatient{
  7. Object B extends app{
  8. def Hi=a.hi
  9. Hi
  10. }
  11. }
  12. }
  13. }

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:

  1. Package random{
  2. Object Random {
  3. Private val A = 1664525
  4. private val B = 1013904223
  5. private val n = 32
  6. Private var seed=0
  7. Private var follow:bigint=0
  8. Private var previous:bigint=0
  9. Def nextint (): int={
  10. follow= (previous*a+b)%bigint (Math.pow (2, N). Tolong)
  11. Previous=follow
  12. (Follow%int. MaxValue). Intvalue ()
  13. }
  14. def nextdouble ():D ouble={
  15. Nextint.todouble
  16. }
  17. def setseed (Newseed:int) {
  18. Seed=newseed
  19. Previous=seed
  20. }
  21. }
  22. }
  23. Object Test extends app{
  24. var r =random. Random
  25. R.setseed (args (0). ToInt)
  26. for (I <-1 to Ten) println (R.nextint ())
  27. for (I <-1 to Ten) println (R.nextdouble ())
  28. }

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:

  1. Import java.util. {Hashmap=>jhashmap}
  2. Import Scala.collection.mutable.HashMap
  3. Object Javamap {
  4. def transmapvalues (Javamap:jhashmap[any,any]): hashmap[any,any]={
  5. Val result=new Hashmap[any,any]
  6. for (k <-Javamap.keyset (). ToArray ()) {
  7. Result+=k->javamap.get (k)
  8. }
  9. Result
  10. }
  11. def main (args:array[string]): Unit = {
  12. Val jmap:jhashmap[any,any]=new Jhashmap[any,any]
  13. var smap=new Hashmap[any,any]
  14. for (I <-1 to 9)
  15. Jmap.put (i, "javamap" +i)
  16. Smap=transmapvalues (Jmap)
  17. Smap.foreach (println)
  18. }
  19. }

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:

  1. Object Javamap {
  2. Import java.util. {Hashmap=>jhashmap}
  3. Import Scala.collection.mutable.HashMap
  4. def transmapvalues (Javamap:jhashmap[any,any]): hashmap[any,any]={
  5. Val result=new Hashmap[any,any]
  6. for (k <-Javamap.keyset (). ToArray ()) {
  7. Result+=k->javamap.get (k)
  8. }
  9. Result
  10. }
  11. def main (args:array[string]): Unit = {
  12. Val jmap:jhashmap[any,any]=new Jhashmap[any,any]
  13. var smap=new Hashmap[any,any]
  14. for (I <-1 to 10)
  15. Jmap.put (i, "javamap" +i)
  16. Smap=transmapvalues (Jmap)
  17. Smap.foreach (println)
  18. }
  19. }

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:

  1. Object sys{
  2. Import Scala.io.StdIn
  3. Import Java.lang.system._
  4. def main (args:array[string]): Unit = {
  5. Val Pass=stdin.readline ()
  6. if (pass== "secret") {
  7. Val Name=getproperty ("user.name")
  8. out.printf ("greetings,%s!", name)
  9. }Else{
  10. ERR.PRINTLN ("error")
  11. }
  12. }
  13. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.