CIT 590 Intro to Programming Style Classes. Remember to finish up findAllCISCourses.py.

Slides:



Advertisements
Similar presentations
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Advertisements

Python Objects and Classes
CIT 590 Intro to Programming Classes. Schedule change The upcoming HW (HW6) is your last major Python HW. It will involve object oriented programming.
Written by: Dr. JJ Shepherd
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/18/2012i206 Lab 1 - Exercise1.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Programming Languages and Paradigms Object-Oriented Programming.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CMP-MX21: Lecture 6 Objects & Methods 1 Steve Hordley.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Java Language and SW Dev’t
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
CIT 590 Intro to Programming First lecture on Java.
Defining New Types Lecture 21 Hartmut Kaiser
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 5 Function Interfaces 4/18/09 Python Mini-Course: Day 2 - Lesson 5 1.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Abstraction ADTs, Information Hiding and Encapsulation.
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Object Oriented Programing (OOP)
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 12 Inheritance and Class Design 1.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Written by: Dr. JJ Shepherd
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Chapter 3 Implementing Classes
CSCE 240 – Intro to Software Engineering Lecture 3.
Comp1004: Building Better Objects II Encapsulation and Constructors.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 231: Introduction to Data Structures Python and Objects – Day 3 Dr. Curry Guinn.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
CSSE 120—Rose Hulman Institute of Technology
Functions.
Object Oriented Programming
Writing Methods AP Computer Science A.
Encapsulation and Constructors
Review of Previous Lesson
Functions, Procedures, and Abstraction
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CIT 590 Intro to Programming Style Classes

Remember to finish up findAllCISCourses.py

Elements of style Do not make an extremely long function. Break it up in some manner Generally in this course the long function will be your main method Remember coke-machine.py Is there repeated code – just make a function out of it Do not write an incredibly long single line of code In Python you can break a line up by using parantheses for instance docStrings! – write a docString for every function. Triply quoted string telling the user what the function does

Elements of style Break the program into small portions and write the smallest portion first Your functions need to be reusable Take pride in your functions. Make your worst enemy want to use them General purpose Try and make them robust to bad inputs Write the smallest function first = write unit tests for the smallest function first The main function should be the last thing you complete

Elements of testing Write more than 1 test case for each function Check the test files that we used for your homeworks Your tests and meant to find bugs so you do not want to be gentle! Adding tests is easy. Do not be hesitant about adding more. See movieTests.py in the repo All unit tests being used for evaluation can be found in the files section of canvas (we will organize it better)

Design Buzzwords Don’t Repeat Yourself As a software developer your goal always always is to avoid copy pasting code. The opposite of DRY is …. WET – Write Everything Twice Keep It Simple Stupid (KISS) You Ain’t Gonna Need It (YAGNI) Reduce the number of components if you feel you are implementing functionality that you were not asked for rule gets applied a lot in the industry

Classes Encapsulate functions that can be used for a common purpose bankAccount.py Classes implicitly apply the principles of abstraction and information hiding The user of the bank account class does not need to know how balance is being maintained Just get a handle to the class and use the deposit and withdrawal functions

Class definition class BankAccount(object): def __init__ (self, initBalance): self.balance = initBalance def deposit(self, amount): self.balance += amount Parent class methods

Creating an instance of the class The class definition by itself does not create an instance of the class myFirstAccount = BankAccount() For those of you with some java background, there is no ‘new’ An instance of a class is called an object

Methods A class method looks very very much like a regular function The first argument HAS to be ‘self’ Calling methods within other methods We have to always refer to which object we are calling the method on def transfer (self, amount, toAccount): self.withdraw(amount) toAccount.deposit(amount)

Constructors The __init__ is implicitly called when you say B = BankAccount() First time we are seeing the __ __ convention Python uses this when we have functions that are used behind the scenes

Objects are references => be careful Very similar to the situation of two lists The copy module is your best friend import copy bankAccount2 = copy.copy(bankAccount1) Passing an object as a function parameter Again similar to lists, the object will be modified Sometimes you want the modification Sometimes you want to copy

How to print out an object __str__ When you call print this internal function will be called. Python will always provide some kind of default However the default is ‘lame’ Try commenting out the __str__ in the bankAccount object

Accessing the data fields Python classes expose their data fields. For the bankaccount example you could always manipulate the balance data field directly BUT this is considered bad programming practice It is considered much better form to provide what is called a getter and a setter In this bank account example, a getter for balance is fully justified since that is a service you would want However a direct setting of bank balance = xyz seems inappropriate. We have deposit and withdraw More on this when we get to Java

Inheritance You’ve already seen it without it being explicitly called as such Idea = Using methods defined on the parent class Software reuse By inheriting from a class you only need to define the specialized methods Often you might be inheriting from a different developer’s class Checking account example class CheckingAccount(bankAccount.BankAccount) Checking account inherits from bankaccount Checking account is a special type of bankaccount When should I use inheritance Apply the ‘is-a’ method A real number is a complex number A checking account is a bank account

Question on classes Using one class inside another class A bank contains a lot of bankAccounts. bank = Bank() What do we get when we do print Bank() a) The account is owned by CEO balance is 100 b) The account is owned by CEO balance is 0 c) The account is owned by Arvind balance is 100 d) The account is owned by Arvind balance is 0 e) I am confused

Inbuilt functions to help explore inheritance Use isinstance Issubclass Using the types module (import types) You can check for an int by doing Isinstance(3, types.IntType)

The is-a versus has-a concept A common method used to identify inheritance is to ask the question ‘Is class B a special case of class A?’ If the answer is yes, B extends A. If the answer is no, then you might want one class contained inside the other – composition A Bank contains BankAccounts - composition A CheckingAccount is a BankAccount - inheritance