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

Lecture5

This lecture covers design notations in applications programming, focusing on good object-oriented programming practices such as code layout, boolean functions, and class organization. It emphasizes the importance of readability through proper spacing, indentation, and avoiding unnecessary comparisons. Additionally, it introduces concepts like group classes, list lookup patterns, and client charts to illustrate object interactions and relationships.

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

Lecture5

This lecture covers design notations in applications programming, focusing on good object-oriented programming practices such as code layout, boolean functions, and class organization. It emphasizes the importance of readability through proper spacing, indentation, and avoiding unnecessary comparisons. Additionally, it introduces concepts like group classes, list lookup patterns, and client charts to illustrate object interactions and relationships.

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/ 9

Information Technology

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:

 Follow good OOP practices including code layout and


alignment
 Describe and use list lookup and group classes
 Draw a client chart to represent object interactions

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

Boolean Functions

A boolean variable can only be true or false; a boolean function


returns a boolean value.

 There is no need to test if true equals true or false


equals false:
 boolean found = true;
 if (found == true) Wrong!
– The condition is true if found is true, otherwise false.
 if (found) Right!
– The condition is true if found is true, otherwise false.
 This code is shorter and easier to write and read.
 Use if (!found) instead of if (found == false)

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

Indenting shows Flow of Control

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

Accessors & Mutators

toString()
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

A Group Class

A common pattern is to store the list in its own 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

List Lookup Pattern


A common pattern is to give each object an id. To find an object
in the list, we can scan each object until we find one that
matches.

 The supplier offers a boolean function


public boolean matches(int id)
{ return this.id == id; }

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

Lists Code Example – see handout

 Animals class has a list of Animal objects


that is, Cow objects.

private LinkedList<Animal> animalList;

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

Example of a List
Remember .. Variables defined as final cannot be changed

 The Cow class has final private constants – lines 27 & 28

final private String SOUND = "moo";


final private String ANIMAL = "Cow";

The Cow class inherits from Animal and effects (provide


implementation for) noise() the abstract method in the parent class.
– Abstract method noise() in Animal class – line ??
– Cow inherits from Animal – line ??
– Cow default constructor – line ??
– Cow alternate constructor – line ??
– Cow effects noise() – line ??

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

Example of a List

 Animals class uses java classes – line ??


import java.util.LinkedList;
import java.util.ListIterator;

 Attribute animalList is of type LinkedList where the objects have


been declared as Animal (so we do not need to cast) – line ??

private LinkedList<Animal> animalList;

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

4
Example of a List
There are 5 elements in the list

 The animalList attribute is created and populated in


the Animals default constructor – lines ??

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

What if I did this?


Animal animal = animalList.get(5);
What would happen?

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

 This will compile, but when we run the application…


 The runtime will throw an exception:
java.lang.IndexOutOfBoundsException: Index: 5, Size: 5

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

You cannot call a method on a null object

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

Null Object

When you declare a reference variable, the default value is


null.
 Example:
– private Rectangle rectangle;
– None of the attributes have been set, because the
constructor has not been called, ie the object has not been
created.
 If we try to call rectangle.area();, we will get a
NullPointerException

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

Runtime Errors
These are the two most common runtime errors you will
encounter:

• you are trying to


reference an index that
IndexOutOfBoundsException does not exist

• you are trying to call a


method (or attribute)
NullPointerException on an object that does
not exist ie a null object

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>

 It is used to test assumptions. For example, if you write a


method to calculate the WAM of a student, you would
assume that the WAM will be less than 100, we write:

assert wam < 100;

 If the wam is greater than 100 the assertion will fail


 Assertion checking is usually turned on during
implementation and turned off during testing and at release
time.
 A failed assertion will generate an AssertionError Exception
INSEARCH CRICOS provider code: 00859D I UTS CRICOS provider code: 00099F

Pre & Post Conditions

Precondition at the start of the method


Post condition at the end of the method

 An assertion placed at the beginning of a method is a pre


condition. It checks an assumption at the start of the method.
 An assertion placed at the bottom of a method body is a post
condition. It checks what is true after the method’s code has
been executed
 Assertions are disabled in BlueJ, you can enable them by
changing the default property in BlueJ. See
http://lists.bluej.org/pipermail/bluej-discuss/2006-
January/005217.html

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.

 A class is represented by an oval with a label.


 The root class is shown at far left.
 A link arrow points from left to right, from the client to the
supplier class.
 A supplier class supplies methods that are used or called by its
client.

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

7
Client Chart Example

Game Dice

– A link is created when a class name is mentioned in the


client code.
– A link between two classes is shown only once, but a class
may be shown many times; this creates a tree structure
from the root node.
– This convention means that an oval has only one input link;
there are no joins

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

Client Chart

A collection (such as an array or list) is shown by writing []


above the link; this symbol indicates that a client class (at left)
uses many objects of the supplier class (at right).

[]
Game Player

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

OO Design Steps

Create Group Identify


classes Objects
• Create classes,
• Implement list
attributes &
lookup pattern
methods

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

 Alignment shows flow of control

 Good spacing helps readability

 Egyptian bracketing is not easy to read

 Class order should be followed

 Group classes & List Lookup patterns


should be used

 Client chart show object relationships

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

You might also like