Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.

Slides:



Advertisements
Similar presentations
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Advertisements

Abstract Data Type Fraction Example
Thinking Mathematically
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Abstract Data Type. COMP104 Slide 2 Summary l A class can be used not only to combine data but also to combine data and functions into a single (compound)
Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.
MTH 092 Section 12.1 Simplifying Rational Expressions Section 12.2
BY MISS FARAH ADIBAH ADNAN IMK
MATH 2A CHAPTER EIGHT POWERPOINT PRESENTATION
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Copyright 2013, 2010, 2007, Pearson, Education, Inc. Section 5.3 The Rational Numbers.
Thinking Mathematically
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Converting, Comparing and Ordering Rational Numbers
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
Case Study - Fractions Timothy Budd Oregon State University.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
Introduction to Classes in C++
Section 8.8.  In this lesson you will learn to add, subtract, multiply, and divide rational expressions. In the previous lesson you combined a rational.
Improper Fractions, Mixed Numbers, and Decimal Numbers
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Add & Subtract Rationals – Common Denominator To add or subtract rational expressions with common denominators: 1) Add or subtract the numerators 2) Write.
MA/CSSE 473 Day 06 Euclid's Algorithm. MA/CSSE 473 Day 06 Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm.
Fractions.
Unit 2: Integers Unit Review. Multiplying Integers The product of two integers with the same sign is a positive. Eg: (+6) x (+4) = +24; (-18) x (-3) =
Multiplying Fractions. When we multiply a fraction by an integer we: multiply by the numerator and divide by the denominator For example, × = 54.
Chapter 2 Real Numbers and algebraic expressions ©2002 by R. Villar All Rights Reserved Re-engineered by Mistah Flynn 2015.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Drill #81: Solve each equation or inequality
Complex Number System Reals Rationals (fractions, decimals) Integers (…, -1, -2, 0, 1, 2, …) Whole (0, 1, 2, …) Natural (1, 2, …) Irrationals.
Lesson 2-5. Refresher Simplify the following expressions
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Warm-up Divide. 1. (x 6 – x 5 + x 4 ) ÷ x 2 2. (9c 4 + 6c 3 – c 2 ) ÷ 3c 2 3. (x 2 – 5x + 6) ÷ (x – 2) 4. (2x 2 + 3x – 11) ÷ (x – 3)
Objects and Classes Eric Roberts CS 106A January 25, 2010.
Simplifying Radical Expressions Objective: Add, subtract, multiply, divide, and simplify radical expressions.
A Sample Program #include using namespace std; int main(void) { cout
7 th Grade Math Vocabulary Word, Definition, Model Emery Unit 2.
Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.
Section 5.3 The Rational Numbers.
Chapter 19: Recursion.
MA/CSSE 473 Day 06 Euclid's Algorithm.
MA/CSSE 473 Day 06 Euclid's Algorithm.
COMPSCI 107 Computer Science Fundamentals
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Adding and Subtracting Rational Numbers
Operators and Expressions
Arithmetic Operators in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Fraction Abstract Data Type
Real Numbers Lesson 1.1.
Without a calculator, simplify the expressions:
The Real Numbers And Their Representations
Arithmetic Operators in C
Adding and Subtracting Rational Numbers
Section 5.3 The Rational Numbers
Simplifying Complex Rational Expressions
Pointers Lecture 2 Tue, Jan 24, 2006.
Simplifying Square Roots
Adding and Subtracting Rational Numbers
Adding and Subtracting Rational Numbers
Write about: What did you do over the LONG weekend?
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Class Example - Rationals
Fractions, Decimals, & Percents
Warm-ups: Simplify the following
DIVIDE TWO RATIONAL NUMBERS
UNIT 1 Chapter 1-1 Number Systems.
Presentation transcript:

Class Example - Rationals 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.

Rationals (cont'd) Rational numbers are not a built-in type like int s and double s. 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.

Rational Methods 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.plus(b);

Rational Operators n 1 /d 1 + n 2 /d 2 = (n 1 * d 2 + n 2 * d 1 ) / (d 1 * d 2 ) n 1 /d 1 - n 2 /d 2 = (n 1 * d 2 - n 2 * d 1 ) / (d 1 * d 2 ) n 1 /d 1 * n 2 /d 2 = (n 1 * n 2 ) / (d 1 * d 2 ) n 1 /d 1 / n 2 /d 2 = (n 1 * d 2 ) / (n 2 * d 1 )

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

A Rational Method Rational Rational::add(Rational b) { Rational c; 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.

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

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

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

Additional Methods For the time being we will also write two additional methods so we can test our class: void Rational::set(int n0, d0) { n = n0; d = d0; } void Rational::print() { cout << n << “/” << d; }

Testing the Rational Class The following main program will test the methods of the Rational class: int main(void) { Rational a, b, c; a.set(1,3); b.set(2,3); c = a.plus(b); c.print(); cout << endl; } The result of this program is: 9/9

Oops! 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.

Calculating the GCD 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).

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

New Class Definition We make this be a private method of the class, as it is not called from outside the class: class Rational { public: Rational add(Rational b); Rational subtract(Rational b); Rational multiply(Rational b); Rational divide(Rational b); private: int n,d; int gcd(int a, int b); };

New add method Rational Rational::add(Rational b) { Rational c; c.n = n * b.d + b.n * d; c.d = d * b.d; int g = gcd(c.n, c.d); c.n = c.n / g; c.d = c.d / g; return c; }