P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.

Slides:



Advertisements
Similar presentations
C++ Classes & Data Abstraction
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Objects and Classes Objects and Classes objects defining classes method declaration object references and aliases instance variables encapsulation and.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 4 Objects and Classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
CSC 212 Object-Oriented Programming and Java Part 1.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
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.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSC 205 Java Programming II Defining & Implementing Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
1 CS Programming Languages Class 22 November 14, 2000.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Written by: Dr. JJ Shepherd
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Classes (Part 1) Lecture 3
Objects as a programming concept
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Classes and Objects 2nd Lecture
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Classes In C#.
Lecture 9 Concepts of Programming Languages
Defining Classes and Methods
Chapter 9 Objects and Classes
Defining Classes and Methods
Data Structures and Algorithms for Information Processing
Information Hiding and Encapsulation Section 4.2
CSG2H3 Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Presentation transcript:

p Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and operations to manipulate those types. p This presentation introduces OOP. Object Oriented Programming Data Structures and Other Objects Using Java

Abstract Data Type (ADT) p A new type p Contains some form of data representation p Provides operations for manipulating the data

Object-Oriented Programming p A methodology for how programs are written p Extension of ADT concept to include inheritance capability p An abstraction of the real world real world animal mammal canine dog Lakota

Key Principles of ADTs and OOP p Encapsulation p A way of organizing all the components of an object into one entity p E.g. class libraries p Information Hiding p A way of separating the descriptive specification for an object from its implementation p E.g. primitive data types such as double

Object p Defines a new type of data p Includes data members (usually hidden) called instance variables p Includes member methods that define the permissible operations on the data members p Includes constructors – special methods that provide initializations for instance variables

Example - Throttle Off Throttle Top 6 Position 4 getFlowisOnshift shutOff Hidden

Example - Throttle Off Throttle Top 6 Position 4 shift shutOff isOngetFlow

Java Class p In Java (and some other programming languages), objects are built using the mechanism called a class p A class encapsulates all of the components of the object, including instance variables, constructor(s), and method definitions

Defining a New Class in Java public class public class { <methods>}

Throttle Class public class Throttle { private int top; private int position; <methods>} Name begins with capital letter Instance variables

Constructor Methods p Responsible for initializing instance variables p Can have more than one, as long as they have different signatures p Can have no constructor – default constructor p A method with no return type

Throttle Constructor public Throttle(int size) Parameters: size, the number of on positions Precondition Size > 0 Postcondition Throttle initialized with specified # of on positions; initially off Throws: IllegalArgumentException

public Throttle(int size) { if (size <= 0) throw new IllegalArgumentException (“Size <= 0: “ + size); top = size; position = 0; }

Throttle Constructor public Throttle( ) Parameters: none Precondition: none Postcondition Throttle initialized with 5 on positions; initially off Throws: IllegalArgumentException

public Throttle( ) { top = 5; position = 0; }

Methods p Accessor – gives information about an object without altering it p Modifier – changes the “status” of an object (generally by changing instance variables)

Throttle Accessor getFlow( ) public double getFlow( ) Get the current flow of this Throttle Returns: The current flow rate

public double getFlow( ) { return (double) position / (double) top; }

Reasons for Accessor Methods p Programmer using Throttle doesn’t need to worry how its implemented p Could later change Throttle implementation without affecting existing user programs p Method can be thoroughly tested p Information hiding keeps programmers from using instance variables in unintended ways (e.g. setting position to negative)

Throttle Accessor isOn( ) public boolean isOn( ) Check whether Throttle is on Returns If Throttle flow is above zero, returns true; otherwise returns false.

public boolean isOn( ) { return (position > 0); } Question: why doesn’t the programmer just check the value of position directly?

Throttle Modifier shutOff( ) public void shutOff( ) Turn off this Throttle Precondition: none Postcondition: the Throttle’s flow is shut off

public void shutOff( ) { position = 0; }

Throttle Modifier shift( ) public void shift( int amount) Move Throttle position up or down Parameters: amount – the amount to move the position up or down Postconditions: Throttle’s position has been moved by amount. If result is more than top position, then position set to top. If result is less than zero position, then position set to zero.

public void shift(int amount) { if (amount > top - position) position = top; else if (position + amount < 0) position = 0; else position += amount; } Why isn’t this: (position + amount > top)?

Methods Activating (Calling) Methods public boolean isOn() { return (getFlow() > 0); } Could just reference position instance variable

Creating and Using Objects Prior examples: int[] values = new int[20]; Scanner input = new Scanner(System.in);

Creating and Using Objects Throttle control = new Throttle(100); Declares new variable of type Throttle } } Creates new Throttle object Initializes Throttle size to 100

Creating and Using Objects Throttle control = new Throttle(); Initializes Throttle size to default value

Creating and Using Objects Throttle control = new Throttle();. control.shift(3); Activates shift( ) method for object control Object name Period operator Method name Method parameters

final int SIZE = 8; final int SPOT = 3; Throttle small = new Throttle(SIZE); small.shift(SPOT); System.out.print(“Small throttle position = “); System.out.println(SPOT + “ out of “ + SIZE + “.”); System.out.println(“The flow is now: “ + small.getFlow());

Throttle tiny = new Throttle(4); Throttle huge = new Throttle(10000);tinyhuge top position 4 0 top position ThrottleThrottle Each object has its own copy of the instance variables

Null References Throttle control;. control = new Throttle(100); } What is the state of control during this time?

Null References Throttle control = null;. control = new Throttle(100);

Null References Throttle control;. control = new Throttle(100);. control = null; // no longer needed

Assignments with Reference Variables Throttle t1; Throttle t2; t1 = new Throttle(100); t1.shift(25); t2 = t1; top position Throttle t1t2 ?? t2.shift(-5); 20

Assignments with Reference Variables Throttle t1; Throttle t2; t1 = new Throttle(100); t1.shift(25); t2 = new Throttle(100); t2.shift(25); t1t2 top position top position ThrottleThrottle

(t1 == t2) ? Throttle t1; Throttle t2; t1 = new Throttle(100); t1.shift(25); t2 = t1; top position Throttle t1t2 TRUE

(t1 == t2) ? Throttle t1; Throttle t2; t1 = new Throttle(100); t1.shift(25); t2 = new Throttle(100); t2.shift(25); t1t2 top position top position ThrottleThrottle FALSE