OOP with Java, David J. Barnes Creating and Using Objects1 Why So Much for So Little? We want to use computers to solve complex tasks. Complex problems.

Slides:



Advertisements
Similar presentations
AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example.
Advertisements

Introduction Class Notes How to Program in C++ By : dettle & dettle READING MATERIAL
OOP - Object Oriented Programming Object Oriented Programming is an approach to programming that was developed to make large programs easier to manage.
cMichael Fung, CS&E, The Chinese University of HK1 Object-Oriented Programming Using Java Introduction to OOP.
Object-Oriented Analysis and Design
Introduction To System Analysis and Design
Object-Oriented PHP (1)
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
OOP & JAVA. HelloWorld.java /** * The HelloWorld class is an application that * displays "Hello World!" to the standard output. */ public class HelloWorld.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 1 Introduction to Object-Oriented Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CPT 140 Programming Constructs1 OBJECT ORIENTED TECHNOLOGY Terminology and Basic Concepts.
Introduction To System Analysis and design
Object Oriented Software Development
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Introduction to Object-oriented programming and software development Lecture 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
11 1 Object oriented DB (not in book) Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Learning objectives: What.
“Object-orientation” – what is it all about? Gill Harrison, Innovation North.
BCS 2143 Introduction to Object Oriented and Software Development.
CSCI-383 Object-Oriented Programming & Design Lecture 9.
Introduction to Objects Adapted from “TEN STEPS TO OBJECT-SPEAK” a CPT Tech Talk by Joy Starks September 17, 1999.
SE-1010 Dr. Mark L. Hornick 1 Introduction to Object-Oriented Programming (OOP) Part 1.
Introduction To System Analysis and Design
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Computer Science II 810:062 Section 01. How is CS I different from CS II? When you teach Java there are a series of decisions that have to be made…
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
ITEC 3220A Using and Designing Database Systems Instructor: Prof Z. Yang Course Website: 3220a.htm
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Chapter 1 Introduction to Object-oriented Programming.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
OOP with Java, David J. Barnes Creating and Using Objects1 Why So Much for So Little? We want to use computers to solve complex tasks. Complex problems.
Abstraction ADTs, Information Hiding and Encapsulation.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
COMPSCI 172 – Introduction to Objected- Oriented programming in Java Class hour: 12:05-12:55 pm MWF (section 1). 1:10-2:00 pm MWF (section 2) McGraw Room.
OOP with Java, David J. Barnes/Eric Jul Defining Classes1 Object State and Complexity Objects maintain a state. State is represented by a set of attributes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
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)
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
Object Oriented Programming Object and Classes Lecture 3 MBY.
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
OOP - Object Oriented Programming
Objects First with Java A Practical Introduction using BlueJ
Java Software Solutions
Object-Oriented Database Management System (ODBMS)
Exceptions The Need for Exceptions Throwing Exceptions
ITEC 3220A Using and Designing Database Systems
Java Software Solutions
Presentation transcript:

OOP with Java, David J. Barnes Creating and Using Objects1 Why So Much for So Little? We want to use computers to solve complex tasks. Complex problems require powerful programming languages. Powerful programming languages have more rules than simpler languages.

OOP with Java, David J. Barnes Creating and Using Objects2 What are Object-Oriented Languages? Support an O-O view of problem solving. Objects categorized into classes. Objects interact with each other. –Objects have behavior. –Objects maintain state. Behavior and state are intimately related. Software reuse is facilitated.

OOP with Java, Eric JulCreating and Using Objects3 What are pure Object-Oriented Languages? Support an O-O view of problem solving exclusively. Examples include Smalltalk, Emerald. Hybrid OO languages allow mixing values and objects. Examples: Java, C++, Simula 68. Hybrid languages are considered messy by the OO purists.

OOP with Java, David J. Barnes Creating and Using Objects4 Modeling the Operation of a Port Classes and objects in the scenario. –The port. –Ships moving in and out. –Cargo and associated transportation. –Passengers, vehicles, car parks. –Customs officers, pilots. –Aspects of the natural environment.

OOP with Java, David J. Barnes Creating and Using Objects5 Interactions in the Port Transfer of cargo between ship and dock. Passengers embarking and disembarking. A pilot joining or leaving a ship. A ship requesting permission to enter. The weather affecting ship movements.

OOP with Java, David J. Barnes Creating and Using Objects6 Classes and Objects A class definition provides a description of a typical object within that class. –CargoShip, PassengerFerry. An individual object is an instance of a class. Definition of behavior and attributes.

OOP with Java, Eric JulAttributes7 Attributes store the state of an object. The state stored in an attribute is often a reference to another object. Simple state is, in hybrid languages, represented as values (non-object values).

OOP with Java, David J. Barnes Creating and Using Objects8 Class and Instances Liner might capture the common characteristics of ocean liners. –RMS Titanic and RMS Olympic might be two instances of that class. Class definition is like a blueprint or template. –Color, size, pattern might vary, but instances of the same class come from the same mold.

OOP with Java, David J. Barnes Creating and Using Objects9 Creating an Object // An illustration of object creation. // (The ship class is assumed to preexist.) class ShipMain1 { public static void main(String[] args){ // Define a method variable to refer to // a Ship object. Ship argo; // Construct a new Ship object. argo = new Ship(); }

OOP with Java, David J. Barnes Creating and Using Objects10 Object Interactions Communication between people: –Information: “I am going to a party.” –Question: “What is the time?” –Order/Request: “Please buy me some gum.” Objects communicate in similar ways. –Passing messages to each other. –Actor/Agent, Client/Server relationships.

OOP with Java, David J. Barnes Creating and Using Objects11 Sending a Message to a Ship // An illustration of message sending. class ShipMain2 { public static void main(String[] args){ // Define a method variable. // Make it refer to a new Ship object. Ship argo = new Ship(); // Ask the ship to report its position, // course and speed. argo.report(); }

OOP with Java, David J. Barnes Creating and Using Objects12 Moving a Ship // An illustration of ship movement. class ShipMain3 { public static void main(String[] args){ Ship argo = new Ship(); // Ask the ship to move. argo.move(); }

OOP with Java, David J. Barnes Creating and Using Objects13 Passing Arguments with Messages The report and move messages convey implicit information. –The Ship uses its own internal state to respond. Some messages need to be accompanied by further explicit information. –“Change course to...” –“Reduce speed to...”

OOP with Java, David J. Barnes Creating and Using Objects14 Arguments Explicit information can be passed to an object in a message by adding arguments to the message. Arguments are objects containing information. In hybrid languages, such as Java, arguments can also be values.

OOP with Java, David J. Barnes Creating and Using Objects15 Passing Arguments class ShipMain4 { public static void main(String[] args){ Ship argo = new Ship(); argo.report(); argo.move(); // Ask it to change course, move and // report its new settings. argo.setCourse(90); argo.move(); argo.report(); }

OOP with Java, David J. Barnes Creating and Using Objects16 Receiving Replies Human communication often requires a reply. –“What is the time?” –“Are there any free berths in the port?” –“What is the square-root of 2?” Similarly, we can request information from a Ship. We often store the answer somewhere.

OOP with Java, David J. Barnes Creating and Using Objects17 Simple Example of Reply Ship’s speed is returned as a value – a double which here merely is a number, e.g., 17.3 The value, we may assume, is in knots. The number is stored and used to request the ship to go one knot faster.

OOP with Java, David J. Barnes Creating and Using Objects18 Requesting a Ship’s Speed class ShipMain5 { public static void main(String[] args){ Ship argo = new Ship(); argo.report(); // Define a variable to hold the speed. double currentSpeed; // Ask the ship what its current speed is. currentSpeed = argo.getSpeed(); // Increase the ship's speed. argo.setSpeed(currentSpeed+1); argo.move(); argo.report(); }

OOP with Java, David J. Barnes Creating and Using Objects19 Variables and Objects Variables and objects are different. A variable may refer to a single object. Different variables may refer to the same object - aliases. A variable may be switched from one object to another. A variable may be uninitialized.

OOP with Java, Eric JulCreating and Using Objects20 Variables Contain References A variable contains either a value or a reference to an object. A reference can be thought of as a pointer to an object. Illustrated graphically.

OOP with Java, David J. Barnes/Eric Jul Creating and Using Objects21 The Main Method's Class We (Barnes and Jul) choose never to create an object of the main method’s class. This approach is not followed by many other authorities. The main method is a static method. –Static methods have special properties that we shall explore in due course. A messy hack—just live with it for now.

OOP with Java, David J. Barnes Creating and Using Objects22 Review O-O problem solving involves identifying classes, objects and object interactions. Objects maintain state and exhibit class- defined behavior. Instances of the same class behave in similar ways. Message passing illustrates object interaction.

OOP with Java, David J. Barnes Creating and Using Objects23 Review (cont.) Messages may contain additional information: in the form of arguments. A result may be returned as the result of a message. Variables and objects are distinct. –Variables switch between different objects. –An object may be simultaneously referred to by different variables.

OOP with Java, David J. Barnes & Jul Creating and Using Objects24 Review (cont.) Variables contain references. In hybrid languages, variables may alternatively contain values. References can be thought of as pointers.