An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Basic OOP Concepts and Terms
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
ASP.NET Programming with C# and SQL Server First Edition
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Object Oriented Software Development
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
Programming Languages and Paradigms Object-Oriented Programming.
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.
Writing Classes (Chapter 4)
CIT241 Prerequisite Knowledge ◦ Variables ◦ Operators ◦ C++ Syntax ◦ Program Structure ◦ Classes  Basic Structure of a class  Concept of Data Hiding.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
CSC 212 Object-Oriented Programming and Java Part 1.
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.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Basic OOP Concepts and Terms. In this class, we will cover: Objects and examples of different object types Classes and how they relate to objects Object.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Object Oriented Programming and Data Abstraction Earl Huff Rowan University.
Class Fundamentals BCIS 3680 Enterprise Programming.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 11 An introduction to object-oriented design.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Programming Logic and Design Seventh Edition
Topics Procedural and Object-Oriented Programming Classes
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Creating and Using Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Defining Your Own Classes
Basic OOP Concepts and Terms
Object Oriented Programming in java
2.1 Introduction to Object-Oriented Programming
Chapter 7 Objects and Classes
Presentation transcript:

An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I

Objectives In this chapter you will: Compare real-world classes and objects to software classes and objects Survey structured and object-oriented application development Create class diagrams

Objectives (continued) Code object-defining classes Code application classes that create objects Apply the composition relationship between classes

Object-Oriented Application Development: Part I All Java statements must be included in a class Classes studied so far were designed to execute procedures, thus used procedural programming Object-oriented applications contain programmer-defined classes Programmer-defined classes represent real- world objects to solve a real-world problem

Overview of OO Application Development A real-world system is often managed by an information system  Examples: Space shuttle program, university registration system, company payroll system A real-world object can belong to a real-world class A software object represents a real-world object A software class represents a real-world class A software object belongs to a software class

Overview of OO Application Development (continued)

Attributes and Behaviors of Objects Object attributes are characteristics that define an object  Example: A student in a university system has attributes student ID, gender, birth date, address Objects in a system have attributes and are related to each other Object behaviors are the actions or operations that define what an object does Only some attributes and behaviors are relevant to a system

Structured Application Development Applications can be developed as  Structured application development  Object-oriented application development The two approaches have steps in common 1. Understand the problem 2. Plan the application 3. Write the code 4. Compile and test 5. Deploy the application

Structured Application Development (continued) Structured application development focuses on how data flow from one application to another Focuses on how system entities are related by system events Treats data and events separately Tend to be procedural in nature

Object-Oriented Application Development Object-oriented application development focuses on objects in the system and how they interact Objects are related by their attributes Variables and methods organized in separate classes defining different types of objects A separate application handles input and output Object-oriented applications are more modular

Object-Oriented Application Development (continued)

Apply the Concept Problem domain is a video rental store MovieMania, which rents and sells DVDs Two types of objects: DVDs and customers DVD copy objects  Attributes: MMID, ISAN, due date  Behaviors: Add and remove from inventory DVD work objects  Attributes: ISAN, title, rating, running time  Behaviors: Check out and return DVD

Objects and Classes Objects and classes are the two most important concepts in OO application development Objects Classes Visibility of instance variables and methods MovieMania continued

Objects A software object represents a real-world object in computer memory An instance variable describes an attribute of an object An instance method describes a behavior of an object An object is identified by reference and created using the Java keyword new A constructor is a special method that carries out the details of creating an object

Classes Objects are implemented in classes An application class manages specific applications  Contains a main method  Procedural in nature An object-defining class is a template for creating objects An object-defining class has data members and method members Objects are instantiated from classes

The Visibility of Instance Variables and Methods Visibility of an instance variable is determined by its modifier The keyword private indicates a variable is only visible within its defining class The keyword public indicates a variable is visible in any class The keyword protected indicates a variable is visible in subclasses or in classes in the same package

Apply the Concept Recall the video rental store MovieMania, focusing on sales Each sale must have an invoice  Invoice number  Date of sale  Total amount for the sale Two types of invoice  Receives 10% discount  Receives no discount

Apply the Concept (continued) Unified Modeling Language (UML) is standard notation for graphically planning OO applications First create a class diagram, which shows the structure of classes and their relationships The class name is in the top section Data members are in the middle section  Instance variable identifiers use nouns Instance variables should be private, indicated by a minus sign at the beginning of the line

Apply the Concept (continued) The data types of instance variables follow a colon after the name Method members are shown in the bottom section of the class diagram followed by () Method members include mutator methods and accessor methods  Mutator methods assign values to a variable  Accessor methods retrieve values from a variable Return type of the method follows the method name, parentheses, and colon

Apply the Concept (continued)

The class Invoice is public so other classes can access its public members Instance variables are private and cannot be accessed directly by outside classes Instance variables are accessed indirectly through public get and set methods The constructor uses the arguments to assign values to instance variables There is no main method, thus it is not executable

A Complete OO Application A complete OO application allows users to input data, create objects, and output information Application classes Encapsulation Information hiding Object interface

Application Classes Object-defining classes do not contain a main method and cannot be executed A driver class contains a main method An application class does three things  Obtains input from the user or file  Performs processing including object creation  Displays output Application classes follow a procedural pattern

Encapsulation Encapsulation is the process of creating an object-defining class that combines attributes and behaviors A typical OO application contains many different types of objects Object-defining classes must be created for each one A procedural application class handles input, processing, and output

Information Hiding Encapsulation enables information hiding Information hiding refers to the process of concealing information about an object’s implementation Programmers only need know what data a constructor needs and what information the class provides Information hiding allows a programmer to change an implementation without a complete rewrite of the application

Object Interface An object interface is the set of methods in a class used to manipulate its instance variables Each method has a signature  Name, visibility, return type, parameters Examples: public Invoice (String number, double total, int type) public void setInvoiceTotal (double invTotal, int invoiceType)

Apply the Concept Invoice.java will be enhanced to include an application class Invoice objects are the only objects in this system A loop allows the user to create multiple invoices Within the loop  Prompts the user for input  Creates an Invoice object  Produces output

Apply the Concept (continued)

A while loop allows the user to create invoices until an X is entered Line 37 creates a new Invoice The Invoice constructor sets instance variables and calculates the total Lines 40 – 50 display the Invoice information The class InvoiceApp has access to get and set methods because they are public

Apply the Concept (continued) Accessing a private instance variable without using a get method produces a compiler error

Apply the Concept (continued)

Other Topics in OO Application Development Composition Static class members Final instance members InvoiceApp.java continues

Composition Some of the data members are instance variables that refer to other objects Composition is a characteristic of an object-defining class that indicates the class is composed of other objects

Composition (continued)

Static Class Members Data members of a class can include class variables (or static fields) Class variables hold data to be shared by all objects of a particular class All instances of a class share access to a single class variable, and do not have their own copies Static methods are classwide methods that access static fields

Final Instance Variables The values of instance variables can change during program execution A final instance variable is constant and cannot be changed during program execution Final variables are all capitals by programming convention Use the keyword final A final variable must be initialized or the program will not compile

Apply the Concept Add products to the Invoice User-supplied invoice number, product ID, and quantity Each Invoice can only have one product

Apply the Concept (continued)

The instance variable aProduct is a Product object, an example of composition The variable TAX_RATE is a final instance variable The variable invoiceCount is static and each InvoiceWithProduct increments it The InvoiceWithProduct constructor initializes variables and calls the Product constructor Accessor methods allow access to variables

Apply the Concept (continued) The Product class constructor assigns the value of prodID to the field productID Calls two set methods which set product price and description InvoiceWithProductApp.java is the driver class The class must get the product ID from the Product class  Get a Product object, and call its accessor method anInvoice.getAProduct().getProductID()

Apply the Concept (continued) Calling getProductID on an InvoiceWithProduct object gives a compiler error

Apply the Concept (continued)

Case Study: MusicWorld MusicWorld allows a user to enter information about a sales transaction, and compute total including tax and quantity discount The application has grown to more than 500 lines of code, strictly procedural Contains no programmer-defined classes Improvement: Convert the application to OO

Analysis of MusicWorldApp11.java Real-world CD objects have ID, title, price attributes, and no behaviors A CD order has one or more line items A line item has an identification number and a single CD object, quantity, discount, and subtotal A final CD order has an array of line items, a tax amount, a final total, and calculates the subtotal, tax, and total

Analysis of MusicWorldApp11.java (continued)

A CD order has line items Each line item has a CD These are examples of composition, or “has-a” relationships

Program Code for MusicWorldApp11.java Three classes: CD, LineItem, and CDOrder The static variable TAX_RATE is common to all CDOrder objects The set methods in CDOrder assign values to instance variables The constructor for CDOrder calculates the subtotal, tax, and final total when the CDOrder object is created using its methods

The MusicWorld Application The three object-defining classes compile but will not run The application requires a class with a main method The main method will receive a list of CDs from the user It will create the required objects, and output the order total The MusicWorld driver class will be created in Chapter12

Summary Two types of application development: Structural and object-oriented Structured development focuses on the flow of data from one procedure to another In object-oriented applications, software objects model real-world objects and software classes model real-world classes Object-defining classes are blueprints for objects Application classes contain a main method and are responsible for program execution

Summary (continued) Encapsulation allows OO applications to hide implementation details of objects An application interacts with objects via their interface Static class members are common to all instances of a particular class A class member can be private, protected, or public A final instance variable is a constant