Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I."— Presentation transcript:

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

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

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

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

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

6 Overview of OO Application Development (continued)

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

8 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

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

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

11 Object-Oriented Application Development (continued)

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

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

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

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

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

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

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

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

20 Apply the Concept (continued)

21 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

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

23 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

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

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

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)

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

28 Apply the Concept (continued)

29 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

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

31 Apply the Concept (continued)

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

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

34 Composition (continued)

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

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

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

38 Apply the Concept (continued)

39 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

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 method anInvoice.getAProduct().getProductID()

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

42 Apply the Concept (continued)

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

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

45 Analysis of MusicWorldApp11.java (continued)

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

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 CDOrder object is created using its methods

48 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

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

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


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

Similar presentations


Ads by Google