an introduction to java chapter 11 object-oriented application development: part i

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

Upload: irene-norton

Post on 31-Dec-2015

218 views

Category:

Documents


3 download

TRANSCRIPT

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

An Introduction to Java

Chapter 11Object-Oriented Application Development: Part I

Page 2: 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

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

Objectives (continued)

Code object-defining classes Code application classes that create

objects Apply the composition relationship

between classes

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

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

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

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

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

Overview of OO Application Development (continued)

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

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

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

Structured Application Development Applications can be developed as

Structured application development Object-oriented application development

The two approaches have steps in common1. Understand the problem2. Plan the application3. Write the code4. Compile and test5. Deploy the application

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

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

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

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

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

Object-Oriented Application Development (continued)

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

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

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

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

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

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

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

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

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

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

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

Apply the Concept

Recall the video rental store MovieMania, focusing on sales

Each sale must have an invoice Invoice numberDate of saleTotal amount for the sale

Two types of invoiceReceives 10% discountReceives no discount

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

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

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

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

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

Apply the Concept (continued)

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

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

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

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

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

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 filePerforms processing including object creationDisplays output

Application classes follow a procedural pattern

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

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

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

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

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

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)

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

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

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

Apply the Concept (continued)

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

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

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

Apply the Concept (continued) Accessing a private instance variable without

using a get method produces a compiler error

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

Apply the Concept (continued)

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

Other Topics in OO Application Development

Composition Static class members Final instance members InvoiceApp.java continues

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

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

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

Composition (continued)

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

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

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

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

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

Apply the Concept Add products to the Invoice User-supplied invoice number, product

ID, and quantity Each Invoice can only have one

product

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

Apply the Concept (continued)

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

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

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

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()

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

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

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

Apply the Concept (continued)

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

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

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

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

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

Analysis of MusicWorldApp11.java (continued)

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

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

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

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

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

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

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

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

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

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