Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Slides:



Advertisements
Similar presentations
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Advertisements

Section 6.1 CS 106, Fall The Big Q What do we get by being able to define a class?! Or Do we really need this?!
Python Objects and Classes
CS104: Chapter 15 CS 104 Students and me. Big Question Q: You say creating a class is how to create a new type. Why would we even want to do this?! A:
Object-Oriented Programming
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Coding Standard: General Rules 1.Always be consistent with existing code. 2.Adopt naming conventions consistent with selected framework. 3.Use the same.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
James Tam Classes and Objects You will learn how to define new types of variables.
Week 3 Recap CSE 115 – Fall Java Source Code File Made up of: Package declaration Class definition.
Classes and Objects, Part 1 Victor Norman CS104. Reading Quiz, Q1 A class definition define these two elements. A. attributes and functions B. attributes.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
IS JAVASCRIPT OBJECT-ORIENTED? Contains objects Data Methods Doesn’t have classes Does have constructors Does have prototype functions Doesn’t provide.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Inheritance. Inhertance Inheritance is used to indicate that one class will get most or all of its features from a parent class. class Dog(Pet): Make.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Object Oriented Programming Philosophy. Part 1 -- Basic Understanding & Encapsulation.
PARAMETERS Making Messages Specific Setting Up Associations Actual and Formal Parameters Return Types Accessor and Mutator Methods.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
4.1 Instance Variables, Constructors, and Methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Python Programming in Context
Files Victor Norman CS104. Reading Quiz, Q1 A file must be ___________ before a program can read data from it. A. accessed B. unlocked C. opened D. controlled.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
10-Nov-15 Java Object Oriented Programming What is it?
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
PYTHON OBJECTS & CLASSES. What is an object? The abstract idea of anything What is in an object: Attributes Characteristics Represented by internal variables.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Classes and Objects, Part 1 Victor Norman CS104. “Records” In Excel, you can create rows that represent individual things, with each column representing.
Chapter 17 Q and A Victor Norman, et al. CS104. What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Class Fundamentals BCIS 3680 Enterprise Programming.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Andrew(amwallis) Classes!
Creating Your Own Classes
CS-104 Final Exam Review Victor Norman.
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Creating Your OwnClasses
Anatomy of a class Part I
Case Study: Undefined Variables
Object Oriented Programming in Python
Private.
An Introduction to Object Orientated Programming
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Object-Oriented Programming
Classes and Objects CGS3416 Spring 2019.
Migrating to Object-Oriented Programs
Anatomy of a class Part I
Creating and Using Classes
Presentation transcript:

Section 6.1 CS 106 Victor Norman IQ Unknown

The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Ancient History (last Tuesday) A card is a tuple with 2 parts, a suit (one of “s”, “d”, “c”, “h”) and a number (2 – 14). We create a card by making a tuple. We access the suit via card[0] and numbervia card[1]. What is good and what is bad about this implementation?

What types of variables can we make? Is this good enough?

Second big Q What defines a type? Data + operations – what you can store. – what you can do to or with it.

Lecture... a class definition is like a recipe (or template). – you don't eat the recipe, right? an object is an instantiation of that class – that's what you eat. Or, a class is a new type.

Class (or Type) Marriage of data and methods. Boundary between caller and implementer – perspective on the world. self is a reference to the object, from within the class code. Each object has its own namespace, accessible via self. (In fact, isn't an object just a namespace? Could be...)

Creating class instances Q: One creates a new instance of a class by calling the class ______________. A: constructor

What does constructor code do? Q: When code instantiates a class, the __init__ method is called. This method's "primary purpose is to establish initial values for the ____________ of the newly created object." A: attributes

Code for Car constructor Q: I have a class Car that I instantiate this way: car = Car () Write the method definition for Car’s constructor. A: def __init__ ( self ): # code here to initialize attributes to # default values … self. _color = “ black ”

“Setter” Signature Q: After creating a Car instance (i.e., object), my main code wants to set the Car's color to a new color. Write the signature of the Car member function, setColor(), to set the car's color. A: def setColor(self, color): ‘’’set the color of this car to the given color’’’ self._color = color

“Getter” Signature Q: Then, my main code wants to get the color from my car instance. Write the signature for the member function getColor(). A: def getColor(self): ‘’’return the color of this Car to the caller.’’’ return self._color # my color

Syntax errors? How many syntax errors are there in this code?: class Car def _init_(make, model, year): self.myMake = make self.myModel = model myYear = year

Answer me this. Given this class definition: class Car: def __init__(self, make, model, year): self.myMake = make self.myModel = model self.myYear = year which is legal code to make a new Car instance? A.aCar = Car.__init__(self, “Honda”, “Odyssey”, 2001) B.aCar = Car.__init__("Honda", "Odyssey", 2001) C.aCar = Car("Honda", "Odyssey", 2001) D.aCar = Car(self, "Honda", "Odyssey", 2001) E.Car(aCar, "Honda", "Odyssey", 2001)

Why use classes? What are the advantages/disadvantages of defining a class/type? Advantages: Can control the state of each object – the setter methods allow only some attributes to be changed. – Sets up a sort-of “firewall” around each object. Can control what a user can do with an object. Is more “polite”: code “asks” an object to do something for it. Other advantages: subclassing, etc. Disadvantages: More syntax. More code.

Accessors and no mutators? Q: What would be the advantage to only giving the user accessor functions and no mutator functions? A: You can restrict what attributes can be changed – i.e., you can make them immutable

Example class Student: def __init__(self, name, id, grades): self._name = name self._id = id self._grades = grades def setName(self, newName): “””Change the student’s name””” self._name = newName # getName(), getId(), and getGrades() here. # No setId() here: a student’s id can never # be changed after the object has been created.

self Note that there is code “on the inside” – code inside the class definition. Code in __init__, getName(), etc. This code refers to the object as self. Then, there is code on the outside: stud1 = Student( “Dan”, , [100, 90, 80] ) This code refers to the object as stud1.

Example class Student: def __init__(self, name, id, grades): self._name = name self._id = id self._grades = grades def getName(self): return self._name def setName(self, newName): self._name = newName def getId(self): return self._id # Create a student student1 = Student( “Angelina”, 10, [] )

Designing a Class When you design a class, you decide: what are the important properties (or attributes, characteristics, or state) of an instance. what information a caller should be able to get from an object what state a caller should be able to change in an object what a caller should be able to ask an object to do to itself.

What goes in a class definition? Q: So it is my understanding that a class definition is a series of function definitions nested within the class definition… Is there anything else that goes into it? A: No. The function definitions in the class definition define the operations that the objects provide. The attributes for each object are set in the constructor method.

Naming Conventions A class name always starts with a capital letter. Each word in the class starts with a capital letter. – class MyFathersCar: – class Cs106Lab: A class attribute always starts with an underscore: _ – self._x_loc, self._y_loc, self._color

Naming Conventions local variables must start with a lowercase letter. global variables must also start with a lowercase letter, unless they are CONSTANTS, which must be all uppercase. function names start with a lowercase letter. Either use camelCase for function names and variables or use _'s between words in a name. – myXLocation, or, my_x_location – getLocation(), or, get_location()

Order of methods in a class Q: Does the order the methods are defined in a class matter? A: No. By convention the constructor method is always first, however.

Attribute names Q: Why would the coder choose _x and _y instead of x and y? A: It is a convention to name your attributes with names starting with _. The reader of the code can then know what something is when it sees the _ in front of the name.