introduction to oop 1.0 fundamentals of java programming language 2.0 exception handling 3.0...

21
FP301 Object Oriented Programming Introduction To OOP 1. 0 Fundamentals Of Java Programming Language 2. 0 Exception Handling 3. 0 Classes, Inheritance And Polymorphism 4. 0 1 FP301 OBJECT ORIENTED PROGRAMMING

Upload: amice-sparks

Post on 23-Dec-2015

231 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

FP301 Object Oriented Programming

• Introduction To OOP1.0

• Fundamentals Of Java Programming Language

2.0

• Exception Handling3.0

• Classes, Inheritance And Polymorphism

4.0

1

FP301 OBJECT ORIENTED PROGRAMMING

Page 2: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE

2.1 Understand Objects

2

Page 3: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

Learning Outcome Subtopic 2.1

FP301 OBJECT ORIENTED PROGRAMMING

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

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

• Create classes from objects using UML Class Diagram3

• Identify components of a class4• Construct main method () using

appropriate syntax5• Apply escape sequence for special

character63

Page 4: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

4

Modelling ConceptsAbstraction

FP301 OBJECT ORIENTED PROGRAMMING

• 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.

Page 5: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

5

Cont..Encapsulation

FP301 OBJECT ORIENTED PROGRAMMING

• 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

Page 6: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

6

Cont..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.

FP301 OBJECT ORIENTED PROGRAMMING

Page 7: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

7

• The classes are grouped together based on their functionality.

• Some of the important packages of Java are: lang util io awt net applet

FP301 OBJECT ORIENTED PROGRAMMING

Cont..

Page 8: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

8

Analyze a Problem Using Object Oriented Analysis (OOA)

FP301 OBJECT ORIENTED PROGRAMMING

• 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

Page 9: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

9

Cont..

FP301 OBJECT ORIENTED PROGRAMMING

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: ……….

Page 10: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

10

Create classes from objects using UML Class Diagram

FP301 OBJECT ORIENTED PROGRAMMING

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.

Page 11: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

11

Cont..FP301 OBJECT ORIENTED PROGRAMMING

Possible Objects, attributes and behavior :-

Page 12: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

12

Example:•Class : Bicycle•Attributes/Behaviour brand, colour, wheels, no of gear •Method/Operations speed, speed, stop, changeGear

Notes:•Public +•Private -

Basikal

jenamarodabilGearwarna

kelajuan()memecut()berhenti()tukarGear()

Class

Attributes /behaviour

Method /Operations

Create classes from objects using UML Class Diagram

FP301 OBJECT ORIENTED PROGRAMMING

Page 13: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

13

COMPONENTS OF A CLASS

• The class declaration

• Attribute variable declarations and initialization(optional)

• Methods (optional)

• Comments (optional)

Structuring Classes

FP301 OBJECT ORIENTED PROGRAMMING

Page 14: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

14

Cont..

• Syntax:[modifiers] class class_identifier

• Example:public class Shirt

Class Declaration

FP301 OBJECT ORIENTED PROGRAMMING

Page 15: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

15

Cont..

public int shirtID = 0;

public String description = “-description required-”;

public char colorCode = ‘U’;

public double price = 0.0;

public int quantityInStock = 0;

Variable Declaration and Assignment

FP301 OBJECT ORIENTED PROGRAMMING

Page 16: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

16

Cont..

• 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. */

Comments:

FP301 OBJECT ORIENTED PROGRAMMING

Page 17: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

17

Cont..

• 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

Methods

FP301 OBJECT ORIENTED PROGRAMMING

Page 18: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

18

Cont..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)

FP301 OBJECT ORIENTED PROGRAMMING

Class declaration

Variable declaration and assignment

Method displayInformation()

Page 19: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

19

Construct main method () using appropriate syntax

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

FP301 OBJECT ORIENTED PROGRAMMING

Page 20: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

20

ESCAPE SEQUENCE FOR SPECIAL CHARACTERS

FP301 OBJECT ORIENTED PROGRAMMING

Sequence Meaning

\b Backspace

\t Horizontal tab

\n Line feed

\f Form feed

\r Carriage return

\” Double quote

\’ Single quote

\\ Back slash

Page 21: Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism 4.0 1

21

Cont..FP301 OBJECT ORIENTED PROGRAMMING

123456789

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

WelcometoJava!

Output :-