Vous êtes sur la page 1sur 71

The Nelson Mandela African Institute of Science and Technology ICSE 6202: Information Systems Analysis, Design and

Modeling March April 2012 Lecture 1: Basics of Object Oriented


Loserian S. Laizer Email: loserian.laizer@nm-aist.ac.tz, lslaizer@gmail.com 0713 477210/0787 477210 Office: Wing D, Room 105

NM-AIST - Nelson Mandela African Institute of Science and Technology - 2012

Objectives
Upon completion of this section, you are expected to : 1. Explain the basic concepts of Object Oriented technology. 2. Understand the advantages of Object Oriented system development.

Object Oriented Basics

Contents
Chapter 1. What Object Orientation is Chapter 2. Basic Concepts of OO Technology Chapter 3. Advantages of OO Technology Chapter 4. Object Oriented Programming

Object Oriented Basics

1. What Object Orientation is

In this chapter, you will learn ... 1.1 Object 1.2 Object Orientation

1-4

1. What Object Orientation is

1.1 Object
Physical things (Car, Camera, etc.) OBJECTS Abstract things (Job, Weather, etc.)

The real world consists of Objects.

1-5

1.1 Object

1.2 Object Orientation (1/2)

Skilled Painter's approach

Conventional approach

Object Oriented approach

1-6

1.2 Object Orientation

1.2 Object Orientation (2/2)


Piece Piece Object

Object

Difficult to keep coordination


Object

Easy modification !

Conventional approach

Object oriented approach

1-7

1.2 Object Orientation

2. Basic Concepts of OO Technology


In this chapter, you will learn ... 2.1 Object 2.2 Class 2.3 Inheritance & Override 2.4 Instance 2.5 Message 2.6 Polymorphism

2-8

2. Basic Concepts of OO Technology

2.1 Object
Object name Data

Method

Jukebox Record disks Titles of music play music stop music accept a coin

A system consists of Objects. Objects consist of Data and Methods. The data and the methods in an object are strongly related.

2-9

2.1 Object

Encapsulation
Jukebox Record disks Titles of music play music stop music accept a coin Necessary data for this Object Necessary methods for this Object

Data and Methods are encapsulated as an object. We dont have to know the internal structure of the object.

2-10

2.1 Object

2.2 Class
Class name Data Method Car owner type color speed up slow down turn

Katsra's car

Ryo's car

Shuji's car

Keisuke's car

2-11

2.2 Class

Generalization/Specialization
Generalization -Super-class Vehicle

Car Specialization -Sub-class Convertible Sedan

Truck

: Is a or Is a kind of Relationship Example : A Car is a kind of Vehicle.


2-12 2.2 Class

Aggregation
A class has or contains another class.
contains or is composed of

CAR Whole

SEAT Part

Computer
A computer has CPU, memory and disk.

CPU
2-13

Memory
2.2 Class

Disk

2.3 Inheritance & Override


Superclass
Vehicle owner type color speed up slow down turn

Data and methods are inherited to the subclasses.

Subclass
Taxi customer carry customer
Peculiar data Peculiar method

Subclass
Truck cargo transport cargo

2-14

2.3 Inheritance & Override

Override
employee_info name birthday age address calc_age(birthday) calc_salary(age)

inheritance
executive_info post calc_salary(age)

Override: Same name, but different function

2-15

2.3 Inheritance & Override

(Reference) Overload
employee_info name birthday age post address calc_age(birthday) calc_salary(age) calc_salary(age,post)

Overload: Same method name, with different arguments

2-16

2.3 Inheritance & Override

2.4 Instance
CLASS
Car owner type color speed up slow down turn Class is used as a mold to make an INSTANCE.

Construction of Instance INSTANCE ownerRyo's car : Car : Ryo


type : Sports car color : RED speed up slow down turn

Private Data

2-17

2.4 Instance

2.5 Message

MESSAGE is an interface between Objects. When an Object asks another Object to do something, the first Object sends a MESSAGE to the second Object.

2-18

2.5 Message

Example of Message
MESSAGE is an interface between objects.

mike.go_to(Tengeru)

diana :Customer Age : 29 Sex : Female speak(string) commute(string)

mike :Taxi Driver Age : 45 Sex : Male speak(string) go_to(string)

2-19

2.5 Message

2.6 Polymorphism
We use the same verb in different Objects to do different behaviors. (e.g. We use OPEN to open a window and a data file. The ways to open are different, but the verb is the same.) That is called POLYMORPHISM. Its an abstraction of Message.

2-20

2.6 Polymorphism

Example of Polymorphism
rio : Cat cry() attack() move() MIAOW
Scratch Jump

Kei:Master

hamu.cry()

hamu : Dog cry() attack() move() ku-chi : Parakeet cry() attack() move() PIYOPIYOP
Pick Fly

BOWWOW
Bite Run

The messages are the same cry(). But the behaviors are different.
2-21

2.6 Polymorphism

Summary
In this chapter, you have studied about ... Object, Encapsulation Class Generalization, Specialization, Aggregation Inheritance, Override Instance Message Polymorphism
2-22 2. Basic Concepts of OO Technology

Drill 2-1
Encapsulation
Encapsulate the following data items and methods into 2 groups and make 2 objects. Write your answer on the next page. Object name
Bank account Foreign exchange

Data
Customer_name Account_Number Yen Dollar Balance Exchange_rate

Method
Withdraw_cash Exchange_Dollar_to_TZS Exchange_TZS to_Dollar Deposit_cash

Object name

Example
Data
2-23 2. Basic Concepts of OO Technology

Methods

Drill 2-1
Answer

2-24

2. Basic Concepts of OO Technology

Drill 2-2

Inheritance Make a class hierarchy chart with the following words. You can use each word only once.
Class name Personal_info Data name address telephone_number birthday
2-26

Customer_info

Employee_info

salary position department amount_billed

2. Basic Concepts of OO Technology

Drill 2-3

Aggregation Make Object model of the following objects. Object Model of a Car Tire Car Engine Door

2-27

2. Basic Concepts of OO Technology

3. Advantages of OO Technology
In this chapter, you will study about ... 3.1 Problems in Current System Development 3.2 OO Technology Characteristics 3.3 OO System Development 3.4 What is OO Heading for ...

3-29

3. Advantages of OO Technology

3.1 Problems in Current System Development

Software Crisis
1) Inferior Quality
Specifications not fully met

2) Decreased Productivity
Prolonged period of development

3) Lower Maintainability
Difficult to maintain due to complexity in structure

3-30

3.1 Problems in Current System Development

3.2 OO Technology Characteristics


Characteristics of OO technology 1) Reuse of software Difference programming Class Library 2) Polymorphism 3) Data hiding 4) Iterative and Incremental development
3-31 3.2 OO Technology Characteristics

3.3 OO System Development


Pick up Objects from target business. Make fundamental data and methods. Necessary data and methods are added step by step, with repeating short cycle of analysis, design, programming, and testing. Working target is always object throughout the development process. When design is done, implementation and testing are performed

Iterative and Incremental development

Account
Int x, y getPassword(aN){ }

Account
Int x, y getPassword(aN){ } calcBalance(aN){ }

import java.applet.*; import java.awt.*; public class Account extends Applet{ int x, y; public void getPassword(aN) { } public static calcBalance(xxx) }

Cut Over

3-32

3.3 OO System Development

3.4 What is OO Heading for ...


Characteristics of OO technology Target of system development

Reuse of software Polymorphism Data hiding


Iterative and Incremental development

Improved Quality Improved Productivity Improved Maintainability

3-33

3.4 What is OO Heading for

4. Object Oriented Programming

In this section, you will learn ... 4.1 OO Programming Languages 4.2 JAVA Language 4.3 Object Oriented Programming

4-35

4. Object Oriented Programming

4.1 OO Programming Languages


Smalltalk-80
Pure object oriented language developed by XEROX PaloAlto Research lab.

Eiffel
Pure object oriented language developed by Interactive Software Engineering

C++
Extended language from C developed by AT & T Bell lab.

Java
C++ like object oriented language developed by Sun Microsystems. Portable, Network, Architecture independent.

4-36

4.1 OO Programming Languages

4.2 JAVA Language


4.2.1 Language Features 4.2.2 Online Documents

4-37

4.2 JAVA Language

4.2.1 Language Features


Object Oriented C or C++ like Simple Architecture independent

4-38

4.2.1 Language Features

4.2.2 Online Documents


Java Technology homepage
http://java.sun.com

The Java Language Specification


http://java.sun.com/docs/books/jls/index.html

Java API Documentation


http://java.sun.com/j2se/1.4.2/docs/api/index.html

Java Language Tutorial


http://java.sun.com/docs/books/tutorial/index.html

4-39

4.2.2 Online Documents

4.3 Object Oriented Programming


4.3.1 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 First Step(Application, Applet) Class Packages Instance Encapsulation Polymorphism Others

4-40

4.3 Object Oriented Programming

4.3.1 First Step(Application, Applet) : Example1 (1/2)

Application
1) Example1. java
// A first program public class Example1 { public static void main(String argv[]){ int i; for(i=1;i<=10;i++){ System.out.println(i+":Welcome to JAVA!"); } } }

2) To compile it, enter ...


... javac Example1.java _

3) To run it with JAVA interpreter, enter ...


... java Example1 _
4-42 4.3.1 First Step(Application, Applet)

4.3.1 First Step(Application, Applet) : Example1 (2/2)

1) Example1.java A simple JAVA program is shown on the previous page. This is a stand-alone program that writes Welcome to JAVA! to standard output ten times. You should use a text editor such as the notepad to enter the source code. Note also that you need to use .java as an extension for all JAVA source codes. 2) Compiling javac is a command used to compile JAVA source programs. You need to enter this command at the DOS-window command line. When compilation of a program is completed normally, a file with the .class extension is generated. This is a JAVA binary class file. 3) JAVA Interpreter A JAVA interpreter is available to run this stand-alone JAVA application. Use the java command to run a class file specifying the class name to be loaded as an argument.

4-43

4.3.1 First Step(Application, Applet)

Exercise 1 : How to compile


// A First Program public class Exercise1{ public static void main(String argv[]){ int i; for(i=1;i<=10;i++){ System.out.println(i+":Welcome to JAVA!"); } } }

4-45

4.3.1 First Step(Application, Applet)

Applet
CLASS : java.applet.Applet
init(); start(); paint(); stop(); destroy();

inherits or overrides

User defined applet as a subclass of Applet class


init(); start(); paint(); stop(); destroy();

4-46

4.3.1 First Step(Application, Applet)

Exercise2 (1/2) : First Applet


Exercise2.java

1) // Hello World! This is a comment. import java.awt.*; 2) import java.applet.*; 3) public class Exercise2 extends Applet { public void paint( Graphics gc ) { 4) int x, y; x = 100; y = 400; gc.drawString( "Hello World ! ",125,95); 6) gc.drawString( "x + y = " + (x+y),125,115); } }

(5)

4-47

4.3.1 First Step(Application, Applet)

Exercise2 (2/2) : Applet in a Web page


Exercise2.html
<html> <head><title>Exercise2</title></head> <applet code=Exercise2 width=400 height=200></applet> </html>

4-48

4.3.1 First Step(Application, Applet)

Exercise 1 : How to compile


// A First Program public class Exercise1{ public static void main(String argv[]){ int i; for(i=1;i<=10;i++){ System.out.println(i+":Welcome to JAVA!"); } } }

4-49

4.3.1 First Step(Application, Applet)

Applet
CLASS : java.applet.Applet
init(); start(); paint(); stop(); destroy();

inherits or overrides

User defined applet as a subclass of Applet class


init(); start(); paint(); stop(); destroy();

4-50

4.3.1 First Step(Application, Applet)

Exercise2 (1/2) : First Applet


Exercise2.java

1) // Hello World! This is a comment. import java.awt.*; 2) import java.applet.*; 3) public class Exercise2 extends Applet { public void paint( Graphics gc ) { 4) int x, y; x = 100; y = 400; gc.drawString( "Hello World ! ",125,95); 6) gc.drawString( "x + y = " + (x+y),125,115); } }

(5)

4-51

4.3.1 First Step(Application, Applet)

Exercise2 (2/2) : Applet in a Web page


Exercise2.html
<html> <head><title>Exercise2</title></head> <applet code=Exercise2 width=400 height=200></applet> </html>

4-52

4.3.1 First Step(Application, Applet)

4.3.2 Class
1) 2) import packageName; [modifier] class ClassName [extends SuperclassName] { //declaration of instance variables [modifier] type variableName [=IntialValue]; //declaration of methods [modifier] type methodName(argType argName, ...) { //process description //... } }

3)

4)

4-53

4.3.2 Class

4.3.3 Packages
java

lang
String Thread

io
File

applet
Applet Audio Clip

net
URL Socket

awt
Graphics Component

4-54

4.3.3 Packages

Graphics Class
Shape Drawing Methods in the Graphics Class
Method draw3DRect() drawArc() drawLine() drawOval() drawPolygon() drawRect() drawRoundRect() fillRoundRect() fillArc() fillOval() fillPolygon() fillRect() fillRoundRect() Description Draws a highlighted, 3D rectangle Draws an arc Draws a line Draws a oval Draws a polygon Draws a rectangle Draws a rounded-corner rectangle Draws a filled,highlighted,3D rectangle Draws a filled arc Draws a filled oval Draws a filled polygon Draws a filled rectangle Draws a filled, rounded-corner rectangle

4-55

4.3.3 Packages

4.3.4 Instance
import java.awt.*; import java.applet.*;

: Example 2

public class Example2 extends Applet { Class Button theButton; public void init(){ theButton = new Button("Push me"); add(theButton); } } Constructor (a special method that has the same name as class)

Instance

4-56

4.3.4 Instance

Exercise 4 (1/2) : My First Applet


import java.awt.*; import java.applet.*; public class Exercise4 extends Applet { static final String message = "My First Applet"; private Font font; public void init() { font = new Font( "Helvetica", Font.BOLD, 36 ); } public void paint(Graphics g){ g.setColor(Color.magenta); g.fillOval(10,10,330,100); g.setColor(Color.cyan); g.fillOval(15,15,320,88); g.setColor(Color.red); g.setFont(font); g.drawString( message,40,70 ); } }

4-57

4.3.4 Instance

Exercise4 (2/2) : My First Applet


Exercise4.html
<html> <head> <title>Exercise4</title> </head> <applet code=Exercise4 width=400 height=150></applet> </html>

4-58

4.3.4 Instance

4.3.5 Encapsulation
It is possible to achieve encapsulation by using the following modifiers.

Modifiers
public protected (default) private

Meaning of each modifier


Accessible from all other objects. Accessible only from their own subclass and classes in the same package. Accessible only from classes in the same package. Accessible only from their own class.

4-59

4.3.5 Encapsulation

4.3.6 Polymorphism(1/3)
Employee public calc_salary( )

: Exercise5

SalesPerson public calc_salary( ) 1) Exercise5.java


import java.awt.*; import java.applet.*; public class Exercise5 extends Applet { private Employee you, you2, you3; public void init() { you = new Employee(); you2 = new SalesPerson(); you3 = new Executive(); } public void paint(Graphics g) { g.drawString(you.calc_salary(25),125,75); 4-60

Executive public calc_salary( )

g.drawString(you2.calc_salary(16),125,95); 4.3.6 Polymorphism g.drawString(you3.calc_salary(56),125,115);

4.3.6 Polymorphism(2/3)
2) Employee.java
public class Employee { public String calc_salary(int age) { return salary = + age*1000; }

: Exercise5

3) SalesPerson.java }
public class SalesPerson extends Employee { public String calc_salary(int numOfSales) { return salary = + (numOfSales*500 + 20000); } } 4) Executive.java public class Executive extends Employee { public String calc_salary(int age) { return salary = + age*10000; }

5) Exercise5.html }
<html> <head><title> Exercise5 </title></head> <applet code=Exercise5 width=400 height=200></applet> 4-61 </html> 4.3.6 Polymorphism

4.3.6 Polymorphism(3/3)
1) Exercise5.java

: Exercise5

Even though the same name method, calc_salary() is called, using a reference variable declared as being of the same class type, Employee, the processing of the method varies depending on the referenced instance. This is called polymorphism. 2) Employee.java Instances of this class have a method called calc_salary, which calculates salaries of regular employees based on their ages. Employee is the superclass of the SalesPerson and Executive classes. 3) SalesPerson.java This is a subclass of the Employee class. It overrides the calc_salary method and calculates the salary of employees in the Sales department based on their unit sales. 4) Executive.java This is a subclass of the Employee class. It overrides the calc_salary method and calculates salaries of executives based on their ages. 5) Exercise5.html Displays salaries of regular employees, employees in the Sales department and executives in a browser.
4-62 4.3.6 Polymorphism

4.3.7 Others (Comments)


1) // This is a single line comment. 2) /* This is a mutilate comment. */ 3) /** * This indicates a special doc comment. * A doc comment is commentary that is extracted * by automated documentary generators, such as * javadoc program. */

4-63

4.3.7 Others

4.3.7 Others (Data Type)


Type
boolean char byte short int long float double
4-64

Definition
true or false 16-bit Unicode character 8-bit signed integer 16-bit signed integer 32-bit signed integer 64-bit signed integer 32-bit floating~point value 64-bit floating~point value
4.3.7 Others

4.3.7 Others (Statement) (1/6)


1) Branch (if) 2) Repetition (for) 3) Repetition (while) 4) Multiple branch (switch) 5) Exception Handling (try)

4-65

4.3.7 Others

4.3.7 Others (Statement) (2/6)


1) Branch (if)
if (condition ) { statement; ... } else { statement; ... }

4-66

4.3.7 Others

4.3.7 Others (Statement) (3/6) : Example3 2) Repetition (for)


for ( initialization; conditional; incrementor ) { statement; ... } import java.awt.*; import java.applet.*; public class Example3 extends Applet { public void paint(Graphics gc){ int i; for(i=0; i<100; i=i+10) { gc.drawRect(20+i,20+i,300-i*2,250-i*2); }
The for statement is used to repeat statements a specific number of times. } can basically write the for statement in the same manner as in the C You language. ( ) includes the initialization of the loop variable, condition for terminating the repetition, and a formula to update this variable. 4-67 4.3.7 Others

4.3.7 Others (Statement) (4/6)


3) Repetition (while)
while( condition ) { statement; ... }

do { statement; ... } while( condition );

4-68

4.3.7 Others

4.3.7 Others (Statement) (5/6)


4) Multiple branch (switch)
switch ( int expression ) { case int expression : statement; break; case int expression : statement; break; ... default : statement; }

4-69

4.3.7 Others

4.3.7 Others (Statement) (6/6) : Example4


5) Exception Handling(try)
import java.awt.*; import java.applet.*; public class Example4 extends Applet { public void paint(Graphics g){ int i=0, k=0; try{ for(i=-2;i<2;i++){ g.drawString(" "+i,50,70+i*20); k=3/i; When I=0, arithmetic exception occurs. } }catch(ArithmeticException e){ g.drawString("Divided by zero at i="+i,50,140); }finally{ g.drawString("Done.",50,160); } } }
4-70 4.3.7 Others

Exercise6 : Handling Exceptions


import java.awt.*; public class Exercise6 extends java.applet.Applet { public void paint(Graphics g){ int i=0; int [] array = new int [10]; try{ for(i=0;i<=10;i++){ array[i]=i; g.drawString("No."+i,50,20+i*20); } }catch(ArrayIndexOutOfBoundsException e){ g.drawString("No."+i+" is out of bounds.",50,240); }finally{ g.drawString("Done.",50,260); } } }

4-71

4.3.7 Others

Vous aimerez peut-être aussi