FP301 Object Oriented Programming

Download Report

Transcript FP301 Object Oriented Programming

1.0

• Introduction To OOP

2.0

• Fundamentals Of Java Programming Language 3.0

• Exception Handling 4.0

• Classes, Inheritance And Polymorphism 1

2

1

2

3

4

5

6

• Define modeling concepts: abstraction, encapsulation, and packages.

• Analyze a problem using object-oriented analysis (OOA).

• Create classes from objects using UML Class Diagram • Identify components of a class • Construct main method () using appropriate syntax • Apply escape sequence for special character 3

Abstraction • Abstraction is the process of exposing only the relevant details and ignoring (hiding) the irrelevant details (Don't get confused with "datahiding" of the "Encapsulation").

• Abstraction simplifies the understanding and using of any complex system.

4

Encapsulation • Through encapsulation, you can control what parts of a program can access the members of a class.

• How a member of a class can be accessed is determined by the access modifier that modifies its declaration.

• Java’s access modifiers are: –

public

private

protected

5

Packages • Is a collection of classes that can be shared by Java programs.

• Are classified into 

in-built packages – system packages

user-defined packages

- Created by the users for their requirements.

6

• The classes are grouped together based on their functionality. • Some of the important packages of Java are:       lang util io awt net applet 7

Analysis:

Precise abstraction of what the desired system must do, not how it will be done.

Object-Oriented Analysis is method which examines requirements from the perspective of the classes and objects.

OOA Process:

Use Case Modeling – shows functions Class-based Modeling – Class diagram, CRC Behavioral Model – State Diagram, Sequence Diagram 8

Example : Illustrate and explain object oriented and analysis by develop an information system to support all customer-related business in car rental company. The system should support management of customer data, reservations, vehicle rental and customer billing.

1) Identify problem domain : …………..

2) Objects: ……..

3) Attributes: …….

4) Operation: ……….

9

Draw a class diagram based on scenario below:-

A customer makes order from retail catalog. Use Order as a central class.

Associated with the order are the Customer making the purchase and the Payment.

A Payment is one of three kinds; Cash, Cheque or Credit. The order contains OrderDetails, each with its associated Item. 10

Possible Objects, attributes and behavior :-

11

Example:

Class : BicycleAttributes/Behaviour

gear

Method/Operations

changeGear

brand, colour, wheels, no of speed, speed, stop, Class Notes:

Public

 •

Private

+ Attributes / behaviour Method / Operations

12

Structuring Classes

• The class declaration • Attribute variable declarations and initialization (optional) • Methods (optional) • Comments (optional)

13

Class Declaration • Syntax: [modifiers] class class_identifier • Example: public class Shirt 14

Variable Declaration and Assignment public int shirtID = 0; public String description = “-description required-”; public char colorCode = ‘U’; public double price = 0.0; public int quantityInStock = 0; 15

Comments: • • A comment is text that is not part of your code but serves as guide when trying to figure out what the lines of code of your program are.

To comment the contents of one line, you start it with double forward slashes like this // • Example :

//Here is where you put the comments

You can include many lines of comments in your program. To do that, comment each line with the double slashes. An alternative is to start the beginning of the commented line or paragraph with /* and end the commented section with */

Example :

/* Here is a simple sentence that I want to display when the program starts. It doesn't do much. I am planning to do more stuff in the future. */ 16

Methods • Syntax: [modifiers] return_type method_identifier ([arguments]){ }

method_code_block

• Example: public void displayInformation() { System.out.println("Shirt ID: " + shirtID); System.out.println("Shirt description:" + description); System.out.println("Color Code: " + colorCode); System.out.println("Shirt price: " + price); System.out.println("Quantity in stock: " + quantityInStock); } // end of display method 17

1 public class Shirt { 2 3 public int shirtID = 0; // Default ID for the shirt 4 public String description = "-description required-"; // default 5 // The color codes are R=Red, B=Blue, G=Green, U=Unset 6 public char colorCode = ’U’; 7 public double price = 0.0; // Default price for all shirts 8 public int quantityInStock = 0; // Default quantity for all shirts 9 10 // This method displays the values for an item 11 public void displayInformation() { 12 System.out.println("Shirt ID: " + shirtID); 13 System.out.println("Shirt description:" + description); 14 System.out.println("Color Code: " + colorCode); 15 System.out.println("Shirt price: " + price); 16 System.out.println("Quantity in stock: " + quantityInStock); 17 18 } // end of display method 19 } // end of class) Class declaration Variable declaration and assignment Method displayInformation() 18

is a special method that the JVM recognizes as the starting point for every Java technology program that runs from a command line • Syntax: public static void main (String [] args)

19

Sequence

\b \t \n \f \r \” \’ \\

Meaning

Backspace Horizontal tab Line feed Form feed Carriage return Double quote Single quote Back slash 20

1 2 3 4 5 6 7 8 9 // Program Welcome.java

public class Welcome { public static void main (String args[]) { } System.out.println(“Welcome\nto\nJava!"); } Output :-

Welcome to Java!

21