500 likes | 579 Views
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.
E N D
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
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 • Understand the problem • Plan the application • Write the code • Compile and test • 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
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
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
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
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