Object-Oriented Programming and class Design

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Object Oriented Programming: Classes and Objects.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Writing Classes (Chapter 4)
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
C++ Classes How to Create and Use Them. 2 Overview Terminology File Topology Designing Classes Functions in Classes (methods)  Constructor  Accessors/Modifiers.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Object Oriented Programming A new way of thinking.
Objects and Classes Mostafa Abdallah
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
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,
Chapter 7 Objects and Classes
Topic: Classes and Objects
GC211 Data structure Lecture 3 Sara Alhajjam.
Lecture 3: Introduction to Object and Classes
Introduction to Programming G50PRO University of Nottingham Unit 9 : Classes and objects Paul Tennent
Concepts of Object Oriented Programming
Programming Logic and Design Seventh Edition
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Classes and OOP.
Chapter 3: Using Methods, Classes, and Objects
Class Variables We’ve seen how to add extra subroutines into our programs Can be accessed from within one another We can also add variables that are “global”
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Object-oriented Design in Processing
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 8 Objects and Classes Part 1
Chapter 9 Objects and Classes
Building Java Programs
CS2011 Introduction to Programming I Objects and Classes
Chapter 8 Objects and Classes
Object-oriented Design in Processing
Objects and Classes Creating Objects and Object Reference Variables
Building Java Programs
Session 2: Introduction to Object Oriented Programming
Introduction to Objects & Classes
Object-Oriented Programming and class Design
Chapter 8 Objects and Classes
Object-Oriented Programming and class Design
Object-oriented Design in Processing
Chapter 9 Objects and Classes Part 01
OO Programming Concepts
Object-Oriented Programming and class Design
Lecture 8-2: Object Behavior (Methods) and Constructors
Object-Oriented Design AND CLASS PROPERTIES
Object-oriented Design in Processing
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Chapter 7 Objects and Classes
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

Object-Oriented Programming and class Design Ps Module 6 Object-Oriented Programming and class Design 8/7/2019 CSE 1321 Module 5

Motivation Object-Oriented Programming (OOP) is based on the concept of classes, from which objects are created. This requires a new way of thinking, so hold on… 8/7/2019 CSE 1321 Module 6

Classes Using classes, we’re actually creating new data types! A class represents the concept of things in the real world, like Dogs. Classes follow a template, and have: a name variables (often called “attributes”) functions (which are now called “methods”, “behaviors” or “member functions”) 8/7/2019 CSE 1321 Module 6 3

The Smallest Class CLASS Dog BEGIN END CLASS // Note: easy points on the exam Ps 8/7/2019 CSE 1321 Module 6

The Smallest Class class Dog { } 8/7/2019 CSE 1321 Module 6

Oh no! What have we done? We’ve: created a new (complex) data type! created the “concept” of a Dog We don’t have any Dogs yet, just the concept 8/7/2019 CSE 1321 Module 6

Objects An object: is an entity in the real world that can be distinctly identified might represent a particular dog, employee, student, etc. has a unique identity, state, and behavior. 8/7/2019 CSE 1321 Module 6 7

Object Identity, State and Behavior An object has a unique identity as it represents one entity. The object state consists of a set of data fields (instance variables or properties) with their current values. The object behavior is defined by a set of methods Example: Class Student that describes individual students, such that each student has a unique ID, has his/her own state (information), and all students share same behavior (methods). 8/7/2019 CSE 1321 Module 6 8

Example: Class Bank Account   8/7/2019 CSE 1321 Module 6 9

A “real life” example Let’s make a Dog! Attributes (characteristics) rabid or not rabid (boolean) weight (a number) name (string) Behaviors growl eat

Step 1: The Skeleton CLASS Dog BEGIN // attributes will go here – name, weight, rabid // behaviors will go here – growl, eat END CLASS Ps

Ps Step 2: Add attributes CLASS Dog BEGIN CREATE rabid ← false CREATE weight ← 0.0 CREATE name ← “ ” // Behaviors go here END CLASS Ps

Step 3: The Constructor This is a special method Used to give initial values to ALL attributes Is activated when someone creates a new instance of the class Doesn’t have a return type In most languages, the name of this method MUST be the same name as the class

Step 3: Designing the Constructor Constructors will vary, depending on design Ask questions: Are all Dogs born with the same rabid state? (yes – they are all born non-rabid) Are all Dogs born with the same weight? (no – they are born with different weights) Are all Dogs born with the same name? (no – they all have different names) If ever “no”, then you need information passed in as parameters.

Ps Step 3: The Constructor CLASS Dog BEGIN CREATE rabid ← false CREATE weight ← 0.0 CREATE name ← “ ” // Constructor CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR // Behaviors go here END CLASS Ps

Step 4: Relax and Take a break We’ll get to growl and eat later We have a new data type (Dog) Let’s play around with the main algorithm

The “Driver” Usually put this in a whole new file! The Driver contains main It’s the controlling algorithm Steps: Type in the skeleton Create a couple of instances of classes Start telling them what to do

Step 1: Type in the Skeleton BEGIN MAIN END MAIN Ps

Step 2: Declare Two Dogs Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog END MAIN Memory null j1 j2

The new operator Right now, we have two “dead” dogs new Brings instances “to life” Calls the class’s constructor Opens up enough space in memory to fit an instance of that class

Step 3: Bring j1 to Life Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2

Opens Space Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2

Data Passing Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2

Data Passing Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2 rabid:false weight:14 name:"Bob”

Bring j2 to Life Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) j2 ← new Dog (7, “Ethel”) END MAIN Memory null j1 j2 rabid:false weight:14 name:”Bob”

New calls the Constructor BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) j2 ← new Dog (7, “Ethel”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2 rabid:false weight:14 name:”Bob”

Opens Space for j2 Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) j2 ← new Dog (7, “Ethel”) END MAIN Memory null j1 j2 rabid:false weight:14 name:”Bob”

Passes Data to the Constructor BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) j2 ← new Dog (7, “Ethel”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2 rabid:false weight:14 name:”Bob”

Passes Data to the Constructor BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) j2 ← new Dog (7, “Ethel”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2 rabid:false rabid:false weight:14 weight:7 name:”Bob” name:”Ethel”

Adding Eat() and Growl() CLASS Dog BEGIN CREATE rabid ← false CREATE weight ← 0.0 CREATE name ← “ ” // Constructor CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR // WE STILL NEED EAT AND GROWL END CLASS Ps

Ps CLASS Dog BEGIN CREATE rabid ← false CREATE weight ← 0.0 CREATE name ← “ ” CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR METHOD eat (parameters: amountOfFood) weight ← weight + amountOfFood PRINT (name, “ weighs”, weight, “ lbs”) END METHOD METHOD growl (parameters: none) PRINT (name, “ says GRRRRR!”) END METHOD END CLASS Ps

The “.” operator “Dot” operator used to Format: Get to an instances attributes Get to an instances methods Basically get inside the instance Format: <instance>.<attribute or method>

Using the “.” operator BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) j2 ← new Dog (7, “Ethel”) j1.eat(2) // Bob weighs 16 lbs j2.growl() // Ethel says GRRRR j1.name ← “Fluffy” // This is just cruel j1.eat(4) // Fluffy weighs 20 lbs j2.eat(-1) // Ethel weighs 6 lbs END MAIN

Another Example: Class Circle 8/7/2019 34 CSE 1321 Module 6

Class Circle Pseudocode BEGIN CREATE Radius ← 1.0 CONSTRUCTOR Circle //called default constructor BEGIN END CONSTRUCTOR CONSTRUCTOR Circle (NewRadius) //called constructor Radius ← NewRadius END CONSTRUCTOR METHOD getArea () //compute and return circle area RETURN (Radius * Radius * 3.14159) END METHOD END CLASS Ps 4/26/2018 CSE 1321 Module 6 5

Class Circle public class Circle { double radius = 1.0; Circle() //default constructor { } Circle(double newRadius) //another constructor { radius = newRadius; } public double getArea() //calculates area { return (radius*radius*3.14159); } } 4/26/2018 CSE 1321 Module 6 6

Ps Driver in Pseudocode CLASS TestCircle BEGIN CREATE circle1, circle2 AS Circle // create two variables circle1 ← NEW circle() // create circle1 object circle2 ← NEW circle(25.0) // create circle2 object PRINT ("Circle 1 area = " + circle1.getArea()) PRINT ("Circle 2 area = " + circle2.getArea()) END TestCircle Outputs: Circle 1 area = 3.14159 Circle 2 area = 1963.4937499999999 Ps 4/26/2018 CSE 1321 Module 6 5

Driver in Java class Test_Circle { public static void main (String args[]) { Circle c1 = new Circle(); System.out.println("The area of the c1 is " + c1.getArea()); Circle c2 = new Circle(25); System.out.println("The area of c2 is " + c2.getArea()); } } 4/26/2018 CSE 1321 Module 6 6

Driver in C# class Test_Circle { public static void Main (string args[]) { Circle c1 = new Circle(); Console.WriteLine("The area of the c1 is " + c1.getArea()); Circle c2 = new Circle(25); Console.WriteLine("The area of c2 is " + c2.getArea()); } } 4/26/2018 CSE 1321 Module 6 6

Summary A class: has attributes (which are just variables) has a constructor which is used to initialize attributes has other methods (like eat and growl) is a blueprint (template) for creating objects creates a new data type An object: is an instance of a class has a state (variables) that is independent from others 8/7/2019 CSE 1321 Module 6