19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

586 Chapter 15 Abstract Classes and Interfaces<br />

create a Rational<br />

create a Rational<br />

add<br />

5 Rational r1 = new Rational(4, 2);<br />

6 Rational r2 = new Rational(2, 3);<br />

7<br />

8 // Display results<br />

9 System.out.println(r1 + " + " + r2 + " = " + r1.add(r2) );<br />

10 System.out.println(r1 + " - " + r2 + " = " + r1.subtract(r2) );<br />

11 System.out.println(r1 + " * " + r2 + " = " + r1.multiply(r2) );<br />

12 System.out.println(r1 + " / " + r2 + " = " + r1.divide(r2) );<br />

13 System.out.println(r2 + " is " + r2.doubleValue() );<br />

14 }<br />

15 }<br />

2 + 2/3 = 8/3<br />

2 - 2/3 = 4/3<br />

2 * 2/3 = 4/3<br />

2 / 2/3 = 3<br />

2/3 is 0.6666666666666666<br />

The main method creates two rational numbers, r1 and r2 (lines 5–6), and displays the<br />

results of r1 + r2, r1 - r2, r1 x r2, and r1 / r2 (lines 9–12). To perform r1 + r2,<br />

invoke r1.add(r2) <strong>to</strong> return a new Rational object. Similarly, invoke r1.subtract(r2)<br />

for r1 - r2, r1.multiply(r2) for r1 x r2 , and r1.divide(r2) for r1 / r2.<br />

The doubleValue() method displays the double value of r2 (line 13). The double-<br />

Value() method is defined in java.lang.Number and overridden in Rational.<br />

Note that when a string is concatenated with an object using the plus sign (+), the object’s<br />

string representation from the <strong>to</strong>String() method is used <strong>to</strong> concatenate with the string. So<br />

r1 + " + " + r2 + " = " + r1.add(r2) is equivalent <strong>to</strong> r1.<strong>to</strong>String() + " + "<br />

+ r2.<strong>to</strong>String() + " = " + r1.add(r2).<strong>to</strong>String().<br />

The Rational class is implemented in Listing 15.13.<br />

LISTING 15.13<br />

Rational.java<br />

1 public class Rational extends Number implements Comparable {<br />

2 // Data fields for numera<strong>to</strong>r and denomina<strong>to</strong>r<br />

3 private long numera<strong>to</strong>r = 0;<br />

4 private long denomina<strong>to</strong>r = 1;<br />

5<br />

6 /** Construct a rational with default properties */<br />

7 public Rational() {<br />

8 this(0, 1);<br />

9 }<br />

10<br />

11 /** Construct a rational with specified numera<strong>to</strong>r and denomina<strong>to</strong>r */<br />

12 public Rational(long numera<strong>to</strong>r, long denomina<strong>to</strong>r) {<br />

13 long gcd = gcd(numera<strong>to</strong>r, denomina<strong>to</strong>r);<br />

14 this.numera<strong>to</strong>r = ((denomina<strong>to</strong>r > 0) ? 1 : -1) * numera<strong>to</strong>r / gcd;<br />

15 this.denomina<strong>to</strong>r = Math.abs(denomina<strong>to</strong>r) / gcd;<br />

16 }<br />

17<br />

18 /** Find GCD of two numbers */<br />

19 private static long gcd(long n, long d) {<br />

20 long n1 = Math.abs(n);<br />

21 long n2 = Math.abs(d);<br />

22 int gcd = 1;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!