CS计算机代考程序代写 Java algorithm case study 300144 300888 Object Oriented Analysis

300144 300888 Object Oriented Analysis
Dr Mahsa Razavi

Lecture 2 Class & Object
References
Textbook Chapter 1 Bhuvan’s Book Chapter 1 Booch’s book Chapter 2, 3
2

Things in Last Lecture
• Positioning Object Oriented Analysis – Phases of software development
• Object Oriented Paradigm
– Procedural programming paradigm – Object oriented paradigm
– Methods for OOA
• About UML
– A brief introduction to UML – UML 2.0 diagrams
3

Main Topics of Lecture
• Objects and classes
• Fundamentals of Object–Orientation

– Classification – Abstraction
– Inheritance
– Encapsulation – Polymorphism
Case study
4

Sub-Module
▪ Objects and classes
▪ Fundamentals of Object–Orientation
▪ Classification ▪ Abstraction
▪ Encapsulation ▪ Inheritance
▪ Polymorphism
▪ Case study
5

Classes and Objects
A “CLOCK” in general is a CLASS.
Your CLOCK and
My CLOCK are specific Objects – instances of the Class.
6

Objects
Wikipedia definitions:
• Philosophy: “an object is anything that we can think or talk about”
• Computerscience
– from OOA/D point of view: “an object is an entity encapsulated with a set of attributes (data members) and behaviors (function members)”
– from OOP point of view: “an object is a location in memory having a value and referenced by an identifier”
• Attributes describe the object, and also describe the state in which it exists
• Behavior is manifested in changes of the object’s state
7

Objects
Wall clock
alarm clock
Electronic clock
red
gold
grey
8
Show time Reset time
Style
Color

Classes
• A Class is a DEFINITION, a TEMPLATE, for the Objects
• NOTE: A Class is NEITHER an Individual Object, NOR simply a Collection of Objects
• A Class represents a Whole Set of Objects Objects are instances of classes
9

Objects as instances of a class
• If you have two Student objects, they each have their own data
– E.g., Student A has a different set of values for its attributes from that of Student B
• But they both have the same set of methods
• A class defines the behavior of their associated
objects
– A class is an abstraction of a set of individually different objects that have the same behavior, and the same set of date fields (attributes).
10

One Example: Objects vs Classes
Object 1 Object 7
Object 6 Object 2 Object 4
Object 5
Class: Car
Object 3
11

OO v/s Non-OO (Similarities and Differences)
• Non-Object-oriented (Traditional) – Data and Procedures are kept Separate
– Procedures work on Data to produce results Data
Procedures
Subprograms
12
Data

OO v/s Non-OO, Similarities and Differences (Cont.)
• Object-oriented
– Focus on Responsibilities – to be satisfied by Objects
– Responsibilities translate into Data and Functions within an Object
Data
Procedures
13

Comparing the Mindsets
Procedural Mindset
• What is the system supposed to do?
• What are the functions of the system?
• How do I design and code to achieve the functions?
• Focus is on the algorithms.
OO Mindset
• What is the system made up of? Objects
• What are the responsibilities of these objects?
• How will these objects collaborate to satisfy the responsibilities?
• Algorithms/code deferred
14

Sub-Module
▪ Objects and classes
▪ Fundamentals of Object–Orientation
▪ Classification ▪ Abstraction
▪ Inheritance
▪ Encapsulation ▪ Polymorphism
▪ Case study
15

The Star of OO Fundamentals
CLASSIFICATION
Fundamentals of Object-Orientation: THE OBJECT
16

Classification
▪ Objects and classes
▪ Fundamentals of Object–Orientation
▪ Classification ▪ Abstraction
▪ Inheritance
▪ Encapsulation ▪ Polymorphism
▪ Case study
17

Classification (Cont.)
Lithops Lesliei
18

Classification (Cont.)
• Starting point of object analysis
• Group things that have a common structure or exhibit a
common behavior
• After classification, you get collections of objects, each with similar structure and behaviour.
Group things that have common structure and behavior.
Picture for Booch’s book
19

Classification (Cont.)
Lithops Lesliei
20

Abstraction
▪ Objects and classes
▪ Fundamentals of Object–Orientation
▪ Classification ▪ Abstraction
▪ Encapsulation ▪ Inheritance
▪ Polymorphism
▪ Case study
21

Abstraction (Cont.)
An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries , relative to the perspective of the viewer (system developer).
Abstraction focuses on the essential characteristics of an object, relative to the perspective of the viewer
22 Picture for Booch’s book

Abstraction (Cont.)
From Objects to Class- the 1st level of abstraction
Ignore UML notation but focus on the content
Cat
– name – color – sex
– age
+ setName()
+ getName()
+ eat()
+ makeNoice() …
23

Abstraction (Cont.)
From Class to Class- the 2nd level of abstraction
Can be Abstracted to
FROG
CAT
DOG
ANIMAL
CLOCK
Can be Abstracted to
Classes are abstracted to higher level classes
PICTURE
THING
HAT
24

Classification vs Abstraction
• Classification tells you what are the classes you have to define given a problem while abstraction tells you what are the characteristics you have to specify for each class.
– Starts by grouping a given set of (reality) objects based on certain characteristics, e.g., Marker Pens, Ballpoint Pens etc.
– From the classification (grouping), most common properties and behavior is abstracted to arrive at a Class definition
– Iterate the process until you reach a satisfied class definition
25

Classification vs Abstraction (Cont.)
Requirements from Problem Space Can be Classified & Abstracted to
Arrive at Good Classes
BOOK
The processes of classification and abstraction are mostly interactive
MAN
CHEST
SHOE
HAT
26

Sub-Module
▪ Objects and classes
▪ Fundamentals of Object–Orientation
▪ Classification ▪ Abstraction
▪ Inheritance
▪ Encapsulation ▪ Polymorphism
▪ Case study
27

Identifying more general groupings to create Inheritance Hierarchy
PLANT and ANIMAL are LIVING things
LIVING
PLANT
ANIMAL
EUCALYPTUS
FERN
FROG
CAT
28

Inheritance
• Inheritance represents IS-A relationship – An EUCALYPTUS is a plant
– A cat is an animal.
– “Who is this boy?” “He is a little John.”
means the boy is a son of John
• Child class (subclass) is derived from Parent class
(superclass)
– An object of the child class is an object of the parent class
• Child class inherits the attributes and methods of Parent class plus perhaps additional features
• Child class can change implementation of Parent class features (methods overriding)
29

Inheritance (Cont.)
• Inheritance enables significant code reuse since child classes gain access to the code defined in their ancestors.
This example was taken from Kenneth Anderson’s lecture notes
30

Inheritance (Cont.)
The generic behavior of animals or each species only needs to be implemented once
31

Inheritance (Cont.)
• An object of a child class does not HAVE TO inherit all the properties/behavior of its parent class (or classes). A subclass can override any method of its superclass and add extra methods.
Mobile phone
+ phonecall()
+ sendMessage() + alarm()
+ playGame()
Smart phone
+ browse()
+ email()
+ playGame()
32

Moving on the Inheritance Tree
Finding Superclasses Needs abstract thinking
Domain knowledge helpful Essentially FOR Reuse
Finding Subclasses Concrete lower level Implementation thinking EssentiallyWITH Reuse
Generalisation
Specialisation
33

Moving on the Inheritance Tree (Cont.)
Generalisation
Specialisation
34

abstract class Employee { private String fname; private String gname; private String staffID; Employee (String family, String given, String sid)
{
fname = family; gname = given; staffID = sid;
}
abstract double setSalary
class SalaryEmployee extends Employee {
private double salary; SalaryEmployee (String family, String given, String sid, double pay) {
super(family, given, sid);
// pass to Employee constructor
salary = setSalary(pay); }
double setSalary (double salary) {
return salary; }
…… }
…… }
A Case Study in Java
(double salary);
Object Oriented Analysis, 300144 35

Sub-Module
▪ Objects and classes
▪ Fundamentals of Object–Orientation
▪ Classification ▪ Abstraction
▪ Inheritance
▪ Encapsulation ▪ Polymorphism
▪ Case study
36

• •

Encapsulation
Classes can control the accessibility of the features of
their objects, typically specifying whether an attribute or method can be changed or accessed by other objects.
An attribute or a method of a class can be
– Public:anyobjectcanaccess
– Private:onlythemethodsintheclasscanaccess
– Protected:onlytheclassanditssubclassescanaccess
This ability to hide features of a class/module is referred to as encapsulation or information hiding;
– however, encapsulation is a topic that is broader than just data hiding.
37

Encapsulation (Cont.)
• Encapsulationletsyou
– hide data and algorithms in one class from the rest of your
application
– limit the ability for other parts of your code to access that information
– protect information in your objects from being used incorrectly
Object Oriented Analysis, 300144 38

Why is encapsulation important?
• The benefits of encapsulation
– A programmer can change part of the code of a system without
effecting other part of the code.
– The third parties of system development do not have to release their code
– In general, encapsulation helps increase system robustness and code reusability
Object Oriented Analysis, 300144 39

How does the access control work (C++)?
class Student { public:
int studentId; string name;
int courseCode; void display();
};
//implementation of display()
int main() {
Student s;
s.studentId = 1234567; s.name = “Dongmo Zhang”; s.courseCode = 3510; s.display();
return 0;
}
All ok
40

How does the access control work (Java)?
class Student {
int studentId; string name;
int courseCode; void display() {
class Registration {
public static void main(String [ ] args) {
// initialise an object Student s = new Student(); s.studentId = 1234567; s.name = “Dongmo Zhang”; s.courseCode = 3510; s.display();
}
All ok
……; }
}
41

More about the access control work
class Student { private:
int studentId; public:
string name;
int courseCode; void display()
int main() {
Student s;
s.studentId = 1234567; s.name = “Dongmo Zhang”; s.courseCode = 3510; s.display();
return 0;
}
{…}
//implementation of display()
};
All ok?
illegal
42

Sub-Module
▪ Objects and classes
▪ Fundamentals of Object–Orientation
▪ Classification ▪ Abstraction
▪ Inheritance
▪ Encapsulation ▪ Polymorphism
▪ Case study
43

Polymorphism
• Polymorphism literally means “many forms”
• OOA definition: “Being able to refer to different derivations of a class in the same way, but getting the behavior appropriate to the derived class being referred to”
It is very hard to fully understand polymorphism without knowing how a OO program is working. Let’s try
44


Suppose that you have three classes: Phone, PadPhone and TouchScreenPhone.
• Any phone can make a phone call therefore dial() is a method of Phone.
• The way of dialing with a pad phone or with a touch screen phone is different therefore at least one of the classes has to override function dial().
Polymorphism (Cont.)
Phone +dial()
PadPhone + dial()
TouchScreenPhone + dial()
45

Polymorphism (Cont.)
PadPhone p1 = new PadPhone(); TouchScreenPhone p2
= new TouchScreenPhone();
p1.dial(); p2.dial();
Obviously p1 and p2 will call their local definition of the method dial().
Phone + dial()
PadPhone +dial()
TouchScreenPhone + dial()
46

Polymorphism (Cont.)
• But suppose both p1 and p2 are declared as Phone: Phone p1, p2;
p1 = new PadPhone();
p2 = new TouchScreenPhone();
p1.dial(); p2.dial();
What version of dial( ) will be invoked?
Phone + dial()
PadPhone +dial()
TouchScreenPhone + dial()
47

Polymorphism (Cont.)
(move)
ANIMAL
(move)
On Receipt of Message
move from a Calling
Object, the appropriate
move – depending on whether it is a Frog or a Horse – will be executed
HORSE
(move)
FROG
(move)
Advantage? CALLING object need not know what is Moved, so, if a new CAT object is added, the CALLING class doesn’t change
CAT
(move)

Why is polymorphism so important?
• Polymorphism allows you to update a functionality
– Assume that you have implemented many classes built up on the generic of phone class (no matter whether it is a keypad phone or a smartphone). Now you invent a new type of phones without dial plate (say voice controlled phones) therefore the dial() is overridden. Polymorphism allows you to reuse these old classes without have to change the implementation of dial(). (only overriding the function in the new phone)
• Polymorphism allows you to use same interface for different types of objects (different operations).
49

class Polymorph {
public static void main(String[] args)
{
Polygon poly1, poly2, poly3; poly1 = new Rectangle(); poly2 = new Triangle();
poly3 = new Polygon(); poly1.set_values (4,5); poly2.set_values (4,5); poly3.set_values (4,5); System.out.print( poly1.area()
+“\n” + poly2.area()+ “\n“
+ poly3.area()+ “\n”); }
}
20
10 0
Optional
Case study: an example in Java
class Polygon {
int width, height;
void set_values (int a, int b) {
width=a; height=b; } int area () {
return 0; }
class Rectangle extends Polygon {
}
int area () {
return width * height; } }
class Triangle extends Polygon { int area () {
return (width * height / 2); }
}
How to
calculate
area?
Object Oriented Analysis, 300144
50
Output:

Module Outline (Lect2b)
• About the UML
– Origins and Evolution of the UML – Evolution of the UML
• The UML Today
– Visualizing, Specifying, Construction and Documenting
• SomeUMLbasedprojects
• Introduction to UML 2.0
• UML 2.0 diagrams and purpose

Conclusions
• A Class is a DEFINITION of a certain type of Objects
• Objects are instances of classes
• OOanalysisnormallystartswithclassificationof objects.
• Abstractiondiscoversthetheessentialcharacteristics of an object
• Inheritance reflects the “is-a” relationship between objects
• Encapsulation hides data via access control.
• Polymorphism allows one method with the same signature to have multiple definition.
52

Homework
• Review this lecture note
• Complete the specified readings
• Complete the project/tutorial of this week
• Prepare your answer for the 2nd tutorial exercises.
53