Lecture 05 – Classes.  A class provides the definition for the type of an object  Classes can store information in variables  Classes can provide methods.

Slides:



Advertisements
Similar presentations
Multiplying, Dividing, Adding, Subtracting Rational Expressions
Advertisements

Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Fractions. ADDING FRACTIONS  Build each fraction so that the denominators are the same  ADD the numerators  Place the sum of the two numerators on.
Classes 2 COMPSCI 105 SS 2015 Principles of Computer Science.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Mixed Numbers & Improper Fractions
Basic Algebraic Operations
Fractions.  The Numerator is the number on top  The Denominator is the number on bottom  The Factors of a number are those numbers that will divide.
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
Adding, subtracting, multiplying, and dividing fractions
Classes 2 COMPSCI 105 S Principles of Computer Science.
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
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.
Classes 3 COMPSCI 105 S Principles of Computer Science.
Exceptions 2 COMPSCI 105 S Principles of Computer Science.
Change between Mixed #’s & Improper Fractions. Write each fraction in simplest form
Fraction Review TAKE NOTES!!!!!!. Vocabulary Numerator: the number on top in a fraction Denominator: the number on bottom in a fraction Example: What.
Adding & Subtracting Whole Number and Fractions
Example #1 Add the whole numbers, then add the fractions. Rewrite with a common denominator.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
1 FRACTIONS. 2 VOCABULARY Fraction- a number that describes part of a whole or part of a set. Numerator- top number of a fraction that tells how many.
Mixed Numbers to Improper Fractions. Lets say you have a mixed number of 1 and 5/8 You can change this into the number 13/8. For converting mixed numbers.
Adding Fractions With Like and Unlike Denominators Presented By Nathaniel Thompson.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
By; Emma Maynard  The numerator is top # in a fraction. Example: 2/4 Numerator.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
JSON COMPSCI 105 SS 2015 Principles of Computer Science.
Definitions: One Step Literal Equations Solving a literal Equations requires doing the OPPOSITE operation to both sides to a variable. Unlike equations.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
0-3: Operations with Integers
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
JSON COMPSCI 105 S Principles of Computer Science.
Lists COMPSCI 105 S Principles of Computer Science.
Multiply the coefficients, the 2 and the -3 to get -6 a 3 * a 2 will be a 5, you add exponents when you multiply terms b 1 * b 4 will be b 5.
Lecture 09 – Classes.  At the end of this lecture, students should be able to:  Define a new class  Store state information about instances of the.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Dear Power point User, This power point will be best viewed as a slideshow. At the top of the page click on slideshow, then click from the beginning.
Are You Smarter Than a 5 th Grader? Fraction Edition.
Adding & Subtracting Fractions With Like Denominators.
+ Fractions. + Part of a whole + + Numerator How many pieces The number on the top of a fraction.
Improper Fractions and Mixed Number.  An improper fraction is a fraction in which the numerator is larger than the denominator. Example: 7/3 The numerator.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Unit 7 - Exponents.
Python programming - Defining Classes
0-4/0-5 Operations with Fractions
COMPSCI 107 Computer Science Fundamentals
COMPSCI 107 Computer Science Fundamentals
COMPSCI 107 Computer Science Fundamentals
Operations with Fractions and mixed numbers
Fraction XII Subtracting Unlike Denominators
Fraction Subtracting Unlike Denominators
Fractions Adding Unlike Denominators
Fraction XII Subtracting Unlike Denominators
Lesson 3.1 How do you solve one-step equations using subtraction, addition, division, and multiplication? Solve one-step equations by using inverse operations.
Fraction XII Subtracting Unlike Denominators
Grade 10 Academic (MPM2D) Unit 1: Linear System Solving First Degree Equations Mr. Choi © 2016 E. Choi – MPM2D - All Rights Reserved.
Fractions Adding Unlike Denominators
Fractions Adding and Subtracting Unlike Denominators
Fraction XI Subtracting Unlike Denominators
COMPUTER PROGRAMMING SKILLS
subtracting fractions with like denominators
Fractions, Decimals, Percents
Fraction XII Subtracting Unlike Denominators
Adding & subtracting Fractions With Common denominator.
CISC101 Reminders Assignment 3 due today.
More Basics of Python Common types of data we will work with
0-4/0-5 Operations with Fractions
def-ining a function A function as an execution control structure
Defining Functions.
Presentation transcript:

Lecture 05 – Classes

 A class provides the definition for the type of an object  Classes can store information in variables  Classes can provide methods that do something with the information  Example: A square class 2COMPSCI Principles of Computer Science class Square: def __init__(s): self.size = s from Geometry import Square side = 10 s = Square(side)

 Add a method to the class to calculate the perimeter of the square. The following code shows how the method may be used. 3COMPSCI Principles of Computer Science from Geometry import Square side = 10 s = Square(side) p = s.perimeter()

 Add a method to the class to change the size of the square using a scaling factor. For example, if you scale the square by a factor of 2, then the sides of the square will be twice as long. The following code shows how the method may be used. 4COMPSCI Principles of Computer Science from Geometry import Square side = 10 s = Square(side) big_s = s.scale(2)

 Write a function that compares the size of two squares given as parameters 5COMPSCI Principles of Computer Science def is_bigger(a, b): #returns true if a is larger than b #add your code here

 Add a method to the Square class that compares the size of the square with the size of another square. The method should be called bigger_than() and should accept a square as a parameter 6COMPSCI Principles of Computer Science from Geometry import Square s = Square(6) t = Square(7) if s.bigger_than(t): print(“The first square is bigger”)

 Write a class to represent fractions in Python  create a fraction  add  subtract  multiply  divide  text representation 7COMPSCI Principles of Computer Science ½ numerator denominator

8COMPSCI Principles of Computer Science methods state num: den: 7 8 methods state num: den: 3 4 methods state num: den: 1 2 x y z

 All classes must have a constructor  The constructor for a Fraction should store the numerator and the denominator 9COMPSCI Principles of Computer Science class Fraction: def __init__(self, top, bottom): self.num = top #numerator self.den = bottom #denominator

 So far, we can create a Fraction  We can access the state variables directly  Although not generally good practice to do so  What else can we do with Fractions?  Nothing yet. We need to write the functions first! 10COMPSCI Principles of Computer Science >>> x.num 3 >>> x.den 4 >>> x = Fraction(3, 4)

 All classes get a number of methods provided by default  Since default behaviour is not very useful, we should write our own versions of those methods 11COMPSCI Principles of Computer Science

 Often we want to use a string that combines literal text and information from variables  Example:  We can use string formatting to perform this task  Use curly braces within the string to signify a variable to be replaced  We can put the argument position in the curly braces 12COMPSCI Principles of Computer Science name = 'Andrew' greeting = 'Hello ' + name + '. How are you?' my_name = 'Andrew' greeting = 'Hello {name}. How are you?'.format(name=my_name) first = 'Andrew' second = 'Luxton-Reilly' greeting = 'Hello {0} {1}'.format(first, second)

 What is the output from the following code:  Rewrite the code so that it uses explicit variable names in the string. 13COMPSCI Principles of Computer Science sentence = 'Hello {2}. It is {0} today and it is {1}.'.format('Andrew', 'Wednesday', 'Cold')

 The __repr__ method produces an string that unambiguously describes the object  All classes should have a __repr__ function implemented  Ideally, the representation could be used to create the object  For example, a fraction created using Fraction(2, 3) should have a __repr__ method that returned 'Fraction(2, 3)' 14COMPSCI Principles of Computer Science def __repr__(self): return 'Fraction({0}, {1})'.format(self.num, self.den) >>> x = Fraction(2, 3) >>> x >>> x = Fraction(2, 3) >>> x Fraction(2, 3)

 The __str__ method returns a string representing the object  By default, it calls the __repr__ method  The __str__ method should focus on being human readable  We should implement a version with a natural representation:  After we have implemented the method, we can use standard Python 15COMPSCI Principles of Computer Science >>> x = Fraction(3, 4) >>> print(x) def __str__(self): return str(self.num) + '/' + str(self.den) >>> x = Fraction(3, 4) >>> print(x) 3/4

 Write the __repr__ method for the Square class created earlier.  Would it be useful to implement a __str__ method?  What would you choose to produce as output from a __str__ method? 16COMPSCI Principles of Computer Science