Académique Documents
Professionnel Documents
Culture Documents
Programming Principles II
Spring 2014
Instructor: Wenjia Li
01/07/16
Contact Information
Who am I?
Wenjia Li
Office hours:
Where?
CIT 2307
When?
Tu & Th 130 PM to 2 PM and Wed 2 PM to 4 PM
AND by appointment
01/07/16
Object-oriented
Language
Procedural languages (such as C, Pascal,
BASIC, Ada, and COBOL) are composed of
procedures (or functions, or methods). Each
procedure deals with a dedicated task.
Data and operations on data are separate.
Assemble car
NOWADAYS
What is An Object?
An object represents a distinct entity in
the real world
E.g., a car, a student, a bank account
Example of Objects
Data field 1
Capital
State
Data field n
State
Term
Method 1
Interest_Rate
Compute_Balance
Behavio
r
Behavio
r
Method m
An
Object
A bank
account
7
Creating Objects
Remember that a class is like a data type:
one can create variables of the class (namely
objects of the class) using the same way as
they declare variables of other data types:
Declaration:
class_name object_name;
Creation
object_name = new class_name( );
Object Assignment
Like arrays, the assignment of objects is
essentially making references.
For primitive data variables, the assignment is copying
values (instead of making references).
In following example, the old a1 becomes garbage
after assignment; Java reclaims its memory
automatically.
Account a1,a2;
a1 = a2;
a1
a2
Before
assignme
Capital =100;
Rate=0.02
Capital =2000;
Rate=0.04
a1
a2
After
assignme
10
nt
Accessing Class
Members
Recall the way we accessed library
methods and data:
JOptionPane.showInputDialog( );
Math.random( );
Math.PI;
An Example
Constructors
We would like to create accounts somehow
more automatically.
automatically
Can we create an account by also specifying
Capital, Rate, and Term when we declare an
object?
Constructors (Cont.)
We can have more than one constructors
in a class.
Constructors are thus overloaded methods.
Default Constructor
Observation: A class may be
declared without constructors.
In this case, a no-arg constructor
with an empty body is implicitly
declared in the class.
Called default constructor
It is provided automatically only if no
constructors are explicitly declared in
the class.
01/07/16
16
Array of Objects
Like creating/using an array of primitive
data variables, one can create an array
of objects (class variables).
Declaration & memory allocation
Account[] array = new Account[900];
17
Passing Objects to
Methods
Like passing array, passing objects to
a method is passing by reference.
What is needed to pass is the name
of the object. (just like passing arrays
and variables of primitive data
types).
21
22
23
01/07/16
24
Calling Overloaded
Constructor
public Circle() {
this(1.0);
}
25
26
Composition
01/07/16
27
Code Reuse
Effective software development relies on
reusing existing code.
Code reuse must be more than just
copying code and changing it which is
often the case with procedural languages
like C.
Goal with OOP languages: reuse
classes without changing the code within
the class
one OOP technique for code reuse is known
as composition.
01/07/16
28
A Simple Database
Your favorite boss wishes to implement a simple database of
Motorcycles they currently have on the dealerships lot.
Their application has only a few requirements...
They would like record the VIN, Color, Date of Arrival, and
Date of Sale each Motorcycle that has entered and left the
dealership. A report is required that prints all information for
each Motorcycle. Motorcycles must be comparable to avoid
duplicate entries in the database. For ease of data entry, it
must be possible to make a copy of an existing Motorcycle.
Your contribution to this project is to design and implement
a class named Motorcycle that will represent a single
Motorcycle.
01/07/16
29
30
01/07/16
31
32
33
34
A Motorcycle Class
Constructor
01/07/16
35
36
Composition
Note that the Motorcycle class contains two
class type instance variables (String and Dates).
37
Composition
Considerations
01/07/16
38
Designing a Motorcycles
Class: The equals Method
The definition of equals for the class Motorcycle
includes an invocation of equals for the class
String, and an invocation of the method equals
for the class Date.
The Motorcycle class passes responsibility for
determining equality to the String and Date
classes invoking their equals methods.
This is an important example of code reuse arising from
the use of composition to implement Motorcycle.
39
Designing a Motorcycles
Class: The equals Method
01/07/16
40
01/07/16
41
42
01/07/16
43
01/07/16
44
45
46
47
01/07/16
48
01/07/16
49
50
01/07/16
51
01/07/16
52
But What If
the user really wants to change the
array within the class?
The user shouldnt know that the class
uses an array.
The array must represent some abstract
data element in the class (e.g. student
grades).
Provide a method that changes the
abstract data element without revealing
the existence of an array.
01/07/16
53
Remember
01/07/16
54