1 Object-oriented design requires that we interact with a problem in much the same way that we interact with our world – we treat it as a set of separate.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
© Vinny Cahill 1 Classes in Java. © Vinny Cahill 2 Writing a Java class Recall the program to calculate the area and perimeter of a rectangle of given.
Alice in Action with Java
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Road Map Introduction to object oriented programming. Classes
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Object Oriented Design Lecture 11.
UML Basics & Access Modifier
Object Oriented Software Development
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 4 Objects and Classes.
Chapter 8 More Object Concepts
Writing Classes (Chapter 4)
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
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.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
JavaScript, Fourth Edition
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
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.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
Data Structures Using C++1 Chapter 1 Software Engineering Principles and C++ Classes.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
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.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Introduction to Object-Oriented Programming Lesson 2.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Chapter 11 An introduction to object-oriented design.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Chapter 11 An introduction to object-oriented design.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Object-Oriented Programming: Classes and Objects
Chapter 3: Using Methods, Classes, and Objects
Creating Your OwnClasses
Encapsulation & Visibility Modifiers
Corresponds with Chapter 7
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Programming with Microsoft Visual Basic 2008 Fourth Edition
CIS16 Application Development and Programming using Visual Basic.net
Classes and Objects CGS3416 Spring 2019.
Creating and Using Classes
CSG2H3 Object Oriented Programming
Presentation transcript:

1 Object-oriented design requires that we interact with a problem in much the same way that we interact with our world – we treat it as a set of separate objects that perform actions and relate to each other. An object-oriented program is a set of interacting objects, each one responsible for its own activities and data. The objects interact or communicate by passing messages to one another. Introduction to Object-Oriented Design

2 What are Objects? An object can be considered a container that includes: –data in the form of a set of attributes or properties, each of which has a value at any given point in time. –a set of operations or methods that can be performed on the data. We often say objects receive messages from other objects, asking them to perform services or operations which are the object’s methods.

3 Objects are said to encapsulate (enclose together in a single indivisible unit, like in a capsule) their data and the processes that act on that data. In object-oriented design, each object can be regarded as a ‘black box’ whose internal workings are hidden from all other objects. This is known as information hiding. The principles of encapsulation and information hiding simplifies the use of objects, because the rest of the system does not need to know how they are structured or how they perform their operations.This makes the object as robust and independent as possible. Encapsulation and Information Hiding

4 Where Do Objects Come From? An object is created from a template or pattern, called a class, which defines the basic relationships, attributes, and operations available to the objects of that class. The process of creating objects from classes is called instantiation, and an object is described as an instance.

5 Constructors The process of instantiating an object from the class template is performed when a constructor (a function) is called or invoked. Constructors have the same name as their class. Constructors assign initial values to an object’s attributes. –Some constructors have parameters that initialize the attributes with specific values. –Other constructors have no parameters, in which case a new object is assigned all the default values for its attributes.

6 Accessors and Mutators An accessor is simply a descriptive name for an operation that accesses a value. By convention, accessor names start with the word get, such as getPayslip(). A mutator is simply a descriptive name for an operation that changes the value of a property. By convention, mutator names start with the word set, such as setPayRate(). Values stored in the properties of an object may only be changed by the accessors and mutators encapsulated inside that object.

7 Visibility: Public and Private Methods Some methods in an object are public and some are private. By declaring an object’s operations as public or private, we are describing their visibility to the system. Operations that perform services for other objects must be visible to those other objects, i.e., they are public methods. (Use + to indicate public methods.) Accessors and mutators are public methods. Operations used only by the object are private methods. They perform the internal actions in an object and cannot be accessed directly from outside the object. (Use – to indicate private methods.)

8 Steps in Creating an Object-Oriented Solution 1.Identify the classes, together with their attributes, responsibilities, and operations. 2.Determine the relationship between the objects of those classes. 3.Design the algorithms for the operations, using structured design. 4.Develop a test or driver program.

9 Example: SwimmingPool Class ClassAttributesResponsibilitiesMethods SwimmingPoollength width minDepth maxDepth Receive dimensions Calculate perimeter Calculate volume Calculate Box Volume Calculate Wedge Volume +setDimensions() +calcPerimeter() +calcVolume() -calcBox() -calcWedge() length width minDepth maxDepth

10 Swimming Pool Class Pseudocode Class SwimmingPool length width minDepth maxDepth setDimensions(l, w, minD, maxD) length = l width = w minDepth = minD maxDepth = maxD END calcPerimeter() RETURN (2*length + 2*width) END calcVolume() boxvol = calcBox() wedgevol = calcWedge() RETURN (boxvol + wedgevol) END calcBox() RETURN (length*width*minDepth) END calcWedge() RETURN (0.5 * length * width *(maxDepth-minDepth)) END testSwimmingPool() Create backyardPool as new SwimmingPool() backyardPool.setDimensions (20, 40, 3, 8) PRINT “Perimeter = “ backyardPool.calcPerimeter() PRINT “Volume = “ backyardPool.calcVolume() END

11 How Do You Implement Classes and Objects in Javascript? Javascript does not support a formal notion of a class like Java or C++. To implement a “class” in Javascript you write your own constructor to create methods and initialize properties in a new object. A constructor in Javascript is a function with two special features: –it is invoked through the new operator –it uses a special keyword, this, for initialization.

12 The Swimming Pool Class in Javascript - 1 // First, define the methods for the class function setDimensions(l, w, minD, maxD){ this.length = l; this.width = w; this.minDepth = minD; this.maxDepth = maxD; } function calcPerimeter(){ return (2*this.length + 2*this.width); } function calcVolume(){ var boxvol, wedgevol; boxvol = this.calcBox(); wedgevol = this.calcWedge(); return (boxvol + wedgevol); }

13 The Swimming Pool Class in Javascript - 2 function calcBox(){ return (this.length * this.width * this.minDepth); } function calcWedge(){ return (0.5 * this.length * this.width *(this.maxDepth-this.minDepth)); } // Then define a constructor method for our // SwimmingPool objects. The constructor // initializes properties and assigns methods

14 The Swimming Pool Class in Javascript - 3 function SwimmingPool(l, w, minD, maxD) { // Initialize the properties this.length = l; this.width = w; this.minDepth = minD; this.maxDepth = maxD; // Define the methods this.setDimensions = setDimensions; this.calcPerimeter = calcPerimeter; this.calcVolume = calcVolume; this.calcBox = calcBox; this.calcWedge = calcWedge; }

15 The Swimming Pool Class in Javascript - 4 // Now we can use the “class” to construct objects var backyardPool = new SwimmingPool(20, 40, 3, 8); var olympicPool = new SwimmingPool(164, 82, 6.5, 6.5); var perimeter, volume; perimeter = backyardPool.calcPerimeter(); alert(“Backyard pool perimeter = “ + perimeter); volume = olympicPool.calcVolume(); alert(“Olympic pool volume = “ + volume); alert(“Olympic pool depth = “ + olympicPool.minDepth);

16 Lab Exercise In Javascript, implement a class called Vacation. Include 4 properties: –number of days –air/ ship costs –cost per day for hotel –cost per day for meals Include 2 methods; –setExpenses(days, air, hotel, meals) –calcTotal() which calculates the total cost of the vacation Using the Vacation class, write a test program that creates 2 Vacation objects and prints out the total cost of each vacation: –var cruiseVacation = new Vacation(7, 400, 200, 0); –var bahamasVacation = new Vacation(10, 300, 200, 100); Show me your work when done.