Object Oriented Programming: Introduction To
Object Oriented Programming: Introduction To
object
oriented
programming
problem solving
The key to designing a solution is breaking it down
into manageable pieces
State (Modifier)
Variable/Data/Field
(OOP) SW
Object has: Method – to do something
on the project/by project
Method can return values - A result
that the method has computed
and returns it to the caller – Can
have 0 or 1 value only
4
concept of objects
object stores its state in fields = variables
operate on an object's
internal state
methods primary mechanism for
object-to-object
communication
Good…
1. Modularity:
The source code for an object can
be written and maintained
independently of the source code
for other objects.
2. Information-hiding:
interaction only with an object's
methods, internal implementation
remain hidden from the outside
world.
3. Code re-use:
can use object written by experts in
your program.
CLASS
Object
Object
description/blue
print of a kind of An object is
object. It does not called an
by itself create instance of a
any objects class
example of class
class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear);
}
}
creating objects
Notes :
class BicycleDemo {
public static void main(String[] args) {
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}
public class CreateObjectDemo {
rectTwo.origin = originOne;
eg.
String name = new String(“Muhammad Haziq”);
12
Constructing String objects
• New String objects are created whenever the String
constructors are used:
String name4 = new String(); // Creates an object
String name5 = new String("Socrates");
String name6 = name4;
13
Invoking Methods
• We've seen that once an object has been instantiated, we
can use the dot operator to invoke its methods
name.length()
14
Object without object reference
cannot be accessed
: String
sv1: String
value = “Abu”
value = “Ali”
15
Object References
16
References
• Note that a primitive variable contains the value
itself, but an object variable contains the address
of the object
17
Assignment Revisited
• The act of assignment takes a copy of a
value and stores it in a variable
num1 38
Before:
num2 96
num2 = num1;
num1 38
After:
num2 38 18
Object Reference Assignment
• For object references, assignment copies the
address:
name1 "Steve Jobs"
Before:
name2 "Steve Austin"
name2 = name1;
19
Questions
String stud1;
stud1 = new String(“Ani”);
stud1 = new String(“Obi”);
Method Type of
return
A UML Class Diagram value
The notation we used here is based on the industry standard notation
called UML, which stands for Unified Modeling Language.
22
Object Instantiation
class
object
23
Object Design Questions
• What role will the object perform?
• What data or information will it need?
– Look for nouns.
• Which actions will it take?
– Look for verbs.
• What interface will it present to other objects?
– These are public methods.
• What information will it hide from other objects?
24
– These are private.
Design Specification for a
Rectangle
• Class Name: Rectangle
• Role: To represent a geometric rectangle
• States (Information or instance variables)
- Length: A variable to store rectangle’s length (private)
- Width: A variable to store rectangle's width (private)
• Behaviors (public methods)
- Rectangle(): A constructor method to set a rectangle’s
length and width
- calculateArea(): A method to calculate a rectangle’s
area 25
UML Design Specification
Instance variables -- memory locations
used for storing the information needed.
Class Name
26
Method can has input (parameter)
& output (return value)
• Parameter : value given to method so that it can do
its task
• Can has 0 or more parameter
• Return value: A result that the method has
computed and returns it to the caller
• Can returns 0 or 1 value
• Eg. - pow(2,3)
- calculateArea()
- getBalance( )
- move( )
Continued…
27
Method Declarations
• A method declaration specifies the code that will
be executed when the method is invoked (called)
28
Method Control Flow
• If the called method is in the same class, only
the method name is needed
compute myMethod
myMethod();
29
Method Control Flow
• The called method is often part of another
class or object
main doIt helpMe
obj.doIt(); helpMe();
30
Method Design
• What specific task will the method
perform?
• What input data will it need to perform its
task?
• What result will the method produce?
• How input data are processed into result?
• What algorithm will the method use?
31
Method calculateArea()
Algorithm
• Method Name: calculateArea()
• Task: To calculate the area of a rectangle
• Data Needed (variables)
– length: A variable to store the rectangle's length
– width: A variable to store the rectangle's width
– area: A variable to store result of calculation
• Processing: area = length x width
• Result to be returned: area 32
Coding into Java
public class Rectangle // Class header
{
private double length; // Instance variables
private double width;
return area;
} // calculateArea()
} // Rectangle class
33
Method calculatePerimeter()
Algorithm
• Write an algorithm to calculate the
perimeter of a rectangle.
• Write the method in Java.
34
calculatePerimeter()
Algorithm
• Method Name: calculatePerimeter()
• Task: To calculate the perimeter of a
rectangle
• Data Needed (variables)
– length
– width
– perimeter
• Processing: perimeter = 2 x(length + width)
• Result to be returned: perimeter 35
calculatePerimeter() in Java
code
public double calculatePerimeter()
{
double perimeter;
perimeter = 2 * (length + width);
return perimeter;
} // calculatePerimeter()
36
Creating Rectangle Instances
• Create, or instantiate, two instances of the
Rectangle class:
Rectangle rectangle1 = new Rectangle(30,10);
Rectangle rectangle2 = new Rectangle(25, 20);
37
Using Rectangle Instances
• We use a method call to ask each object to
tell us its area:
System.out.println("rectangle1 area " + rectangle1.calculateArea());
System.out.println("rectangle2 area " + rectangle2.calculateArea());
References to
Method calls
objects
38
Syntax : Object Construction
• new ClassName(parameters);
– Example:
• new Rectangle(30, 20);
• new Car("BMW 540ti", 2004);
– Purpose:
• To construct a new object, initialize it with
the construction parameters, and return a
reference to the constructed object.
39
The RectangleUser Class
Definition
Class
Definition
An application must
public class RectangleUser
have a main() method
{
public static void main(String argv[])
Object
{ Creation
Rectangle rectangle1 = new Rectangle(30,10);
Rectangle rectangle2 = new Rectangle(25,20);
System.out.println("rectangle1 area " +
rectangle1.calculateArea());
System.out.println("rectangle2 area " +
rectangle2.calculateArea());
} // main()
} // RectangleUser Object
Use
40