Вы находитесь на странице: 1из 35

ADVANCED

PROGRAMMING
W
E
E
K

Revision Of
Object-Oriented
Programming

OBJECTIVES
After studying this chapter you should be able to:
Understand and apply the principles of good
programming in developing Java program.
Elaborate the characteristics of Object-Oriented
Programming.
Develop Java program by using proper problem
solving technique.

Classes and Objects


Object-oriented programs use objects.
An object is a thing, both tangible and intangible.
Account, Vehicle, Employee, etc.
To create an object inside the computer program, we
must provide a definition for objectshow they
behave and what kinds of information they maintain
called a class.
An object is called an instance of a class.

Graphical Representation of a
Class
<Class Name>

Example:

Account

We
Weuse
useaarectangle
rectangleto
to
represent
a
class
with
represent a class with
its
itsname
nameappearing
appearing
inside
the
inside therectangle.
rectangle.

Motorcycle

The notation we used here is based on the industry standard


notation called UML, which stands for Unified Modeling Language.

Graphical Representation of a Object


<Object Name>

We
Weuse
useaarectangle
rectangleto
to
represent
an
object
represent an objectand
and
place
the
underlined
place the underlined
name
nameof
ofthe
theobject
object
inside
the
rectangle.
inside the rectangle.

Example:
SV198

This
Thisisisan
anobject
objectnamed
named
SV198.
SV198.

Message and Methods


To instruct a class or an object to perform a task, we send
a message to it.
You can send a message ONLY TO the classes and
objects that understand the message you sent to them.
A class or an object must possess a matching method to
be able to handle the received message.
A method defined for a class is called a class method, and
a method defined for an object is called an instance
method.
A value we pass to an object when sending a message is
called an argument of the message.

Sending a Message
Message
Messagedeposit
depositwith
with
the
argument
250.00
the argument 250.00isis
sent
sentto
toaaBankAccount
BankAccount
object
SV198.
object SV198.

deposit 250.00

SV198 : BankAccount

Class and Instances Data


Values
An object is comprised of data values and methods.
Data values are also called data members (belongs to a class or instance of a
class)
An instance data value is used to maintain information specific to individual
instances. For example, each BankAccount object maintains its current balance.
A class data value is used to maintain information shared by all instances or to
represent collective information about the instances.
For example, minimum balance is the information shared by all Account
objects.
Two types of data values: variable a data value that can change, constant
one that cannot change

Sample Instances Data


Values
SV129 : BankAccount

SV098 : BankAccount

SV211 : BankAccount

current balance
908.55

current balance
1304.98

current balance

All
Allthree
threeBankAccount
BankAccount
objects
possess
objects possessthe
the
same
instance
data
same instance data
value
valuecurrent
currentbalance.
balance.

354.00

The
Theactual
actualdollar
dollar
amounts
are,
amounts are,of
ofcourse,
course,
different.
different.

Sample Classes Data Values


BankAccount
minimum balance
100.00

There
Thereisisone
onecopy
copyofof
minimum
minimumbalance
balancefor
for
the
whole
class
and
the whole class and
shared
sharedby
byall
allinstances.
instances.

This
Thisline
lineisisan
an
instance-of
instance-of
relationship.
relationship.
SV129 : BankAccount

SV098 : BankAccount

SV211 : BankAccount

current balance
908.55

current balance
1304.98

current balance
354.00

Java Language Coding


Guidelines
Comment :Javadoc
Documentation
Javadoc is a tool for generating Java documentation
in HTML format.
The utility translates doc comments that are
delimited by /** and */ in the source code.
The resulting documentation resembles that for the
standard Java classes.

Java Language Coding


Guidelines
Javadoc Example
/** Class to demo how to build javadocs
@author Rohaida Romli
@version 1.0
*/
public class TestJavaDoc
{
public static void main(String[] args)
{
System.out.println("Testing java doc");
viewInfo(Mawarny",11111);
}

Class Comment

/** This is how to comment method


@param name for argumen name
@param phoneNo for argumen phone number
*/
public static void viewInfo(String name, int phoneNo)
{
System.out.println("Name
:"+name);
System.out.println("Phone :"+phoneNo);
}

Method Comment

Sample

Java Language Coding


Guidelines

View Javadoc Documentation

In Kawa, choose Build>Javadoc


You can view the class documentation generated
in javadoc format using browser (IE, Netscape)
or execute this command (through DOS)
javadoc TestJavaDoc.java
javadoc document

Java Language Coding


Guidelines
Formatting Style

consistent indentation especially in repetition(looping)


and if statements.
Example :
for (int counter=0;counter<=MAX; counter++)
System.out.println("Counter:"+counter);
if ( pilihan == 1 )
System.out.println("Pilihan 1.");
else if ( pilihan == 2 )
System.out.println("Pilihan 2.");
else
System.out.println("Pilihan selain 1,2.");

Java Language Coding


Guidelines
Naming Convention

All variables,method names, attributes start with


lowercase.
Example : viewInfo(), name, studentId, staffId
All constants are in uppercase.
Example: MAX, MONTH_PER_YEAR
All class start with uppercase.
Example : Student, Lecturer, Car
Use descriptive names for meaningful code.
Do not use i,a,b,c1,c2 etc.

Unified Modeling Language


The Unified Modeling Language (UML) facilitates
object-oriented design (OOD).
It uses graphic symbols to represent classes and the
relationships among them.
A class is represented by a box in which its name,
fields, and methods are listed.
A relationship between two classes is represented by
a arrow connecting them.

Unified Modeling Language


UML Symbols
UML uses stylized arrows to represent the
relationships between classes:
Relationshi
p
inheritance
aggregation
composition

Symbol

Unified Modeling Language


UML Example
Department

Person

Company

Voter

Unified Modeling Language


Access Category Symbols
UML uses the +, , and # characters to indicate
the access category for class members:
Access

Character
public
+
protected
#
private

Unified Modeling Language


UML Class Representation
Fields are listed in the second part
with the syntax
<access> <name> : <type>

These define the objects state.


Methods are listed in the third part
with the syntax
<access> <signature> : <return-type>

These define the objects behavior.

Unified Modeling Language


public vs private?
You must decide when a variable, constant or method
should be private or public.
public (+) choose public when you want the
programmer who uses your class to have access to
the method or variable
private (-) when you want to control the access
only from the class itself.

Characteristic Of ObjectOriented Programming


Abstraction
Encapsulation
Inheritance
Polymorphism
Aggregation
Composition

Characteristic Of ObjectOriented Programming


Abstraction
Abstraction is the process of finding the essential
feature set for a class.
Concentrating on what an object is and does before
making any decision about how the object will be
implemented.
Capture the attributes and operation of a object/realworld deck.

Characteristic Of ObjectOriented Programming


Encapsulation
Encapsulation is the process of hiding object data
and providing methods for data access.
Each object of a class has its own set of instance
fields.
Instance fields are declared with the access specifier
private, public or protected.

Characteristic Of ObjectOriented Programming


Inheritance
Inheritance is the process of deriving new classes from
existing classes.
It is represented as tree, which consists of superclasses and
subclasses.
A superclass is a parent class or a base class.
A subclass is a child class, an extended class or a derived
class.
A subclass inherits accessible data fields and methods from
its superclass, and may also add new data fields and methods.

Characteristic Of ObjectOriented Programming


Inheritance
Example:
Cylinder class and Sphere class derived from circle
class are called as subclasses, while circle class is
called a superclass.
Superclass

Circle

Sphere

Cylinder

Subclass

Characteristic Of ObjectOriented Programming


Polymorphism
Denote the principle that behavior can vary depending on the
actual type of an object.
Consists of two concepts namely:
a) overloading
- when a single class has several methods with the
same name but different parameter types.
b) overriding
- when method defined in the subclass using the
same name as in its superclass.

Characteristic Of ObjectOriented Programming


Polymorphism
Example:
Circle
Overriding

Area()

Sphere

Cylinder

Area()

Luas()
Luas()
OVERLOADING

Characteristic Of ObjectOriented Programming


Aggregation
Aggregation is a special form of association that
represents an ownership relationship between two
classes
Models has-a relationship
In Java, an object is an aggregate of another member
object if:
it has a field reference to the member object
the member object can exist independently

Characteristic Of ObjectOriented Programming


Composition
Aggregation is a stronger form of association in
which the composition has sole responsibility for
managing its part.
In Java, an object is an composite of another object
part if:
it has a field reference to the object part;
the object part cannot exist independently;

Characteristic Of ObjectOriented Programming


Aggregation & Composition
Example:

Name

Person
composition

Address
aggregation

Characteristic Of ObjectOriented Programming


Aggregation & Composition
Example:
Public class Name{
/** Data field omitted*/
/**Constructors omitted */
/**Methods omitted*/
}

Public class Address{


/** Data field omitted*/
/**Constructors omitted */
/**Methods omitted*/
}

Public class Person{


/** Data field omitted*/
private Name name;
private Address address;
/**Constructors omitted */
/**Methods omitted*/
}

Problem-solving Process
In the programming environment, the problemsolving process involves the following steps:
Analyze the problem, outline the problem and its
solution requirements and design an algorithm to solve
the problem.
Implement the algorithm in a programming language,
such as Java and verify that the algorithm works.
Maintain the program by using and modifying it if the
problem domain changes.

Problem-solving Process

References
Thomas Wu. C, An Introduction To Object-Oriented
Programming With Java. (2006). Mc Graw Hill.
Liang, D. (2005). Introduction To Java programming.
Prentice Hall.
Horstman, C. (2006). Big Java (2nd ed). New York: John
Wiley & Sons.

Вам также может понравиться