CS计算机代考程序代写 Java algorithm SWEN20003 Object Oriented Software Development

SWEN20003 Object Oriented Software Development

SWEN20003
Object Oriented Software Development

Modelling Classes and Relationships

April 21, 2021

Modelling Classes and Relationships SWEN20003 April 21, 2021 1 / 44

The Road So Far

OOP and Java Foundations

Classes and Objects

Abstraction
I Inheritance
I Abstract Classes
I Polymorphism
I Abstract Classes
I Interfaces

Software Tools

Modelling Classes and Relationships SWEN20003 April 21, 2021 2 / 44

Lecture Objectives

After this lecture you will be able to:

Identify classes and relationships for a given problem specification

Represent classes and relationships in Unified Modelling Language
(UML)

Develop simple Class Diagrams

In-class code found here

Modelling Classes and Relationships SWEN20003 April 21, 2021 3 / 44

https://repl.it/@OOSD/Lecture-12-UML

First Steps

Before writing Java code:

Design your system, then implement the system (write code) following
your design!

Modelling Classes and Relationships SWEN20003 April 21, 2021 4 / 44

Problem Statement

This game involves a number of characters, like the player, their allies, and
their enemies. Some of these characters walk around the world, and some
allow the player to interact with them.

There are also other objects in the game like keys, blocks, and power-ups,
some of which can be interacted with, some that can be collected/picked
up, and some that can be “used” after being picked up.

Some objects in the game have health and can “die”, while some are killed
or removed instantly. Other objects can attack and defend other
characters.

Modelling Classes and Relationships SWEN20003 April 21, 2021 5 / 44

Design Algorithm

Modelling Classes and Relationships SWEN20003 April 21, 2021 6 / 44

Identifying Classes – Noun Extraction

This game involves a number of characters, like the player, their allies, and
their enemies. Some of these characters walk around the world, and some
allow the player to interact with them.

There are also other objects in the game like keys, blocks, and power-ups,
some of which can be interacted with, some that can be collected/picked
up, and some that can be “used” after being picked up.

Some objects in the game have health and can “die”, while some are killed
or removed instantly. Other objects can attack and defend other
characters.

Modelling Classes and Relationships SWEN20003 April 21, 2021 7 / 44

Class Design

How would you design this system?

What relationships would the classes have, if any?

Would there be one overarching superclass, or many?

How do we represent our design?

Modelling Classes and Relationships SWEN20003 April 21, 2021 8 / 44

Introduction to UML

Keyword

Unified Modeling Language (UML): a graphical modelling language that
can be used to represent object oriented analysis, design and
implementation.

What you are learning is class modelling; UML is only one
notation/tool!

Modelling Classes and Relationships SWEN20003 April 21, 2021 9 / 44

Representing a Class in UML

How would you represent the superclass of the game, GameObject?

How would you represent the class?

How would you represent its attributes?

How would you represent it methods?

Modelling Classes and Relationships SWEN20003 April 21, 2021 10 / 44

Representing A Class

Modelling Classes and Relationships SWEN20003 April 21, 2021 11 / 44

Representing Class Attributes

Components of an attribute:

Name (e.g. xPos)

Data Type (e.g. : int)

Initial Value (e.g. = 0)

Privacy (e.g. +)

Multiplicity

Finite [10] (array)
Range (Known) [1..10] (array or list, etc.)

Range (Unknown) [1..*] (list, etc.)
Range (Zero or More) [*] (list, etc.)

Static (e.g. numMethodCalls : int)

Modelling Classes and Relationships SWEN20003 April 21, 2021 12 / 44

Representing Class Attributes
Representing access control:

+ Public

∼ Package
# Protected

− Private

Modelling Classes and Relationships SWEN20003 April 21, 2021 13 / 44

Methods
Components of a method:

Name (e.g. render())

Privacy (e.g. +)

Return type (e.g. : void)

Parameters (e.g. name : String)

Modelling Classes and Relationships SWEN20003 April 21, 2021 14 / 44

Assess Yourself

Implement the GameObject class

Modelling Classes and Relationships SWEN20003 April 21, 2021 15 / 44

Assess Yourself

public class GameObject {

private int xPos = 0;

private int yPos = 0;

private String[] imageFiles;

private static int numObjects;

public void render() {

//block of code to execute;

}

public boolean render(int imageIndex) {

//block of code to execute

}

public static int getNumObjects() {

// block of code to execute;

}

}

Modelling Classes and Relationships SWEN20003 April 21, 2021 16 / 44

Representing Class Relationships

Classes may relate to other classes through different types of relationships.

Following are the four common types of relationships between classes:

Association

Generalization (Inheritance)

Realization (Interfaces)

Dependency

Let’s look at how to represent these relationships in UML.

Modelling Classes and Relationships SWEN20003 April 21, 2021 17 / 44

Association

Represents a has-a (containment) relationship between objects.

Allows objects to delegate tasks to other objects.

Shown by a solid line between classes.

During class refinement the design team decides to represent the position
of the GameObject with a Position class (instead of coordinates).

How might the class relationship be represented?

Modelling Classes and Relationships SWEN20003 April 21, 2021 18 / 44

Association

When one class is contained by another, we always use an association.

Keyword

Association: A link indicating one class contains an attribute that is itself
a class. Does not mean one class “uses” another (in a method, or
otherwise).

Modelling Classes and Relationships SWEN20003 April 21, 2021 19 / 44

Properties of Association

Association links can represent more information:

Name

Role labels

Directionality
I Unidirectional (hierarchical, ownership)
I Bidirectional (co-operation)

Multiplicity

Type
I Plain association
I Aggregation
I Composition

Modelling Classes and Relationships SWEN20003 April 21, 2021 20 / 44

Properties of Association – Name and Role

Modelling Classes and Relationships SWEN20003 April 21, 2021 21 / 44

Properties of Association – Direction

Modelling Classes and Relationships SWEN20003 April 21, 2021 22 / 44

Properties of Association – Multiplicity

Keyword

Multiplicity: specifies the number of links that can exist between
instances (objects) of the associated classes.

Indicates how many objects of one class relate to one object of
another class

Can be a single number or a range of numbers

We can add multiplicity on either end of class relationship by simply
indicating it next to the class

Modelling Classes and Relationships SWEN20003 April 21, 2021 23 / 44

Properties of Association – Multiplicity

One class can be related to another in the following ways:
I One-to-one
I One-to-many
I One-to-one or more
I One-to-zero or one
I One-to-a bounded interval (one-to-two through twenty)
I One-to-exactly n
I One-to-a set of choices (one-to-five or eight)

Multiplicity can be expressed as:
I Exactly one – 1
I Zero or one – 0..1
I Many – 0..* or *
I One or more – 1..*
I Exact Number – e.g. 3..4 or 6
I Or a complex relationship – e.g. 0..1, 3..4, 6..* would mean any number of

objects other than 2 or 5

Modelling Classes and Relationships SWEN20003 April 21, 2021 24 / 44

Assess Yourself

Create a UML representation for the following scenario:

A Student can take up to five Courses

A Student has to be enrolled in at least one Course

A Course can contain up to 400 Students

A Course should have at least 10 Students

Modelling Classes and Relationships SWEN20003 April 21, 2021 25 / 44

Self-Association

Each Student has a student representative they can contact, who is also a
student

Modelling Classes and Relationships SWEN20003 April 21, 2021 26 / 44

Multiple Associations

Does the following class relationship enforce a single student rep
per-subject?

How about a single student rep per-subject, per-student?

Modelling Classes and Relationships SWEN20003 April 21, 2021 27 / 44

Association – Type (Aggregation)

Different form of association, where one class “has” another class, but
both exist independently

If a GameObject is destroyed, the Position object disappears;
dependence

If the Pond object is destroyed, the Duck lives on; independence

Makes sense; a Duck can find another Pond

Modelling Classes and Relationships SWEN20003 April 21, 2021 28 / 44

Association – Type (Composition)

Different form of association, indicating one class cannot exist without the
other; in other words, existing on its own doesn’t make sense

A Department is entirely dependent on a University to exist

If the University disappears, it makes no sense for a Department
to exist

But a Professor is just a person; they can find another University!

Modelling Classes and Relationships SWEN20003 April 21, 2021 29 / 44

Generalization – Inheritance

Modelling Classes and Relationships SWEN20003 April 21, 2021 30 / 44

Abstract Classes

Italicised methods or classes are abstract.

Modelling Classes and Relationships SWEN20003 April 21, 2021 31 / 44

Assess Yourself

What next? Now we have:

GameObject

Position

Character, which inherits from GameObject

How could we add behaviour?

Modelling Classes and Relationships SWEN20003 April 21, 2021 32 / 44

Assess Yourself

Some design help…

Only the Player and Enemy can move. The Guardian and the Enemy can
attack each other, and the Enemy can also attack the Player. Both
Characters can also target the player.

Modelling Classes and Relationships SWEN20003 April 21, 2021 33 / 44

Realization – Interfaces

Modelling Classes and Relationships SWEN20003 April 21, 2021 34 / 44

Dependency

Dependency represents a weak relationship between classes; dependency
implies that a change to one class may impact the other class. It is
represented by a dotted arrow.

For example, when a method in a class has another class as one of the
input parameters, this results in a dependency relationship.

Modelling Classes and Relationships SWEN20003 April 21, 2021 35 / 44

Assess Yourself

Construct a UML diagram for the following description:

A school has one or more departments.

A department offers one or more subjects.

A particular subject will be offered by only one department.

A department has instructors and instructors can work for one or more
departments.

A student can enrol in up to 5 subjects in a school.

Instructors can teach up to 3 subjects.

The same subject can be taught by different instructors.

Students can be enrolled in more than one school.

Modelling Classes and Relationships SWEN20003 April 21, 2021 36 / 44

Assess Yourself

Modelling Classes and Relationships SWEN20003 April 21, 2021 37 / 44

Assess Yourself

How could your Project 1 submission be better designed?

Project answers don’t come that easily…

Modelling Classes and Relationships SWEN20003 April 21, 2021 38 / 44

Is modelling useful?

Modelling Classes and Relationships SWEN20003 April 21, 2021 39 / 44

Is Modelling Useful?

Modelling Classes and Relationships SWEN20003 April 21, 2021 40 / 44

Is Modelling Useful?

Modelling Classes and Relationships SWEN20003 April 21, 2021 41 / 44

UML Drawing Tools

draw.io

LucidChart (Sign up with your student email for full functionality)

StarUML

IntelliJ with plugins/integrations (useful for converting UML to code
instantly)

And many more…

draw.io is apparently the tool of choice for SWEN30006, but the choice is
yours.

Modelling Classes and Relationships SWEN20003 April 21, 2021 42 / 44

https://www.draw.io/
https://www.lucidchart.com/
https://www.lucidchart.com/pages/usecase/education-request
http://staruml.io/

Assess Yourself

Finish implementing the class diagram for the game example.

Hint: Good practice for project 2!

Modelling Classes and Relationships SWEN20003 April 21, 2021 43 / 44

Metrics

A world class chef is working to develop a robotic assistant, and they’ve hired you
to build a simulated kitchen to test it out.

The robot needs to be able to:

Use normal human tools and utensils (oven, fork)

Open things (cupboards and containers)

Prepare ingredients (cutting, dicing, boiling)

Be operated by a human (for safety reasons)

Create a class diagram for this scenario, including any inheritance or realisation

(interface) relationships.

Modelling Classes and Relationships SWEN20003 April 21, 2021 44 / 44