Writing a Fraction Class

Similar documents
Floa.ng Point : Introduc;on to Computer Systems 4 th Lecture, Sep. 10, Instructors: Randal E. Bryant and David R.

UNIT IV: DATA PATH DESIGN

Floats are not Reals. BIL 220 Introduc6on to Systems Programming Spring Instructors: Aykut & Erkut Erdem

Roadmap. The Interface CSE351 Winter Frac3onal Binary Numbers. Today s Topics. Floa3ng- Point Numbers. What is ?

61A LECTURE 14 MULTIPLE REPRESENTATIONS. Steven Tang and Eric Tzeng July 17, 2013

61A LECTURE 14 MULTIPLE REPRESENTATIONS

CS177 Python Programming. Recita4on 2 - Compu4ng with Numbers

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Sign & Magnitude vs 2 s Complement ( Signed Integer Representa:ons)

Data Representa+on: Floa+ng Point Numbers

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

Macro Assembler. Defini3on from h6p://

A Founda4on for Programming

How a computer runs a program. Binary Representa8on and Opera8ons on Binary Data. Opera8ng System 9/17/13. You can view binary file contents

CS 61C: Great Ideas in Computer Architecture Func%ons and Numbers

Floa=ng- Point Numbers

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Arithmetic Expressions 9/7/16 44

Outline. Integer representa+on and opera+ons Bit opera+ons Floa+ng point numbers. Reading Assignment:

Lecture 2: Number Representa2on

Outline. Integer representa+on and opera+ons Bit opera+ons Floa+ng point numbers. Reading Assignment:

Main Memory Organization

Representa7on of integers: unsigned and signed Conversion, cas7ng Expanding, trunca7ng Addi7on, nega7on, mul7plica7on, shiaing

CSI33 Data Structures

Programming Languages and Techniques (CIS120)

10.5 Polymorphism. def presentquestion(q) : q.display() response = input("your answer: ") print(q.checkanswer(response)) 11/11/16 39

Garbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; }

Proofs about Programs

Creating a new data type

Bits, Bytes, and Integers

Document Databases: MongoDB

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits?

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Digital Data

Lecture 19. Operators and Abstraction

Spa$al Analysis and Modeling (GIST 4302/5302) Guofeng Cao Department of Geosciences Texas Tech University

Local defini1ons. Func1on mul1ples- of

Program Verification (Rosen, Sections 5.5)

Rasteriza2on and Clipping

Another Simple Program: Adding Two Integers

Object Oriented Programming. Feb 2015

Programming Languages and Techniques (CIS120e)

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

CS 61C: Great Ideas in Computer Architecture Compilers and Floa-ng Point. Today s. Lecture

CSc 120. Introduction to Computer Programming II. 14: Stacks and Queues. Adapted from slides by Dr. Saumya Debray

LISP: LISt Processing

Number systems & Bit opera1ons. h#p://xkcd.com/99/

R workshop. Ac,on! (Operators and Func,ons)

Last Time. integer/float/string. import math math.sqrt() math.pi. Python Programming, 2/e 2

Introduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2

Common Loop Algorithms 9/21/16 42

Encapsula)on CMSC 202

Computer Programming for Engineering Applica4ons. Intro to Programming 10/2/13 ECE 175. Limita4ons of Reference by Value. The Concept of Pointers

The plan. Racket will return! Lecture will not recount every single feature of Java. Final project will be wri)ng a Racket interpreter in Java.

COMPSCI 105 S Principles of Computer Science. Classes 3

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

Introduction to Python. Fang (Cherry) Liu Ph.D. Scien5fic Compu5ng Consultant PACE GATECH

MIPS Instruc,ons CS 64: Computer Organiza,on and Design Logic Lecture #8

Lists in Lisp and Scheme

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types

UNIT V: CENTRAL PROCESSING UNIT

Why OO programming? want but aren t. Ø What are its components?

CS 101: Computer Programming and Utilization

61A LECTURE 12 OOP 2, INHERITANCE. Steven Tang and Eric Tzeng July 14, 2013

C Programming Primer 8/31/15 1

Chapter 8 SETS AND DICTIONARIES

Computational Mathematics with Python

Instruc=on Set Architecture

Python for Beginners. John B. Johnston Bioinforma3cs Engineer

Chapter 10 INHERITANCE 11/7/16 1

Vulnerability Analysis (III): Sta8c Analysis

Programming Environments

Review: Python Transi,on Warning

Implementing AddChoice()

Objec0ves. Gain understanding of what IDA Pro is and what it can do. Expose students to the tool GUI

CISC327 - So*ware Quality Assurance

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

Encapsulation in C++

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 7. Aykut Erdem, Erkut Erdem, Fuat Akal

Search Engines. Informa1on Retrieval in Prac1ce. Annota1ons by Michael L. Nelson

Binary Arithme-c CS 64: Computer Organiza-on and Design Logic Lecture #2

CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

61A LECTURE 8 SEQUENCES, ITERABLES

61A LECTURE 8 SEQUENCES, ITERABLES. Steven Tang and Eric Tzeng July 8, 2013

MARK CARPENTER, Ph.D.

CS261 Data Structures. Ordered Bag Dynamic Array Implementa;on

Agenda. Review 2/10/11. CS 61C: Great Ideas in Computer Architecture (Machine Structures)

Func%ons (Math) Func%on Defini%ons. Func%ons (Computer Sciences) Func%on = mappings or transforma%ons Math examples:

Array Basics: Outline

Computer Programming-I. Developed by: Strawberry

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

Macroanalysis. futureoftech/data-mining-classics-makesbeautiful-science

Lecture 18. Methods and Operations

Data Structures (list, dictionary, tuples, sets, strings)

Secure and Ef icient Iris and Fingerprint Identi ication

iden+fy and represent numbers using objects and pictorial representa+ons

CISC327 - So*ware Quality Assurance

GENG2140 Lecture 4: Introduc4on to Excel spreadsheets. A/Prof Bruce Gardiner School of Computer Science and SoDware Engineering 2012

Transcription:

Writing a Fraction Class So far we have worked with floa0ng-point numbers but computers store binary values, so not all real numbers can be represented precisely In applica0ons where the precision of real numbers is important, we can use ra#onal numbers to store exact values This helps to reduce or eliminate round-off errors that can occur when performing arithme0c opera0ons A ra0onal number is a number that can be expressed as a ra0o of two integers: 7/8 The top value is called the numerator and the bofom value, which cannot be zero, is called the denominator 83

Designing the Fraction Class We want to use our ra0onal numbers as we would use integers and floa0ng point values Thus, our Fraction class must perform the following opera0ons: 1. Create a ra0onal number 2. Access the numerator and denominator values, individually 3. Determine if the ra0onal number is nega0ve or zero 4. Perform normal mathema0cal opera0ons on two ra0onal numbers (addi0on, subtrac0on, mul0plica0on, division, exponen0a0on) 5. Logically compare two ra0onal numbers 6. Produce a string representa0on of the ra0onal number The objects of the Frac0on class will be immutable because none of the opera0ons modify the objects instance variables 84

Required Data Attributes Because a ra0onal number consists of two integers, we need two instance variables to store those values: self._numerator = 0 self._denominator = 1 At no 0me should the ra0onal number be converted to a floa0ng-point value or we will lose the precision gained from working with ra0onal numbers 85

Representing Values Equivalently Signed values Nega0ve and posi0ve ra0onal numbers each have two forms that can be used to specify the corresponding value Posi0ve values can be indicated as 1/2 or 1/ 2, and nega0ve values as 2/5 or 2/ 5 When performing an arithme0c opera0on or logically comparing two ra0onal numbers, it will be much easier if we have a single way to represent a nega0ve value For simplicity, we choose to set only the numerator to a nega0ve value when the ra0onal number is nega0ve, and both the numerator and denominator will be posi0ve integers when the ra0onal number is posi0ve 86

Representing Values Equivalently Equivalent frac0ons For example, 1/4 can be wrifen as 1/4, 2/8, 16/64, or 123/492 It will be much easier to perform the opera0on if the number is stored in reduced form 87

The Constructor (1) Because Fraction objects are immutable, their values must be set when they are created. This requires parameter variables for both the numerator and denominator def init (self, numerator, denominator) : The method must check for special cases: Zero denominators The number represents zero or a nega0ve number 88

The Constructor def init (self, numerator = 0, denominator = 1) : if denominator == 0 : raise ZeroDivisionError("Denominator cannot be zero.") if numerator == 0 : self._numerator = 0 self._denominator = 1 else : if (numerator < 0 and denominator >= 0 or numerator >= 0 and denominator < 0) : sign = -1 else : sign = 1 89

The Constructor a = abs(numerator) b = abs(denominator) while a % b!= 0 : tempa = a tempb = b a = tempb b = tempa % tempb self._numerator = abs(numerator) self._denominator = abs(denominator) #b # b * sign 90

Testing the Constructor frac1 = Fraction(1, 8) # Stored as 1/8 frac2 = Fraction(-2, -4) # Stored as 1/2 frac3 = Fraction(-2, 4) # Stored as -1/2 frac4 = Fraction(3, -7) # Stored as -3/7 frac5 = Fraction(0, 15) # Stored as 0/1 frac6 = Fraction(8, 0) # Error! exception is raised. 91

Comparing Fractions (1) In Python, we can define and implement methods that will be called automa0cally when a standard Python operator (+, *, ==, <) is applied to an instance of the class For example, to test whether two frac0ons are equal, we could implement a method: isequal() and use it as follows: if frac1.isequal(frac2) : print("the fractions are equal.") 92

Comparing Fractions (2) Of course, we would prefer to use the operator == This is achieved by defining the special method: eq (): def eq (self, rhsvalue) : return (self._numerator == rhsvalue.numerator and self._denominator == rhsvalue.denominator) Automa0cally calls this method when we compare two Fraction objects using the == operator: if frac1 == frac2 : # Calls frac1. eq (frac2) print("the fractions are equal.") 93

Special Methods Some special methods are called when an instance of the class is passed to a built-in func0on. For example, suppose you afempt to convert a Fraction object to a floa0ng point number using the float() func0on: x = float(frac1) Then the float () special method is called. Here is a defini0on of that method: def float (self) : return self._numerator / self._denominator 94

Common Special Methods 95

Common Special Methods 96

Addition of Fractions All of the arithme0c opera0ons that can be performed on a Fraction object should return the result in a new Fraction object For example, when the statement below is executed, frac1 should be added to frac2 and the result returned as a new Fraction object that is assigned to the newfrac variable newfrac = frac1 + frac2 97

Fractional Addition From elementary arithme0c, you know that two frac0ons must have a common denominator in order to add them. If they do not have a common denominator, we can s0ll add them using the formula: 98

Defining the Method For Addition def add (self, rhsvalue) : num = (self._numerator * rhsvalue._denominator + self._denominator * rhsvalue._numerator) den = self._denominator * rhsvalue._denominator return Fraction(num, den) 99

Logic: Less Than Note that a / b < c / d when d a < b c. (Mul0ply both sides with b d.) Based on this observa0on, the less than opera0on is implemented by the lt () method as follows: def lt (self, rhsvalue) : return (self._numerator * rhsvalue._denominator self._denominator * rhsvalue._numerator) 100

Fraction.py 101

Fraction.py 102

Fraction.py 103

Fraction.py 104

Checking Type To ensure that variables are the correct type, Python provides the built-in isinstance() func0on that can be used to check the type of object referenced by a variable. For example, the constructor for the Fraction class requires two integers class Fraction : def init (self, numerator, denominator) : if (not isinstance(numerator, int) or not isinstance(denominator, int)) : raise TypeError ("The numerator and denominator must be integers.") 105

Summary: Classes and Objects A class describes a set of objects with the same behavior Every class has a public interface: a collec0on of methods through which the objects of the class can be manipulated Encapsula0on is the act of providing a public interface and hiding the implementa0on details Encapsula0on enables changes in the implementa0on without affec0ng users of a class 106

Summary: Variables and Methods An object s instance variables store the data required for execu0ng its methods Each object of a class has its own set of instance variables An instance method can access the instance variables of the object on which it acts A private instance variable should only be accessed by the methods of its own class Class variables have a single copy of the variable shared among all of the instances of the class 107

Summary: Method Headers, Data Method Headers You can use method headers and method comments to specify the public interface of a class A mutator method changes the object on which it operates An accessor method does not change the object on which it operates Data Representa0on For each accessor method, an object must either store or compute the result Commonly, there is more than one way of represen0ng the data of an object, and you must make a choice Be sure that your data representa0on supports method calls in any order 108

Summary: Constructors A constructor ini0alizes the object s instance variables A constructor is invoked when an object is created The constructor is defined using the special method name: init () Default arguments can be used with a constructor to provide different ways of crea0ng an object 109

Summary: Method Implementation The object on which a method is applied is automa0cally passed to the self parameter variable of the method In a method, you access instance variables through the self parameter variable 110

Summary: Testing Classes A unit test verifies that a class works correctly in isola0on, outside a complete program To test a class, use an environment for interac0ve tes0ng, or write a tester class to execute test instruc0ons Determining the expected result in advance is an important part of tes0ng 111

Summary: Object Tracing Object tracing is used to visualize object behavior Write the methods on the front of a card, and the instance variables on the back Update the values of the instance variables when a mutator method is called 112

Summary: Patterns for Classes An instance variable for the total is updated in methods that increase or decrease the total amount A counter that counts events is incremented in methods that correspond to the events An object can collect other objects in a list An object property can be accessed with a gefer method and changed with a sefer method If your object can have one of several states that affect the behavior, supply an instance variable for the current state 113

Summary: Patterns for Classes To model a moving object, you need to store and update its posi0on 114

Summary: Object References An object reference specifies the loca0on of an object Mul0ple object variables can contain references to the same object Use the is and is not operators to test whether two variables are aliases The None reference refers to no object 115

Summary: Defining Special Methods To use a standard operator with objects, define the corresponding special method Define the special repr () method to create a string representa0on of an object 116