CS计算机代考程序代写 python compiler Java week 01b

week 01b

COMP 2511
Object Oriented Design &

Programming

Introduction to Java Platform

© Aarthi Natarajan, 2018

Java is Platform Independent

Windows

JRE

Windows

JRE

Solaris

JRE

© Aarthi Natarajan, 2018

The Java Platform

Object Oriented

Distributed

Simple

Platform-
Independent

Multi-threaded

Secure

JAVA

Java Code
(.java)

Java Byte
Code
(.class)

Java compiler

Memory
Management

© Aarthi Natarajan, 2018

Resources
• Java SE Downloads:

https://www.oracle.com/technetwork/java/javase/downloads/index.html

• Java Tutorials: https://docs.oracle.com/javase/tutorial/

• Java SE 8 API Documentation:
https://docs.oracle.com/javase/8/docs/api/

• Eclipse (Eclipse IDE 2019-03) Downloads:
https://www.eclipse.org/downloads/

• Jenkov Java Tutorials:
http://tutorials.jenkov.com/java/index.html

© Aarthi Natarajan, 2018

Java Language Basics

© Aarthi Natarajan, 2018

Lecture Demo
This week, we will look at:
• Java Language Basics

₋ How to create a simple Java class
₋ Structure of a Java class
₋ Look at the mainmethod
₋ Creating primitive variables
₋ Use control-loop structures if-else, switch
₋ Iterate with loops
₋ Create arrays
₋ How to create comments
₋ Working with Strings

• Use package and import statements

• Create Java classes, object instances, constructors, methods

© Aarthi Natarajan, 2018

Coming up …
Thinking Objects
• Classes and Objects
• Abstraction
• Encapsulation
• Inheritance

© Aarthi Natarajan, 2018

Thinking Objects

© Aarthi Natarajan, 2018

What are objects?
• Objects are real-world entities :

₋ Something tangible and visible e.g., your car, phone,
apple or pet or

₋ Something intangible (you can’t touch) e.g., account,
time

• Objects have state (characteristics or attributes) and
behaviour (methods – what the object can do) e.g.,
₋ a car has state (colour, model, speed, fuel etc.) and
behaviour (start, change gear, brake, refuel)

₋ a dog has state (colour, breed, age, gender) and
behaviour (bark, eat, run)

• Each object encapsulates some state (the currently
assigned values for its attributes)© Aarthi Natarajan, 2018

Object Oriented Design
• Identify your domain
• Identify objects

withdraw
(behaviour)

deposit
(behaviour)

checkBalance
(behaviour)

attributes { account No, balance, interest }

An Account software
object

© Aarthi Natarajan, 2018

Object collaboration
• Objects interact and communicate by sending messages to

each other
• If object A wants object B to perform one of its methods, it

sends a message to B requesting that behaviour

deposit
(behaviour)

Account object

attributes { customerName, address }

Customer object

attributes { account No, balance, interest }

© Aarthi Natarajan, 2018

Object Collaboration
This message is typically made of three parts:
• The object to whom the message is addressed (e.g.,
John’s “Account” object)

• The method you want to invoke on the object(e.g.,
deposit())

• Any additional information needed (e.g., amount to be
deposited)

deposit(400)

Account Customer A
© Aarthi Natarajan, 2018

Objects and Classes

• Many objects are of the same “kind” but have different identity
e.g., there are many Account objects belonging to different
customers, but they all share the same attributes and methods

• “Logically group” objects that share some common properties and
behavior into a class (a blue-print for this logical group of objects)

John’s account
{ accountNo = 123, balance = 100, interest=5% }

Tom’s account
{ accountNo = 567, balance = 600, interest=5.2% }

© Aarthi Natarajan, 2018

– Defining a class, does not actually create an object
• An object is instantiated from a class and the object is said

to be an instance of the class
– An object instance is a specific realization of the class

• Two object instances from the same class share the same
attributes and methods, but have their own object identity
and are independent of each other
– An object has state but a class doesn’t
– Two object instances from the same class share the
same attributes and methods, but have their own
object identity and are independent of each other

Objects and classes

© Aarthi Natarajan, 2018

• A class is sometimes referred to as an object’s type
• An object instance is a specific realization of the class
• create an instance of the Account class as follows

Creating object instances

a1:Account
a1 == a2 —–> True or False?

Memorya1

a2

a2:Account

© Aarthi Natarajan, 2018

You should know …
• How to creating a java class

₋ How to create a class
₋ How to create an object instance
₋ Object data types
₋ Using constructors to initialise values of instance variables
₋ Accessing instance methods and variables
₋ Use of static variables and methods

© Aarthi Natarajan, 2018

• A special method that creates an object instance and assigns
values (initialisation) to the attributes (instance variables)

• Constructors eliminate default values
• When you create a class without a constructor, Python

automatically creates a default “no-arg” constructor for you

Constructor & Instance Variables

Default values
with a “no-arg”
constructor

0

balance 0

Values with an
explicit
constructor

1089023

100

accountNo

NULL Johnname

© Aarthi Natarajan, 2018

Instance Methods
• Similar to instance variables, methods defined inside a class are

known as instance methods
• Methods define what an object can do (behaviour)

© Aarthi Natarajan, 2018

Object References

a1:Account

name: joe
balance: 200

a1 == a2 —–> True or False?

Consider,
a1 = a2
a1 == a2 —–> True or False?

Memorya1

a2

a2:Account

name: sam
balance: 300

© Aarthi Natarajan, 2018

UML stands for Unified Modelling Language (http://www.uml.org/)

Programming languages not abstract enough for OO design

An open source, graphical language to model software solutions,

application structures, system behaviour and business processes

Several uses:

• As a design that communicates aspects of your system

• As a software blue print

• Sometimes, used for auto code-generation

What is UML?

© Aarthi Natarajan, 2018

UML Diagram Types

© Aarthi Natarajan, 2018

Account
-name: String
-balance: float

+getBalance(): float
+getName() : String
+withDraw(float)
+deposit(float)

a1:Account
name = “John Smith”
balance = 40000

a2:Account
name = “Joe Bloggs”
balance = 50000

class ( class diagram )

object instances (object
diagram)

Representing classes in UML

© Aarthi Natarajan, 2018

You should know …
• How to create a java class

₋ Use access modifiers in Java
₋ Define encapsulation
₋ Define inheritance
₋ Invoke super constructors

© Aarthi Natarajan, 2018

• Abstraction
• Encapsulation
• Inheritance

Key principles of OO

© Aarthi Natarajan, 2018

• Helps you to focus on the common properties and behaviours of
objects

• Good abstraction help us to accurately represent the knowledge
we gather about the problem domain (discard anything
unimportant or irrelevant )

• What comes to your mind when we think of a “car” ?
Do you create a class for each brand ( BMW, Audi, Chevrolet…)
?
• write one class called Car

and abstract;
• focus on the common essential qualities of the object
• focus on the application domain

• What if a specific brand had a special property or behaviour?
Later on….inheritance

Abstraction

© Aarthi Natarajan, 2018

Encapsulation
• An OO design concept that emphasises hiding the

implementation
• When you drive a car, do you ever worry how a steering-

wheel makes a right-turn or a left-turn?
• You are only concerned with the function of the steering

wheel
• Encapsulation leads to abstraction

© Aarthi Natarajan, 2018

• Encapsulation of object state implies hiding the object’s
attributes

• An object’s attributes represent its individual characteristics or
properties, so access to the object’s data must be restricted
– Methods provide explicit access to the object
e.g. use of getter and setter methods to access or modify the
fields

Encapsulating Object State

Class Name
– attribute1: int
– attribute2: boolean

+ operation_1(): void
+ operation_2(): int
+ operation_3(): boolean

UML notation for a Class

Account
-name: String
-accountNo: int
-balance: float

+getBalance(): float
+getAccountNo():float
+setBalance(float)
+setAccountNo(float)
+deposit(float)

Private
Attributes

Public
Methods

© Aarthi Natarajan, 2018

1. Encapsulation ensures that an object’s state is in a consistent state
2. Encapsulation increases usability

• Keeping the data private and exposing the object only through its
interface (public methods) provides a clear view of the role of the
object and increases usability

• Clear contract between the invoker and the provider, where the client
agrees to invoke an object’s method adhering to the method
signature and provider guarantees consistent behaviour of the
method invoked (if the client invoked the method correctly)

3. Encapsulation abstracts the implementation, reduces the
dependencies so that a change to a class does not cause a rippling
effect on the system

Why is encapsulation important?

© Aarthi Natarajan, 2018

• So far,
– we have defined classes and object instances
– objects have attributes and responsibilities

• let us now look at relationships between objects e.g.,
– a dog is-amammal
– an instructor teaches a student
– a university enrols students

• Relationships between objects can be broadly classified as:
– Inheritance
– Association

© Aarthi Natarajan, 2018

• So far, we have logically grouped objects with common
characteristics into a class, but what if these objects had some
special features?

e.g., if we wanted to store that sports car has spoilers
• Answer is inheritance – models a relationship between classes in

which one class represents a more general concept (parent or
base class) and another a more specialised class (sub-class)

• Inheritance models a “is-a” type of relationship e.g.,
– a savings account is a type of bank account
– a dog is-a type of pet
– a manager is-a type of employee
– a rectangle is-a type of 2D shape

Relationships (1) – Inheritance

© Aarthi Natarajan, 2018

• To implement inheritance, we
– create a new class (sub-class) , that inherits common properties and

behaviour from a base class (parent-class or super-class)
• We say the child class inherits/ is-derived from the parent class

– sub-class can extend the parent class by defining additional properties and
behaviour specific to the inherited group of objects

– sub-class can overridemethods in the parent class with their own specialised
behaviour

Child class – SavingsAccount
extends Account class adding its own
attributes and methods e.g.,
saver_interest & calc_interest()

Inheritance

Parent class – Account
class Account defines name,
accountNo, balance

Account
-name: String
-accountNo: int
-balance:float

SavingsAccount
-saver_interest: float

+calc_interest(): float

+get_balance(): float
+set_balance(): float

© Aarthi Natarajan, 2018

Shape
-name: String
-area: float

+getName(): String
+setName(String)
+getArea(): float

Rectangle
-height: float
-width: float

+getArea(): floatclass Rectangle overridesmethod
getArea() to provide its own
implementation

class Rectangle extends Shape adding
attributes height , width

Inheritance – another example

© Aarthi Natarajan, 2018

Next …
• Relationships between classes (association ,
composition, aggregation)

• Creating a domain model applying object-
oriented design principles…

© Aarthi Natarajan, 2018