CSC 108H: Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Python Objects and Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
4.1 Instance Variables, Constructors, and Methods.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Discussion 4 eecs 183 Hannah Westra.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
Generics, Exceptions and Undo Command
Java Language Basics.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
CS-104 Final Exam Review Victor Norman.
CSC 108H: Introduction to Computer Programming
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
Week 4 Object-Oriented Programming (1): Inheritance
Functions.
CSC 108H: Introduction to Computer Programming
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Object-Oriented Programming: Classes and Objects
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Object-oriented programming
To Get Started Paper sheet
Lesson 2: Building Blocks of Programming
Chapter 9 Inheritance and Polymorphism
Exception Handling Chapter 9.
MSIS 670 Object-Oriented Software Engineering
Teach A-Level Computer Science: Object-Oriented Programming in Python
Exception Handling Chapter 9 Edited by JJ.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Building Java Programs
Java – Inheritance.
Exceptions.
Java Inheritance.
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Object Oriented Programming Review
Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Java Programming Language
Classes, Objects and Methods
Exceptions 10-May-19.
CISC101 Reminders Assignment 3 due today.
CMSC 202 Exceptions.
Classes and Methods 15-Aug-19.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
Presentation transcript:

CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki

Administration Assignment 3 is up. Has two deadlines. Wed. Aug 8, Fri. Aug 10 Will talk about it at the end of class. Final is Thurs. Aug 16, 7-10 in SF 3201 Material will be covered next week. Office hours next week will be T2-4,F4-6. Exercise 4 will be optional. No time to release it that's not concurrent with the assignment. Aug 2 2012

Class Review Classes are user-made types. An instance of a class is called an object. A class has instance variables. These can have distinct values for each object of the same class. A class also has class methods. These work the same way as other type methods. Aug 2 2012

Create an instance of MyClass, and assign 10 to an instance variable num class MyClass(object): pass Aug 2 2012

Create an instance of MyClass, and assign 10 to an instance variable num class MyClass(object): pass x = MyClass() x.num = 10 Aug 2 2012

Class Review Object Oriented Programming supports Inheritance Polymorphism Encapsulation Aug 2 2012

Class Naming Conventions Classes are named using CamelCase Not pothole_case. Objects are named using pothole_case. Class methods are named using pothole_case. Class variables are named using pothole_case. Aug 2 2012

Classes variables vs. Instance variables Each class can have class variables. This is a variable that is associated with the class, rather than any specific object. To create them, you use an assignment statement as follows: ClassName.variable_name = value The variable can be evaluated with ClassName.variable_name x.variable_name if x is an instance of ClassName. Aug 2 2012

Class variables vs. Instance variables If you change the value of a class variable using ClassName.variable_name, the value changes for the ClassName objects. ClassName.variable_name = new_value If you change the value of a class variable using x.variable_name, then it becomes an instance variable for that particular object. x.variable_name = new_value The value of ClassName.variable_name does not change, nor does the value of y.variable_name for any other ClassName instance y. Aug 2 2012

Class variables Class variables are generally used to denote constants. Altering them via objects leads to complicated code. Essentially this results in a higher level of aliasing problems. Aug 2 2012

What do these statments evaluate to? class MyClass(): z = 0 y = MyClass() z = MyClass() z.z MyClass.z = 10 y.z y.z = 5 MyClass.z MyClass.z =3 Aug 2 2012

What do these statements evaluate to? class MyClass(): z = 0 y = MyClass() z = MyClass() z.z MyClass.z = 10 10 y.z 10 y.z = 5 5 MyClass.z MyClass.z =3 Aug 2 2012

Inheritance ClassA can inherit the methods and variables of ClassB by defining ClassB as follows: class ClassB(ClassA): We call ClassA the superclass and ClassB the subclass. Every instance of ClassB is also an instance of ClassA. Not every instance of ClassA is an instance of ClassB. So the set of instances of ClassA is a superset of the instances of ClassB. Aug 2 2012

Inheritance We saw that if we have the same method name in a subclass as in a superclass, and we call subclass_instance.method(), then we superclass' method is overwritten and we evaluate the subclass' method. But sometimes we want to mostly reuse the superclass method code, and only modify it a little. This comes up particularly commonly in constructors, where if your subclass is only a small change, you would not like to copy and paste the code from the constructor of the superclass. Aug 2 2012

Inheritance It would be really useful if we could call a superclass method inside of a subclass. Two ways of doing this, if x is an instance of SubClass. SuperClass.method_name(x, ...) x goes in place of self. No longer works in python 3. super(SubClass, x).method_name(...) super returns x's superclass object. self implicitly passed here. Aug 2 2012

Inheritance Inheritance allows us to define new methods, and overwrite already existing ones. But even when we overwrite existing ones, we can still access them using super. super(SubClass, x) will return the SuperClass object associated with x. Requires x to be an instance of SubClass. Recall that if x is an instance of a SubClass, it is also an instance of the SuperClass. Aug 2 2012

What do these evaluate to? class ClassA(object): def foo(): return 4 class ClassB(ClassA): pass class ClassC(ClassB): return 5 x = ClassB() y = ClassC() x.foo() super(ClassB, x).foo() y.foo() super(ClassC, y).foo() Aug 2 2012

What do these evaluate to? class ClassA(object): def foo(): return 4 class ClassB(ClassA): pass class ClassC(ClassB): return 5 x = ClassB() y = ClassC() x.foo() 4 super(ClassB, x).foo() y.foo() 5 super(ClassC, y).foo() Aug 2 2012

Break1 Aug 2 2012

Exceptions Python often generates errors. We can make our own functions, modules, types. We can also make our own errors, and generate our own errors. Errors in Python are objects. All error are subclasses of Exception. This means we can define our own errors by creating subclasses of Exception. Aug 2 2012

MyError class MyError(Exception): pass We can create instances of MyError by using MyError(). But these don't stop the code in the same way that python errors do. We can also create instances of python errors. TypeError(), NameError(), etc. Creating them in this way also doesn't stop the code. Aug 2 2012

Causing Code to crash Done using the keyword raise raise TypeError() will cause the code to crash with a TypeError. raise MyError() will cause the code to crash with a MyError. Passing the constructor a string will cause it to crash with that error massage. Aug 2 2012

Why do we want code to crash? It can be one way of enforcing sanity checks. For example if you know that some list needs 10 elements, you can check the length and crash if the length is wrong. Sometimes the program might run a very long time before an early error actually breaks the program. The longer it runs, the harder the error is to source. Mostly crashing is undesirable. Aug 2 2012

Avoiding Crashes. Avoiding crashes in python involves two keywords: try: block1 except: block2 Block1 is executed until an exception is raised. Then block2 is executed. If no exection is raised, block2 is not executed. Aug 2 2012

Not catching some exceptions Often you only want to catch some exceptions. It's common to design code to produce a specific kind of exception. It's a common way to enforce parameter requirements. But code may also have unplanned errors. It is desirable for the code to crash in this case to indicate that something is wrong. except SpecificException: This only catches instances of SpecificException or its subclasses. Aug 2 2012

Getting information from Exceptions As exceptions are objects, it is often useful to give them instance variables. In particular, the things that actually went wrong should be added to the exception. For this to be useful, we need to be able to access the exception that was raised. except MyError e: This creates a local variable e that refers to the instance of MyError that was raised. This local variable can then be used in the exception block. Aug 2 2012

Break2 Aug 2 2012

Assignment 3 Aug 2 2012