0% found this document useful (0 votes)
2 views

Lecture2

The document covers Object Oriented Design Concepts, focusing on how objects are stored in memory, the principles of good OO design, and the use of overloading, static methods, and attributes. It outlines the steps for creating reusable code, the importance of encapsulation, and the rules for effective programming. Key concepts include the distinction between classes and objects, the significance of method signatures, and guidelines for minimizing static members and public mutators.

Uploaded by

koximi1998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture2

The document covers Object Oriented Design Concepts, focusing on how objects are stored in memory, the principles of good OO design, and the use of overloading, static methods, and attributes. It outlines the steps for creating reusable code, the importance of encapsulation, and the rules for effective programming. Key concepts include the distinction between classes and objects, the significance of method signatures, and guidelines for minimizing static members and public mutators.

Uploaded by

koximi1998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Information Technology

Applications Programming

LECTURE 2
Object Oriented Design Concepts

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

Object Oriented Design Concepts


Learning Objectives
At the end of the lecture, you should be able to:

 Discuss how objects are stored in memory


 Use and describe Overloading
 Describe features of good OO Design
 Use and describe static methods and attributes

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

double height = 3.0;

 Or a reference type eg

Dog billy = new Dog("M", "White", "Corgi", 11, true);

 A reference variable is a reference to an object; the location of


the object in memory

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

1
Memory

Name Type Value


height double 3.0
billy Dog 69E77A20F

 Memory location is hexadecimal (base 16)


 We don’t normally care what the memory
location is, so we draw the value as a
reference

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

References

Name Type Value


billy Dog

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

 A reference value can be passed as an argument.


 If you pass a referecne, then the parameter is the
local variable that points to the object
 The local variable disappears when the method
exits, but the object does not.

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

2
Array Object Diagram
Name Type Value
dogs Dog[]

0
1
2

 The new object is now pointed to by an entry in the array


 The value of the array item is a reference to the object

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

Design Steps for Re-useable Code


There are 3 steps:
 Get the pieces – words in the specification
– Class, object, attribute, function,
procedure
– Goal – what do we need to do?
 Connect the pieces
– Create a representation of the system
– Use Data Flow and Control Flow to
uncover relationships
 Convert into OO code
– Identify classes
– Place code in the correct class
– Use design rules and refactoring

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

Goals for Good Programming

 Design for reuse


 Write modular code
– Small methods encapsulated
by classes
 Take small steps
– Compile & test often
– This reduces the need for
debugging code
 Evaluate and refactor the code

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

We will focus on System Design and Code Design


INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

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)

But how do we know what behaviour a class has?

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

Functions and Procedures

 A function calculates a value and changes nothing.

 A procedure changes one or more attribute values.

 A method can be either a function or a procedure – not both!


A method does one single thing.

Function Procedure Method

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

The “Of” Rule


The word “of” gives you the feature and the class
Examples:
 Height of a Box
– Attribute height class Box

 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

 What class would we create?


 What methods would it have?
 What attributes would it need?
 What parameters would the constructor have?

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

 What accessors could we have for Rectangle?

 What mutators could we have?

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

Now we can use the mutators in the alternate constructor

public Rectangle(double length, double width)


{
// initialise instance variables
setLength(length);
setWidth(width);
}

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

Rectangle

We have 2 ways to create a Rectangle object

 Default constructor:

Rectangle rectangle = new Rectangle();


rectangle.setLength(5.0);
rectangle.setWidth(2.0);

 Alternate constructor:

Rectangle rectangle = new Rectangle(5.0, 2.0);

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

 This removes the need to use

Integer.toString(height); or
Double.toString(height);

 Note: int & double can be easily converted to a String


by using
– String result = “”+height;
– Where height is an int or a double

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

The Rectangle constructors are an example of overloading


 Default constructor –

public Rectangle()
{
}

 Alternate Constructor –

public Rectangle(double length, double width)


{
// initialise instance variables
setLength(length);
setWidth(width);
}

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

8
Another Overloading Example

Consider a Calculator class:

 add()
– This method would use attributes set in the constructor

 add(double number1, double number2)


– This method would use arguments passed to the method

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

• Hiding the implementation


(and data) from the client
Supports (calling class)
• Exporting just the class
reuse by behaviour (as few methods
as possible) for client
classes to use

 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

Local variables support method encapsulation; if a value is used


only inside a method, make it local, don't make it an attribute.

 A local variable is declared inside a method.


 It is temporary; it exists only inside the block where it is
declared.
 The scope of a variable is the area where it can be seen and
used.
 The scope may be less than a method eg a variable may only
exist inside a loop
 The scope of a local is the block where it is declared.

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

Parameters

A parameter is a local variable; its scope is the method.

 It is created when the method is called, exists while the


method is executing, and disappears when the method exits.
 The method call supplies one value for each parameter; the
value is called an argument.

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

Rule: Never Repeat Code

Code should never be duplicated


– Create a re-useable method

 Repeated code causes


– Integrity issues
– Maintenance issues
– BUGS

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

Static Attributes

Static means does not change per object (instance of a class)


A Static attribute can be called without an object

public static double PI = 3.141592654;

PI is a static constant defined in the Math class (in Java API)

– Math.PI is an example of a static attribute, we call the Class


then the attribute name, we do not need an object of the
Math class

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

11
Static Methods

A Static method does not use any data that belongs to an


object.

public static double pow(double a, double b) {…}

 Math.pow() is an example of a static method, we call the Class


then the method name, we do not need an object of the Math
class

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

Rule: Minimal Use of Static

Static members break class encapsulation

 A static attribute or method is visible


within the whole system.
 Static attributes and methods are almost
always a bad idea.
 Very few Java library classes have a static
member (attribute or method) because it
is a bad idea
 Only use static if there is no other
reasonable choice.

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

Rule: Few Public Mutators

Public mutators like:

public setLength(double length);

Should be used sparingly

 They allow other classes to change


the data inside this class.
 Its like making the attribute public.

INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

12
public static void main()

public static void main(String[] args)

is the entry point for an application (executable).

 If you need it, then make it the only static member in the
whole system (if you can).

 We will not use this until we start GUI

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

You might also like