1 / 31

Class Example - Rationals

Rational numbers are represented by the ratio of two integers, a numerator and a denominator. This article explains how to represent and perform operations on rational numbers using a Rational class. It also discusses methods for reducing rational numbers to their lowest terms.

gchilders
Télécharger la présentation

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 = new Rational(); c.n = n * b.d + b.n * d; c.d = d * b.d; return c.reduce(); } New add method

  18. Methods • A program takes input, does some calculation, and produces output. • A method is a miniature program – it also is given input, does some calculation, and produces some output. • Most methods are called, or invoked, by other methods. • The main method is invoked when the program is run.

  19. Method Declarations • Every method is part of a class. • Classes include method declarations. • The declaration tells us who can call the method, what kind of value the method returns (if any), the name of the method, the method's parameters, and its code. • The parameters are how we pass information into the method.

  20. Example public Rational add(Rational b) { Rational c = new Rational(); c.n = n * b.d + b.n * d; c.d = d * b.d; return c.reduce(); } This method is public, i.e., anyone can call it. It returns a Rational number, it's name is “add”, and it takes one argument, also a Rational number.

  21. Syntax of a Method Declaration <accessor> <return type> <name> (<parameter list>) { <code> } where <accessor> is one of public, private, protected, or is elided. <return type> is a type, but may be void. <name> is the name of the method. <parameter list> is a comma-separated list of parameters. <code> is the code of the method.

  22. Functions and Procedures • A method may or may not return a value. • Methods that do return values are often called functions. • Methods that do not return values are often called procedures. • A procedure always has the return type void, which is a special type that has no values. • A procedure should not try to return a value in its code.

  23. Invoking a Method • The syntax for invoking an object method is: <object>.<method>(<arguments>) • Object methods are called on objects. • <method> is the name of the method to be called. • <arguments> are the values that are passed into the method, i.e., the values that the corresponding parameters are initialized to.

  24. Invoking a Function • A function is usually invoked by using it in an expression. • A function can appear anywhere that a variable or a literal of its return type can appear in an expression, e.g., a boolean function can occur anywhere a boolean variable or literal could occur. • The function is evaluated, and the return value is used in place of the function call in the expression.

  25. Invoking a Function, cnt'd • A function may not be used on the left-hand side of an assignment statement, e.g., a.add(b) = c; is not legal. • A function may be used on the right-hand side of an assignment statement, e.g., c = a.add(b);

  26. Invoking a Procedure • Procedures are usually invoked by making them statements, i.e., adding a semicolon after the call. • Procedure invocations may not appear as part of an expression. • If the return value of a method is void, do not use it in an expression. • A function may be called as a statement, by adding a semicolon after. Its value is simply ignored.

  27. Parameters • Methods may have parameters. • Parameters are used to pass information into the method. • Each parameter is specified by giving it a type and an identifier, just like variables. • The parameter list is a comma-separated list of parameters. • A method may have no parameters, but it still needs parentheses.

  28. Method invocation • When a method is called, the arguments are evaluated and their values are used to initialize the parameters. • The arguments and parameters must match in number and type. If possible, type conversion will be done on the values. • Control is transferred to the body of the method. • The methods returns a value (if any) to the calling method, using the return statement.

  29. Method code • The code of a method is a miniature program. • It may contain local variables. • When the method is called, the parameters are initialized to the values of the arguments, and the code of the method is executed. • A return statement will cause the method to be terminated. If it is of the form return <value> then value must be of the right type and is returned as the value of the method.

  30. Arguments and Parameters • (Actual) arguments are part of the method invocation. • Arguments are expressions. They may be variables, literals, or use operators. • The argument list does not contain types. • (Formal) Parameters are part of the method declaration. • The parameter list does contain types.

  31. Message and Methods • Different classes may have methods with the same name and number and types of arguments (the method signature). • A message of the same name (and arguments) may be called upon (or sent to) objects of different types, e.g., snowman.moveForward, iceSkater.moveForward() • The method invoked depends on both the type of the object and the message sent.

More Related