1 / 17

Class Example - Rationals

heman
Download Presentation

Class Example - Rationals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational numbers which cannot be expressed as the ratio of two integers, such as Pi. Rational numbers are usually put into lowest terms, which is done by dividing the numerator and denominator by the greatest common denominator (GCD) of both: 4/6 -> 2/3. Class Example - Rationals

  2. Rational numbers are not a built-in type like ints and doubles. Suppose that we what to work with them as a new type. We can create a new Rational class that represents rational numbers and incorporates the usual operators that we apply to numbers: add, subtract, multiply, and divide. In addition, we want to be able to read and write rational numbers in human-readable form. Rationals (cont'd)

  3. The arithmetic operators all take two operands (both rational) and return a third value (also a rational). When we implement this, one of the operands will be the object that the method is called upon and the second operand will be explicitly passed as an input parameter to the method. The return value will be used to return the result. For example, to add two rational numbers together we say: c = a.add(b); Rational Methods

  4. n1/d1 + n2/d2 = (n1 * d2 + n2 * d1) / (d1 * d2) n1/d1 - n2/d2 = (n1 * d2 - n2 * d1) / (d1 * d2) n1/d1 * n2/d2 = (n1 * n2) / (d1 * d2) n1/d1 / n2/d2 = (n1 * d2) / (n2 * d1) Rational Operators

  5. To represent a rational number, we will need to store the numerator and the denominator, both ints. private int n, d; We use the private modifier so that the parts cannot be accessed from outside the Rational class (information hiding). Representing Rational Numbers

  6. public Rational add(Rational b) { Rational c = new Rational(); c.n = n * b.d + b.n * d; c.d = d * b.d; return c; } The variables n and d (which are not qualified) refer to the values of the object upon which the add methods is called. In the example c = a.add(b); the add method is called on object a, so n and d refer to a's fields. A Rational Method

  7. public Rational subtract(Rational b) { Rational c = new Rational(); c.n = n * b.d - b.n * d; c.d = d * b.d; return c; } public Rational multiply(Rational b) { Rational c = new Rational(); c.n = n * b.n; c.d = d * b.d; return c; } Other Methods

  8. public Rational divide(Rational b) { Rational c = new Rational(); c.n = n * b.d; c.d = b.n * d; return c; } Other Methods (cont'd)

  9. public class Rational { private int n,d; public Rational add(Rational b) { ... } public Rational subtract(Rational b) { ...} public Rational multiply(Rational b) { ... } public Rational divide(Rational b) { ... } } Rational Class

  10. For the time being we will also write two additional methods so we can test our class: public void set(int n0, int d0) { n = n0; d = d0; } public String toString() { return n + "/" + d; } Additional Methods

  11. The following main program will test the methods of the Rational class: public class Main2 { public static void main(String args[]) { Rational a = new Rational(); Rational b = new Rational(); a.set(1,3); b.set(2,3); Rational c = a.add(b); System.out.println(c); } } The result of this program is: 9/9 Testing the Rational Class

  12. What have we forgotten? The rational number should be represented in lowest terms, so 9/9 isn't quite right. The GCD of the numerator and the denominator is 9, so dividing both by the GCD 9, yields 1/1, which is the rational number in lowest terms. Oops!

  13. Finding the GCD is one of the oldest known algorithms, ascribed to Euclid (hence Euclid's algorithm). We find the GCD by repeatedly taking the remainder of one number divided by the other until one of the terms becomes 0. The other term is the gcd. This works because the gcd(x,y) = gcd(x, x % y). Calculating the GCD

  14. private int gcd(int a, int b) { while (b != 0) { int t = b; b = a % b; a = t; } return a; } Euclid's Algorithm

  15. To avoid writing the same code over and over again, we also write a reduce method, that takes a Rational number, reduces it to lowest form, and returns it: private Rational reduce() { int g = gcd(n,d); n /= g; d /= g; return this; } Reduce method

  16. Two things to note: Both reduce and gcd are private methods. That is, they can only be called by other methods of the Rational class, and not from any other class. reduce returns this, which refers to the (anonymous) object upon which reduce was called. Reduce method (cont'd)

  17. public Rational add(Rational b) { Rational c; c.n = n * b.d + b.n * d; c.d = d * b.d; return c.reduce(); } New add method

More Related