Objects & Exercises in Scala |
1. Write a Conversions object and add the Inchestocentimeters,gallonstoliters and Milestokilometers methods
Program code:
- Object Conversions {
- Private Val i2c=30.48
- Private Val g2l=3.785411784
- Private Val m2k=1.609344
- def inchestocentimeters (inch:double):D ouble={
- Inch*i2c
- }
- def gallonstoliters (gallon:double):D ouble={
- gallon*g2l
- }
- def milestokilometers (mile:double):D ouble={
- mile*m2k
- }
- def main (args:array[string]): Unit = {
- Val inch=15
- Val gallon=15
- Val mile=15
- println (inch+ " ft = " +inchestocentimeters (inch))
- println (gallon+ " gal = " +gallonstoliters (gallon))
- println (mile+ " km = " +milestokilometers (mile))
- }
- }
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:
- Abstract class unitconversion {
- def Converse (from:double):D ouble
- }
- Object inchestocentimeters extends unitconversion{
- Private Val i2c=30.48
- Override Def Converse (inche:double):D ouble={
- Inche*i2c
- }
- }
- Object gallonstoliters extends unitconversion{
- Private Val g2l=3.785311784
- Override Def Converse (gallon:double):D ouble={
- G2l*gallon
- }
- }
- Object milestokilometers extends unitconversion{
- Private Val m2k=1.609344
- Override Def Converse (mile:double):D ouble={
- M2k*mile
- }
- }
- Object test{
- def main (args:array[string]): Unit = {
- Val inche=10; Val gallon=10; Val mile=10
- println (inche+ " ft = " +inchestocentimeters.converse (inche) + " cm ")
- println (gallon+ " gal = " +gallonstoliters.converse (gallon) + " liter ")
- println (mile+ " km = " +milestokilometers.converse (mile) + " km ")
- }
- }
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:
- Object Card extends enumeration with app{
- Val spades=value ("?")
- Val hearts=value ("?")
- Val diamonds=value ("?")
- Val clubs=value ("?")
- Type Card=value
- def color (c:card) ={
- if (c==card.hearts| | C==card.diamonds)
- println ("Red")
- Else
- println ("Black")
- }
- Color (Spades)
- Color (DIAMONDS)
- }
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