페이지

2015년 11월 12일 목요일

Scala2e Chapter6 Basic Types and Operations 6.1 A specification for class Rational

A rational number is a number that can be expressed as a ratio n/d, where n and d are integers, except that d cannot be zero.
n is called the numerator and d the denominator.

Each rational number will be represented by on Rational object. when you add two Rational objects, you'll create a new Rational object to hold the sum.

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)
res0: Rational = 17/42





댓글 없음: