Section 6.1 CS 106, Fall 2013. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Advertisements

Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
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:
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
1 Object Orientation James Brucker. 2 Smalltalk  Ahead of its time: consistent design and rich library.  Dynamic (like Lisp): variables have no specified.
Object-Oriented Programming
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
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.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann.
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
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
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.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2014.
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.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
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.
Classes and Objects The basics. Object-oriented programming Python is an object-oriented programming language, which means that it provides features that.
ECE122 Feb. 22, Any question on Vehicle sample code?
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
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.
Julian on JavaScript: Functions Julian M Bucknall, CTO.
Ruby Objects, Classes and Variables CS 480/680 – Comparative Languages.
Object Oriented Programing (OOP)
Lec 14 Writing an Instantiable Class III. Agenda Review of Instantiable Classes Scope of variables Using this to override scope issues Lab: Creating a.
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
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?!
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.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Defining Classes. Why is Java designed this way? Improving our class Creating our own class Using our class Implementing our class.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CS-104 Final Exam Review Victor Norman.
University of Central Florida COP 3330 Object Oriented Programming
Case Study: Undefined Variables
Unit-1 Introduction to Java
Implementing Non-Static Features
An Introduction to Object Orientated Programming
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Creating and Using Classes
Presentation transcript:

Section 6.1 CS 106, Fall 2013

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

What do you think?

How to represent a playing card? A card is a tuple with 2 parts, a suit (one of s, d, c, h) and a denom (2 – 14). We create a card by making a tuple. We access the suit via card[0] and denom via card[1]. What is good and what is bad about this implementation?

Ancient History (last week) A card is a tuple with 2 parts, a suit (one of s, d, c, h) and a denom (2 – 14). We create a card by making a tuple. We access the suit via card[0] and denom via 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 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 pointer to the object, from within the 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 Cars constructor. A: def __init__(self): # code here to initialize attributes to # default values … self._color = black

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

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

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.

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.