Lecture2
Lecture2
Applications Programming
LECTURE 2
Object Oriented Design Concepts
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Passing Objects
A variable has a name, type & value
The value can be a primitive type eg
Or a reference type eg
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
1
Memory
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
References
gender String
colour String
breed String
age int 9
spayed boolean 1
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
More on References
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
2
Array Object Diagram
Name Type Value
dogs Dog[]
0
1
2
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
3
4 Design Levels
Planning
Manage stages; analysis, design, code, test etc
System Design
Design the overall system; classes, attributes etc
Evaluate with design rules
Code Design
Write clear, simple, reusable code; naming,
spacing, simplicity, modularity
Coding
Assignments, if, loops, arrays etc
Classes
A class is defined by its behaviour.
If two things behave the same way, they are two objects of
the same class (or related classes)
Object Class
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Names
Listen to the words used
A noun can be a
– Class (or Object): Student, Dog,
tracy
– Attribute (or variable): amount,
length, price
– Function: area, volume, cost
A verb can be a
– Procedure: sell, show
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
4
More on Names
Generic verbs like "find" and "calculate" tell you that a function
is coming up:
Example
– "find the income from seat sales"
– "find the personal profit after tax"
– "calculate the attendance"
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Area of a Rectangle
– Method area() class Rectangle
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
5
Exercise
We want to
Calculate the area of a Rectangle
Calculate the perimeter of a Rectangle
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Answer
public class Rectangle
{
private double length;
private double width;
public Rectangle()
{
}
public Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
public double area()
{
return length*width;
}
public double perimeter()
{
return 2*(length + width);
}
}
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Rectangle Class
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
6
Answer
public double getLength()
Accessors
{
return length;
}
public double getWidth()
{
return width;
}
Mutators
public void setLength(double length)
{
this.length = length;
}
public void setWidth(double width)
{
this.width = width;
}
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Rectangle
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Rectangle
Default constructor:
Alternate constructor:
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
7
toString()
The method: public String toString()
Should be added to your classes. Use it to return the values of
the attributes of a class as a String
Integer.toString(height); or
Double.toString(height);
What would the toString() method return for the Rectangle class?
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Overloading
A method can be overloaded
Java matches a method call to a method definition with the
– name
– arguments: number, type, and order
This is called the signature of the method.
More than one method can have the same name in a class or
package
Method signature can not be the same ie cannot have the exact
same arguments
Example:
sleep()
sleep(int time)
sleep(boolean stop)
sleep(int start, int time)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Overloading Example
public Rectangle()
{
}
Alternate Constructor –
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
8
Another Overloading Example
add()
– This method would use attributes set in the constructor
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
OO Design Principles
Place the code in a set of small
methods.
– Methods should be less than 8
lines of code
Methods should do one single thing
only
Place the methods and data in a set of
classes.
Hide the data and as many methods as
possible. Make attributes & methods
private
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Class Encapsulation
This creates a system with high cohesion (the code lives with
the data) and low coupling (little data is passed between
classes); that is, a set of reusable classes.
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
9
OO Design Rules
An attribute should be private, to enforce
class encapsulation.
Initialise an attribute in the declaration if
possible, example
– private double balance = 0;
Use as few attributes as possible.
– Use local variables if you can
– names should be clear, simple, and
descriptive.
Literals are bad; use a constant.
– private final int DAYS_IN_YEAR = 365; not
a naked value in code, example,
if (days < 365) should be
if (days < DAYS_IN_YEAR)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Local Variables
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Parameters
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
10
Parameter Binding
Parameters are
Find the matching method bound in order;
the first parameter
gets the first
Create local variables for the parameters argument, the
second parameter
gets the next value
Bind the parameters to the argument values (argument), and so
on.
Method calls
Use the parameters inside the method supply values
(arguments);
method headers
Delete the parameters when the method exits
supply variables
(parameters).
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Static Attributes
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
11
Static Methods
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
12
public static void main()
If you need it, then make it the only static member in the
whole system (if you can).
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Key Concepts
Summary
When an object is created, memory is
allocated to store it
Design – get the pieces, connect the
pieces, convert into OO Code
4 Design levels: Planning, System
Design, Code Design, Coding
Noun is a class, object or attribute
Verb is a procedure
Feature “of” class
Rule: Never Repeat Code
Rule: Minimal Use of Static
Rule: Few Public mutators
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
13