0% found this document useful (0 votes)
3 views10 pages

09 RationalClass

Chapter 6 of 'The Art and Science of Java' introduces the Rational class, which represents rational numbers as the quotient of two integers, allowing for exact calculations with fractions. The chapter details the implementation of the Rational class, including overloaded constructors and methods for standard arithmetic operations such as addition, subtraction, multiplication, and division. It emphasizes the benefits of using rational numbers over floating-point representations for precise calculations.

Uploaded by

hakanbaykann10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

09 RationalClass

Chapter 6 of 'The Art and Science of Java' introduces the Rational class, which represents rational numbers as the quotient of two integers, allowing for exact calculations with fractions. The chapter details the implementation of the Rational class, including overloaded constructors and methods for standard arithmetic operations such as addition, subtraction, multiplication, and division. It emphasizes the benefits of using rational numbers over floating-point representations for precise calculations.

Uploaded by

hakanbaykann10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

The Art and Science of

Java
An Introduction
ERIC S. ROBERTS to Computer Science
CHAPTER 6

Objects and Classes


To beautify life is to give it an object.
—José Martí, On Oscar Wilde, 1888

CS102 @ Özyeğin University

Slides are adapted from the originals available at


http://www-cs-faculty.stanford.edu/~eroberts/books/ArtAndScienceOfJava/

Chapter 6—Objects and Classes


Rational Numbers
• Rational class that represents rational numbers, which are
simply the quotient of two integers.

• Rational numbers can be useful in cases in which you need


exact calculation with fractions. Even if you use a double,
the floating-point number 0.1 is represented internally as an
approximation. The rational number 1 / 10 is exact.
• Rational numbers support the standard arithmetic operations:
Addition: Multiplication:
a c ad + bc a x c ac
+ = =
b d bd b d bd

Subtraction: Division:
a c ad – bc a .. c ad
– = =
b d bd b d bc
Implementing the Rational Class
• The next five slides show the code for the Rational class
along with some brief annotations.
• As you read through the code, the following features are
worth special attention:
– The constructors for the class are overloaded. Calling the constructor
with no argument creates a Rational initialized to 0, calling it with
one argument creates a Rational equal to that integer, and calling it
with two arguments creates a fraction.
– The constructor makes sure that the numerator and denominator of any
Rational are always reduced to lowest terms. Moreover, since these
values never change once a new Rational is created, this property
will remain in force.
– The add, subtract, multiply, and divide methods are written so
that one of the operands is the receiver (signified by the keyword
this) and the other is passed as an argument. Thus to add r1 and r2
you would write:
r1.add(r2)
The Rational Class
/**
* The Rational class is used to represent rational numbers, which
* are defined to be the quotient of two integers.
*/ As always, the instance variables are private to this class.
public class Rational {
/* Private instance variables */
private int num; /* The numerator of this Rational */
private int den; /* The denominator of this Rational */

/** Creates a new Rational initialized to zero. */


public Rational() {
this(0); Calls the constructor that takes a single int argument.
}
These constructors are overloaded so that there is more than one way to create a Rational
/** value. These two versions invoke the primary constructor by using the keyword this.
* Creates a new Rational from the integer argument.
* @param n The initial value
*/
public Rational(int n) {
this(n, 1); Calls the constructor that takes two int arguments.
}
page 1 of 5 skip code
The Rational Class
/**
/** The primary constructor
* creates a new Rational
* Creates a newclass
The Rational Rational withto
is used the value x /
represent y.
rational numbers, which
from the numerator and
* @param x The numerator of the rational number denominator. The call
* are defined to be the quotient of two integers.
*
*/ @param y The denominator of the rational number to gcd ensures that the
*/ fraction is reduced to
public class Rational { lowest terms.
public Rational(int x, int y) {
int g =
/** Creates a gcd(Math.abs(x), Math.abs(y));
new Rational initialized to zero. */
num = x / g; The add method creates
public Rational() { a new Rational object
den =
this(0);Math.abs(y) / g;
using the addition rule.
} if (y < 0) num = -num; The two rational values
} appear in this and r.
/**
/**
* Creates a new Rational from the integer argument.
*
* Adds the
@param n rational number
The initial r to this one and returns the sum.
value
*
*/@param r The rational number to be added
* public
@returnRational(int
The sum of the current number and r
n) {
*/ this(n, 1);
public Rational add(Rational r) {
}
return new Rational(this.num * r.den + r.num * this.den,
this.den * r.den);
}

page 2 of 5 skip code


The Rational Class
/**
* Subtracts
Creates a the
new rational
Rational number
with the r from
valuethis x / one.
y.
* @param rx The rational
numeratornumber of theto rational
be subtracted
number
* @return
@param yThe
Theresult
denominator
of subtracting
of the rationalr from the number current number
*/
public Rational
Rational(int subtract(Rational
x, int y) { r) {
int g =new
return gcd(Math.abs(x),
Rational(this.num Math.abs(y));
* r.den - r.num * this.den,
num = x / g; this.den * r.den);
} den = Math.abs(y) / g;
if (y < 0) num = -num;
/**}
* Multiplies this number by the rational number r.
/**
* @param r The rational number used as a multiplier
* @return
Adds theThe
rational
resultnumberof multiplying
r to thisthe onecurrent
and returns number the
bysum.
r
* @param r The rational number to be added
*/
* public
@returnRational
The sum multiply(Rational
of the current number r) {and r
*/ return new Rational(this.num * r.num, this.den * r.den);
public Rational add(Rational r) {
}
return new Rational(this.num * r.den + r.num * this.den,
this.den * r.den);
These methods (along with divide on the next page) work just like the add method but use
} a different formula. Note that these methods do have access to the components of r.

page 3 of 5 skip code


The Rational Class
/**
* Divides
Subtracts
this
thenumber
rational
by the
number
rational
r fromnumber
this one.r.
* @param r The rational number used to beas
subtracted
a divisor
* @return The result of dividing
subtracting
thercurrent
from the number
current by r number
*/
public Rational divide(Rational
subtract(Rational r)r)
{ {
return new Rational(this.num * r.den,
r.den -this.den
r.num * this.den,
* r.num);
} this.den * r.den);
}
/**
/**
* Creates a string representation of this rational number.
* @return
Multiplies
Thethis
string
number
representation
by the rational
of this number
rational
r. number
* @param r The rational number used as a multiplier
*/
* public
@returnString
The result
toString()
of multiplying
{ the current number by r
*/ if (den == 1) {
publicreturn
Rational
"" +
multiply(Rational
num; r) This
{ method converts the
return
} else {new Rational(this.num * r.num, this.den * r.den);
Rational number to its string
} return num + "/" + den; form. If the denominator is 1, the
number is displayed as an integer.
}
}

page 4 of 5 skip code


The Rational Class
/**
* Calculates
Divides this thenumber
greatest
by the
common
rational
divisor
number
usingr.Euclid's algorithm.
* @param x r First
The rational
integernumber used as a divisor
* @param
@returnyTheSecond
resultinteger
of dividing the current number by r
*/@return The greatest common divisor of x and y
*
*/public Rational divide(Rational r) {
private
returnintnew
gcd(int
Rational(this.num
x, int y) { * r.den, this.den * r.num);
} int r = x % y;
while (r != 0) { Euclid’s gcd method is declared to
/** x = y; be private because it is part of the
* Creates y =
a r;
string representation of this implementation
rational of this class and is
number.
* @return r =
Thex %
string
y; representation of never
thisused outside of it. number
rational
*/ }
public
return
String
y; toString() {
} if (den == 1) {
return "" + num;
} else {
} return num + "/" + den;
}
}

page 5 of 5 skip code


Simulating Rational Calculation
• The next slide works through all the steps in the calculation of
a simple program that adds three rational numbers.
1 1 1
+ +
2 3 6
• With rational arithmetic, the computation is exact. If you
write this same program using variables of type double, the
result looks like this:
RoundoffExample

1.0/2.0 + 1.0/3.0 + 1.0/6.0 = 0.999999999999999

• The simulation treats the Rational values as abstract objects.


Chapter 7 reprises the example showing the memory structure.
Adding Three Rational Values
public static
void run()
void{ main(String[] args) {
Rational a = new Rational(1, 2);
public Rational add(Rational
Rational(int x, int y)r){ { 36
5
Rational b = new Rational(1, 3);
int g = gcd(Math.abs(x), Math.abs(y));
Rational
public c = new Rational(1,
int y) 6);
num = Rational(int
return xnew
/ g; Rational( x, this.num{ * r.den + r.num * this.den ,
Rational =sum = a.add(b).add(c);
int= gMath.abs(y)
gcd(Math.abs(x), Math.abs(y));
den this.den
/ g; * r.den );
println(a
System.out.println(a
num = x +/ "g;+ " + b++""++""++bc++""+=""++csum); + " = " + sum);
if (y < 0) num = -num;
}} den = Math.abs(y)
} temporary / g; 36
6
if (y < 0) num = -num;
result a b c sum
} 5 1 1 1
this r 1
num 5 1 6 2 x 3 y 6 gnum1 1
this
den 6
num 2
3
5
1 x 1 y 62
3 den 1 6
g 3
den 1 6 36
5 36
6 36
1

TestRational
1/2 + 1/3 + 1/6 = 1

skip simulation

You might also like