Lecture5
Lecture5
Applications Programming
LECTURE 5
Design Notations
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Design Notations
Learning Objectives
At the end of the lecture, you should be able to:
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Boolean Functions
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
1
Good Spacing
Bad Good
String name=getName(); String name = getName();
area = depth*height; area = depth * height;
for(int i=0;i<2;i++) for (int i = 0; i < 2; i++)
public double sqrt ( int value) public double sqrt (int value)
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Bad Good
If... If …
… …
else if … • else if…
…. …
• else
else
…
…
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
No Egyptian Brackets
Bad Good
public void public void
method() { method()
//do {
something //do
} something
}
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
2
Class Code Order
Attributes
Methods listed in calling order
• If the method end() is used before play(), then it should
appear above play() in the class
toString()
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
A Group Class
Class Objects
{
private LinkedList<Object> objects = new
LinkedList<Object>();
public void method()
{
for (Object object: objects)
object.method();
}
}
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
The client scans the list and returns the matching object
private Object find(int id)
{ for (Object object: objects)
if (object.matches(id))
return object;
return null; }
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
3
Another example of a List
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Example of a List
Remember .. Variables defined as final cannot be changed
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Example of a List
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
4
Example of a List
There are 5 elements in the list
animalList.add(new Cow("Clarabelle"));
animalList.add(new Cow());
animalList.add(new Cow("Buttercup"));
animalList.add(new Cow("Daisy"));
animalList.add(new Cow("Meg"));
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Example of a List
Animals:noise() gets each element in the list and calls
noise() on each element.
ListIterator<Animal> li = animalList.listIterator();
while (li.hasNext())
{
System.out.println(li.next().noise());
}
Output:
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
IndexOutOfBoundsException
There are only 5 elements in the list
There are only indexes 0 to 4
animalList.get(5), will try to return the 6th object – but there is
no 6th object
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
5
What if I did this?
Delete
animalList = new LinkedList<Animal>();
From the Animals Constructor
What would happen?
NullPointerException
The datatype has been defined, but I have not created the
object for the attribute animalList
This will compile, but when I run the application…
The runtime will throw an exception:
java.lang.NullPointerException
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Null Object
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Runtime Errors
These are the two most common runtime errors you will
encounter:
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
6
Assertions
An assertion is either true or false
It has the form assert <boolean condition>
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Design Notation
A client chart shows how a class uses one or more other
classes; classes are connected by use links.
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
7
Client Chart Example
Game Dice
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
Client Chart
[]
Game Player
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
OO Design Steps
Find Common
Code
• Refactor for
parent class
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F
8
Key Concepts
Summary
Boolean functions should be used
without variables
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F