Scala Learning (vi) practice

Source: Internet
Author: User

Objects & Exercises in Scala

1. Write a Conversions object and add the Inchestocentimeters,gallonstoliters and Milestokilometers methods

Program code:

  1. Object Conversions {
  2. Private Val i2c=30.48
  3. Private Val g2l=3.785411784
  4. Private Val m2k=1.609344
  5. def inchestocentimeters (inch:double):D ouble={
  6. Inch*i2c
  7. }
  8. def gallonstoliters (gallon:double):D ouble={
  9. gallon*g2l
  10. }
  11. def milestokilometers (mile:double):D ouble={
  12. mile*m2k
  13. }
  14. def main (args:array[string]): Unit = {
  15. Val inch=15
  16. Val gallon=15
  17. Val mile=15
  18. println (inch+ " ft = " +inchestocentimeters (inch))
  19. println (gallon+ " gal = " +gallonstoliters (gallon))
  20. println (mile+ " km = " +milestokilometers (mile))
  21. }
  22. }

Operation Result:

the feet = 457.2

the gallons = 56.78117676

the km = 24.14016

2. The previous exercise was not very object-oriented. Provides a generic superclass unitconversion and defines the inchestocentimeters,gallonstoliters and Milestokilometers objects that extend the superclass

Program code:

  1. Abstract class unitconversion {
  2. def Converse (from:double):D ouble
  3. }
  4. Object inchestocentimeters extends unitconversion{
  5. Private Val i2c=30.48
  6. Override Def Converse (inche:double):D ouble={
  7. Inche*i2c
  8. }
  9. }
  10. Object gallonstoliters extends unitconversion{
  11. Private Val g2l=3.785311784
  12. Override Def Converse (gallon:double):D ouble={
  13. G2l*gallon
  14. }
  15. }
  16. Object milestokilometers extends unitconversion{
  17. Private Val m2k=1.609344
  18. Override Def Converse (mile:double):D ouble={
  19. M2k*mile
  20. }
  21. }
  22. Object test{
  23. def main (args:array[string]): Unit = {
  24. Val inche=10; Val gallon=10; Val mile=10
  25. println (inche+ " ft = " +inchestocentimeters.converse (inche) + " cm ")
  26. println (gallon+ " gal = " +gallonstoliters.converse (gallon) + " liter ")
  27. println (mile+ " km = " +milestokilometers.converse (mile) + " km ")
  28. }
  29. }

Operation Result:

Ten feet = 304.8 centimeters

Ten gallons = 37.85311784 liters

Ten km = 16.09344 km

3. Define an Origin object that extends from Java.awt.Point. Why this is actually not a good idea (look at the method of the point class carefully)

Description: The GetLocation method in point returns a Point object and requires the Origin class to return to the Origin object. Point has a Move method, SetLocation method. These are not very suitable as Origin (origin).

Program code:

Import Java.awt.Point

Object Origin extends point with app{

Override Def getlocation:point=super.getlocation ()

Origin.move (2, 3)

println (origin.tostring ())

}

4. Define a point class and a companion object so that we can construct a point instance directly with point (3,4) without new

Description: The Apply method uses

Program code:

Class Point (X:int,y:int) {

Override Def toString (): string= "x=" +x+ "y=" +y

}

Object Point extends app{

def apply (X:int,y:int) ={

New Point (x, y)

}

Val p=new Point (3,4)

println (P)

}

Operation Result:

x= 3 y= 4

5. Write a Scala app that uses app traits to print command line arguments in reverse order, separated by spaces. For example, Scala Reverse Hello world should print world Hello

Program code:

Object Reverse extends app{

for (str <-args.reverse)

println (str+ "")

}

Operation Result:

6. Write a poker 4-suit enumeration, and let its ToString method return ♣,♦,♥,♠

Description: The symbols of these four suits win the special symbol soft keyboard selected by the input method. You can use Vim under Lin. Enter :d IG in vim to find the respective cs,ch,cd,cc. Enter vim insert mode by <CTRL-K>, then enter it separately

Program code:

Object PokerFace extends Enumeration {

Type Pokerface=value

Val Spades=value ("♠")

Val Hearts=value ("♥")

Val diamonds=value ("? ")

Val clubs=value ("? ")

def main (args:array[string]): Unit = {

for (poker <-pokerface.values)

println (poker)

}

}

Operation Result:

?

?

7. Implement a function to check if the color of a card is red

Program code:

  1. Object Card extends enumeration with app{
  2. Val spades=value ("?")
  3. Val hearts=value ("?")
  4. Val diamonds=value ("?")
  5. Val clubs=value ("?")
  6. Type Card=value
  7. def color (c:card) ={
  8. if (c==card.hearts| | C==card.diamonds)
  9. println ("Red")
  10. Else
  11. println ("Black")
  12. }
  13. Color (Spades)
  14. Color (DIAMONDS)
  15. }

Operation Result:

Black

Red

8. Write an enumeration that describes the 8 corners of an RGB cube. ID uses color values (for example: Red is 0xff0000)

Description: RGB If the 8-bit representation, red is 0xff0000, Green is 0x00ff00, Blue is 0x0000ff. And so on, 8 vertices were (0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) (1,0,1) (1,1,0) (1,1,1)

Program code:

Object rgb extends enumeration with app {  

val red = value (0xff0000, "RED")   

val black  = value (0x000000, "Black")   

val green =  Value (0x00ff00, "Green")   

val cyan = value (0X00FFFF, "Cyan")   

val yellow = value (0xffff00, "YELLOW")   

val white = value (0xFFFFFF, "white")   

val blue = value (0X0000FF, "BLUE")   

val magenta = value (0xff00ff, "MAGENTA")   

}  

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 (vi) 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.