an introduction to java programming and object-oriented...
TRANSCRIPT
An Introduction to Java Programming and Object-Oriented Application Development 1
An Introduction to Java Programming and
Object-Oriented Application Development
Chapter 11Object-Oriented Application Development:
Part I
An Introduction to Java Programming and Object-Oriented Application Development
2
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
An Introduction to Java Programming and Object-Oriented Application Development 2
An Introduction to Java Programming and Object-Oriented Application Development
3
Objectives (continued)
• Code object-defining classes• Code application classes that create objects• Apply the composition relationship between
classes
An Introduction to Java Programming and Object-Oriented Application Development
4
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
An Introduction to Java Programming and Object-Oriented Application Development 3
An Introduction to Java Programming and Object-Oriented Application Development
5
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
An Introduction to Java Programming and Object-Oriented Application Development
6
Overview of OO Application Development (continued)
An Introduction to Java Programming and Object-Oriented Application Development 4
An Introduction to Java Programming and Object-Oriented Application Development
7
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
An Introduction to Java Programming and Object-Oriented Application Development
8
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
An Introduction to Java Programming and Object-Oriented Application Development 5
An Introduction to Java Programming and Object-Oriented Application Development
9
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
An Introduction to Java Programming and Object-Oriented Application Development
10
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
An Introduction to Java Programming and Object-Oriented Application Development 6
An Introduction to Java Programming and Object-Oriented Application Development
11
Object-Oriented Application Development (continued)
An Introduction to Java Programming and Object-Oriented Application Development
12
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
An Introduction to Java Programming and Object-Oriented Application Development 7
An Introduction to Java Programming and Object-Oriented Application Development
13
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
An Introduction to Java Programming and Object-Oriented Application Development
14
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
An Introduction to Java Programming and Object-Oriented Application Development 8
An Introduction to Java Programming and Object-Oriented Application Development
15
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
An Introduction to Java Programming and Object-Oriented Application Development
16
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
An Introduction to Java Programming and Object-Oriented Application Development 9
An Introduction to Java Programming and Object-Oriented Application Development
17
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
An Introduction to Java Programming and Object-Oriented Application Development
18
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
An Introduction to Java Programming and Object-Oriented Application Development 10
An Introduction to Java Programming and Object-Oriented Application Development
19
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
An Introduction to Java Programming and Object-Oriented Application Development
20
Apply the Concept (continued)
An Introduction to Java Programming and Object-Oriented Application Development 11
An Introduction to Java Programming and Object-Oriented Application Development
21
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
An Introduction to Java Programming and Object-Oriented Application Development
22
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
An Introduction to Java Programming and Object-Oriented Application Development 12
An Introduction to Java Programming and Object-Oriented Application Development
23
Application Classes
• Object-defining classes do not contain a mainmethod 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
An Introduction to Java Programming and Object-Oriented Application Development
24
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
An Introduction to Java Programming and Object-Oriented Application Development 13
An Introduction to Java Programming and Object-Oriented Application Development
25
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
An Introduction to Java Programming and Object-Oriented Application Development
26
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)
An Introduction to Java Programming and Object-Oriented Application Development 14
An Introduction to Java Programming and Object-Oriented Application Development
27
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
An Introduction to Java Programming and Object-Oriented Application Development
28
Apply the Concept (continued)
An Introduction to Java Programming and Object-Oriented Application Development 15
An Introduction to Java Programming and Object-Oriented Application Development
29
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
An Introduction to Java Programming and Object-Oriented Application Development
30
Apply the Concept (continued)• Accessing a private instance variable without
using a get method produces a compiler error
An Introduction to Java Programming and Object-Oriented Application Development 16
An Introduction to Java Programming and Object-Oriented Application Development
31
Apply the Concept (continued)
An Introduction to Java Programming and Object-Oriented Application Development
32
Other Topics in OO Application Development
• Composition• Static class members• Final instance members• InvoiceApp.java continues
An Introduction to Java Programming and Object-Oriented Application Development 17
An Introduction to Java Programming and Object-Oriented Application Development
33
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
An Introduction to Java Programming and Object-Oriented Application Development
34
Composition (continued)
An Introduction to Java Programming and Object-Oriented Application Development 18
An Introduction to Java Programming and Object-Oriented Application Development
35
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
An Introduction to Java Programming and Object-Oriented Application Development
36
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
An Introduction to Java Programming and Object-Oriented Application Development 19
An Introduction to Java Programming and Object-Oriented Application Development
37
Apply the Concept• Add products to the Invoice• User-supplied invoice number, product ID,
and quantity• Each Invoice can only have one product
An Introduction to Java Programming and Object-Oriented Application Development
38
Apply the Concept (continued)
An Introduction to Java Programming and Object-Oriented Application Development 20
An Introduction to Java Programming and Object-Oriented Application Development
39
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 Productconstructor
• Accessor methods allow access to variables
An Introduction to Java Programming and Object-Oriented Application Development
40
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
methodanInvoice.getAProduct().getProductID()
An Introduction to Java Programming and Object-Oriented Application Development 21
An Introduction to Java Programming and Object-Oriented Application Development
41
Apply the Concept (continued)
• Calling getProductID on an InvoiceWithProduct object gives a compiler error
An Introduction to Java Programming and Object-Oriented Application Development
42
Apply the Concept (continued)
An Introduction to Java Programming and Object-Oriented Application Development 22
An Introduction to Java Programming and Object-Oriented Application Development
43
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
An Introduction to Java Programming and Object-Oriented Application Development
44
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
An Introduction to Java Programming and Object-Oriented Application Development 23
An Introduction to Java Programming and Object-Oriented Application Development
45
Analysis of MusicWorldApp11.java (continued)
An Introduction to Java Programming and Object-Oriented Application Development
46
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
An Introduction to Java Programming and Object-Oriented Application Development 24
An Introduction to Java Programming and Object-Oriented Application Development
47
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 CDOrderobject is created using its methods
An Introduction to Java Programming and Object-Oriented Application Development
48
The MusicWorld Application
• The three object-defining classes compile but will not run
• The application requires a class with a mainmethod
• 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
An Introduction to Java Programming and Object-Oriented Application Development 25
An Introduction to Java Programming and Object-Oriented Application Development
49
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
An Introduction to Java Programming and Object-Oriented Application Development
50
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