Scala Practice 3

First, the functional objects

1.1 rational class specifications and create

  • Rational class derived from the rational ( Rational Number ), denoted n- (molecules) / D number (denominator), and at the same time rational operations (addition, subtraction) modeling further includes a return element of the characteristics.

Under no examples of modeling:

scala> val oneHalf=new Rational(1,2)
oneHalf: Rational = 1/2
scala> val twoThirds=new Rational(2,3)
twoThirds: Rational = 2/3
scala> (oneHalf /7)+(1  twoThirds)
<console>:13: error: value / is not a member of Rational
       (oneHalf /7)+(1  twoThirds)
             ^
<console>:13: error: value twoThirds is not a member of Int
       (oneHalf /7)+(1  twoThirds)
scala> (oneHalf /7)+1
<console>:13: error: value / is not a member of Rational
       (oneHalf /7)+1

  

 

  • Rational creation, in parentheses after the class name of the n- , d two class parameters . Scala compiler will collect these two parameters and create a class with the same parameters of the primary constructor .
Class Rational(n:Int,d:Int)
scala> class Rational(n:Int,d:Int){
     | println("Created"+n+"/"+d)
     | }
defined class Rational
 
scala> new Rational(1,2)
Created1/2
res21: Rational = Rational@6fbfd6ed

 

  By default, the Rational class extends java.lang.Object class toString achieve will only print the class name, @ and hexadecimal numbers.

  • In order to print the detailed molecular denominator, you can overload Rational class toString achieved.
scala> class Rational(n:Int,d:Int){
     | override def toString = n+"/"+d }
defined class Rational
 
scala> val x =new Rational(1,3)
x: Rational = 1/3

  

 

1.2 rational prerequisite

     One of the advantages of object-oriented programming is that it allows you to encapsulate data within the object to ensure the validity of data throughout the life cycle. It means that you must ensure rational validity of class to build a rational number,

I.e., the denominator is not 0 , it should be configured to define a main prerequisite explained d is not 0 , a prerequisite is passed to the constructor method or the limit value , that must meet the needs of the caller.

  • One method is to use require , such as:

    

scala> class Rational(n:Int,d:Int){
     | require(d!=0)
     | override def toString = n+"/"+d
     | }
defined class Rational
 
scala> new Rational(2,0)
java.lang.IllegalArgumentException: requirement failed
  at scala.Predef$.require(Predef.scala:268)
  ... 29 elided
 
scala> new Rational(2,9)
res13: Rational = 2/9

Note: Require bring a Boolean parameter method. If the incoming value is true, the require the return to normal, otherwise, the require throws IllegalArgumentException prevent objects are constructed.

 

1.3  Add field

  In order to maintain Rational immutability, the Add method can not be applied to the incoming rational numbers themselves, but must create and return to full pay Rational accumulated value.

class Rational(n:Int,d:Int){
    require(d!=0)
    val number:Int=n
    val denom:Int=d
  override def toString =number + "/"+denom
  def add(that:Rational):Rational=
  new Rational(
     number*that.denom+that.number*denom,
     denom*that.denom
   )
  }
scala>val oneHalf=new Rational(1,2)
oneHalf: Rational = 1/2

  

 

1.4  from this point

   Keyword this pointing to the object instance currently executing method is called, or if in the constructor, then, is being constructed object instance. as follows:

scala> class Rational(n:Int,d:Int){
     | require(d!=0)
     | val number:Int=n
     | val denom:Int=d
     | override def toString =number + "/"+denom
     | def add(that:Rational):Rational=
     | new Rational(
     |     number*that.denom+that.number*denom,
     | denom*that.denom
     |    )
     |     def lessThan(that:Rational)=this.number*that.denom<that.number*this.denom
     | def max(that:Rational)=if(this.lessThan(that)) that else this
     |   }
defined class Rational
 
scala> new Rational(1,2)
res42: Rational = 1/2
scala> new Rational(1,4)
res43: Rational = 1/4
scala> res43.lessThan(res42)
res44: Boolean = true
scala> res43.max(res42)
res45: Rational = 1/2

  

1.5  auxiliary builder

  • Scala constructor than in primary structure is referred to as the secondary builder . Effect: to 5/1 , can be written Rational ( . 5 ), which requires a Rational add auxiliary builder preset denominator 1

 

class Rational(n:Int,d:Int){
   require(d!=0)
   val number:Int=n
   val denom:Int=d
   def this(n:Int) =this(n,1)//辅助构造器
  
  override def toString =number + "/"+denom
  def add(that:Rational):Rational=
  new Rational(
     number*that.denom+that.number*denom,
     denom*that.denom
   )
  }

  

1.6  private fields and methods

  • To simplify about (normalized cell generator) to undifferentiated simplest form, i.e., ( 66/42 ----> 11/7 ) , the introduction of private fields and methods of the greatest common divisor.
 class Rational(n:Int,d:Int){
   require(d!=0)
   private val g =gcd(n.abs,d.abs)
   val number=n/g
   val denom=d/g
   def this(n:Int) =this(n,1)
   def add(that:Rational):Rational=
   new Rational(
     number*that.denom+that.number*denom,
     denom*that.denom
   )
    override def toString =number + "/"+denom
    private def gcd(a:Int,b:Int):Int =
    if(b==0) a else gcd(b,a%b)
  }
 
scala> new Rational(99,66)
res23: Rational = 3/2
 

  

17.  Defined Operators

In Scala common mathematical notation by + Alternatively add method, because Scala where + is a valid identifier, can be used + defined method name.

class Rational(n:Int,d:Int){
    require(d!=0)
    private val g =gcd(n.abs,d.abs)
    val number=n/g
    val denom=d/g
    def this(n:Int) =this(n,1)
  
 
   def +(that:Rational):Rational=
   new Rational(
     number*that.denom+that.number*denom,
     denom*that.denom
   )
    override def toString =number + "/"+denom
    private def gcd(a:Int,b:Int):Int =
    if(b==0) a else gcd(b,a%b)
  }

  

The results are as follows:

scala> new Rational(4,9)
res30: Rational = 4/9
scala> new Rational(4,8)
res31: Rational = 1/2
scala> res30+res31
res32: Rational = 17/18

  

1.8.  Method overloading

In rational operations are defined above, the introduction operation, and the use of rational integers between overloaded.

 

class Rational(n:Int,d:Int){
 require(d!=0)
 private val g =gcd(n.abs,d.abs)
 val number=n/g
 val denom=d/g
    def this(n:Int) =this(n,1)
    def +(that:Rational):Rational=
    new Rational(
     number*that.denom+that.number*denom,
     denom*that.denom
    )
def +(i:Int):Rational=
      new Rational(
     number+i*denom,denom
    )
    override def toString =number + "/"+denom
    private def gcd(a:Int,b:Int):Int =
    if(b==0) a else gcd(b,a%b)
  }

  

The results are as follows:

scala> new Rational(1,2)
res37: Rational = 1/2
scala> 2
res38: Int = 2
scala> res37+res38
res39: Rational = 5/2

  

 

Guess you like

Origin www.cnblogs.com/0205gt/p/10979717.html