Class Example - Rationals

Slides:



Advertisements
Similar presentations
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Advertisements

Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Abstract Data Type Fraction Example
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
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.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
1 Gentle Introduction to Programming Session 5: Memory Model, Object Oriented Programming.
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
Hello, world! Dissect HelloWorld.java Compile it Run it.
5-6 Radical Expressions Objectives Students will be able to: 1)Simplify radical expressions 2)Add, subtract, multiply, and divide radical expressions.
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.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
COSC 1P03 Data Structures and Abstraction 4.1 Abstract Data Types The advantage of a bad memory is that one enjoys several times the same good things for.
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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Mathematical Calculations in Java Mrs. G. Chapman.
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.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
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) =
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
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,
Lesson 2-5. Refresher Simplify the following expressions
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.
© 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 22, 2016.
Objects and Classes Eric Roberts CS 106A January 25, 2010.
Simplifying Radical Expressions Objective: Add, subtract, multiply, divide, and simplify radical expressions.
Outline Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited.
Lecture 5 – Function (Part 2) FTMK, UTeM – Sem /2014.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Section 5.3 The Rational Numbers.
Looking Back at Exponents
MA/CSSE 473 Day 06 Euclid's Algorithm.
MA/CSSE 473 Day 06 Euclid's Algorithm.
COMPSCI 107 Computer Science Fundamentals
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Variables and Arithmetic Operators in JavaScript
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Lecture Set 4 Data Types and Variables
Object Oriented Programming
Computing Adjusted Quiz Total Score
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
Java Enter your code from FRQ to Shell
You can work in groups on this program.
Arithmetic Operators in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Without a calculator, simplify the expressions:
Arithmetic Operators in C
More on Classes and Objects
Section 5.3 The Rational Numbers
Simplifying Complex Rational Expressions
Recursive GCD Demo public class Euclid {
Adding and Subtracting Rational Numbers
Week 4 Lecture-2 Chapter 6 (Methods).
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Write about: What did you do over the LONG weekend?
Java 1/31/2017 Back to Objects.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Fractions, Decimals, & Percents
Class rational part2.
DIVIDE TWO RATIONAL NUMBERS
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 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.

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.add(b);

Rational Operators 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)

Representing Rational Numbers 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).

A Rational Method 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.

Other Methods 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) { c.n = n * b.n;

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

Rational Class 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) { ... } }

Additional Methods 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;

Testing the Rational Class 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

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 private int gcd(int a, int b) { while (b != 0) { int t = b; b = a % b; a = t; } return a;

Reduce method 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 (cont'd) 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.

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