Section 8: ObjectBased Programming - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Section 8: ObjectBased Programming

Description:

int bMonth, int bDay, int bYear, int hMonth, int hDay, int hYear) ... birthDate = new Date(bDay,bMonth,bYear) ; hireDate = new Date(hDay, hMonth, hYear) ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 23
Provided by: PPS295
Category:

less

Transcript and Presenter's Notes

Title: Section 8: ObjectBased Programming


1
  • Section 8 Object-Based Programming
  • Objectives
  • To understand encapsulation and data hiding
  • To understand the theory behind abstract data
    types (ADTs)
  • To be able to create Java ADTs (Java classes)
  • To be able to create, use and destroy objects
  • To be able to define access controls
  • To be able to understand class variables and
    class methods

2
Introduction Object-Oriented Programming (OOP)
encapsulates data(attributes) and methods
(behaviour) into software components called
objects. Objects have the property of
information hiding - they communicate with one
another through well defined interfaces. Objects
do not have knowledge of each others
implementation. Maps onto reality its not
necessary to know details on how a car operates
to be able to drive it.
3
Using a Class to Define an ADT for
Time Attributes hour, minute and second
(represented as integers) Behaviour Simple
construction (0,0,0) Setting the time (setTime()
) Converting to 24H format (toUniversal()
) Converting to standard format (toStandard() )
4
import java.text.DecimalFormat public class
Time1 private int hour, minute, second
public Time1() setTime(0,0,0)
public void setTime(int h, int m, int s) if
( h gt 0 h lt 24 ) hour h
else hour 0 if ( m gt 0 m
lt 60 ) minute m else
minute 0 if ( s gt 0 s lt 60 )
second s else second 0

5
public String toUniversal()
DecimalFormat twoDigits new DecimalFormat("00")
return twoDigits.format(hour)""
twoDigits.format(minute)""
twoDigits.format(second) public String
toString() DecimalFormat twoDigits new
DecimalFormat("00") String result ""
if (hour 12 hour 0) result
12 else result hour 12
result "" result
twoDigits.format(minute) "" result
twoDigits.format(second) if (hour lt 12)
result " AM" else
result " PM" return result
6
import javax.swing. public class TimeTest
public static void main(String args)
Time1 t new Time1() String output
output"The initial 24H time is
"t.toUniversal()"\n" "The initial
standard time is " t.toString()
t.setTime(13,27,6) output"\n\nThe 24H
time now is "t.toUniversal()"\n"
"The standard time now is " t.toString()
t.setTime(99,99,99) output"\n\nThe
(invalid) 24H time is "t.toUniversal()
\nThe (invalid) standard time is "
t.toString() JOptionPane.showMessageDialo
g(null,output,"Testing Time",
JOptionPane.INFORMATION_MESSAGE)
System.exit(0)
7
(No Transcript)
8
Terminology A class is used to define
objects. An object is an instance of a
class. Variables defined within a class are
known as instance variables of the
object. Methods defined within a class are known
as instance methods.
9
Controlling Access to Elements of An Object The
access modifiers public and private are used to
control access to instance variables and
methods. The primary purpose of public methods
is to define the interface for instances of the
class. Private members of a class are not
accessible to users of this class (clients). publ
ic class TimeTest public static void
main(String args) Time1 t new Time1()
t.hour 7
10
Initialising Class Instances Constructors When
an object is created, its members can be
initialised by a constructor method. This
method will share the name of the defining
class. Instance variables can be left to their
default values, or may be initialised in a
constructor, or may be set later after the object
has been created. Constructors cannot specify
return types or return values. A class may
contain overloaded constructors to provide a
variety of ways to initialise instances of that
class.
11
Initialising Class Instances Constructors When
an object is created, any data required for
initialisation may be passed to the
constructor. If no constructors are defined for
a class, the compiler creates a default
constructor that takes no arguments. Its
possible for the programmer to provide such a
constructor. If any constructors are provided,
the compiler does not provide the default
constructor.
12
Using Overloaded Constructors public class
Time2 private int hour, minute, second
public Time2() setTime(0,0,0)
public Time2(int h) setTime(h,0,0)
public Time2(int h, int m) setTime(h,m,0)
public Time2(int h, int m, int s)
setTime(h,m,s) public Time2(Time2
time) setTime(time.hour,time.minute,time.sec
ond) // remainder of definition
13
Set and Get Methods Private instance variables
can be manipulated only by methods of
the class. Classes often provide public methods
to allow clients of the class to set values of,
or get values from instance variables. Providing
access through set and get methods can help
preserve the integrity of the data. Some set and
get methods may be added to the Time class.
This is illustrated in class Time3
14
// set methods // set the hour public void
setHour(int h) if (h gt 0 h lt 24)
hour h else hour 0 // set the
minute public void setMinute(int m) if (m gt 0
m lt 60) minute m else
minute 0
15
// set methods // set the second public void
setSecond(int s) if (s gt 0 s lt 60)
second s else second 0 // set
the time public void setTime(int h, int m, int
s) setHour(h) setMinute(m)
setSecond(s)
16
// get methods // get the hour public int
getHour() return hour // get the
minute public int getMinute() return minute
// get the second public int getSecond()
return second
17
Composition Objects as Instance Variables of
Other Classes A company employee may be
represented as follows
Employee
Class Name
firstName (String) lastName (String) birthDate
(Date) hireDate (Date)
Attributes
Construction() toString()
Methods
18
Composition Objects as Instance Variables of
Other Classes public class Employee private
String firstName, lastName private Date
birthDate, hireDate public Employee(String
fName, String lName, int
bMonth, int bDay, int bYear,
int hMonth, int hDay, int hYear) firstName
fName lastName lName birthDate
new Date(bDay,bMonth,bYear) hireDate
new Date(hDay, hMonth, hYear) public
String toString() return lastName ,
firstName Hired
hireDate.toString() Birthday
birthDate.toString()
19
Composition Objects as Instance Variables of
Other Classes May be tested as
follows Employee e new Employee(Bob,Jones,
24,7,55,1,1,88) System.out.println(e) Jones,
Bob Hired 01/01/1988 Birthday 24/07/1955
20
Static Class Members Each object of a class has
its own copy of all the instance variables of a
class. It is possible (and sometime very useful)
to share a piece of data between all objects of a
class. This is called a class variable and is
associated with the static keyword on variable
definition. public class Example private
static int count 0 public Example()
count System.out.println(Num
instancescount)
21
Using the this Reference public class
SimpleTime private int hour, minute, second
public SimpleTime(int hour, int minute, int
second) this.hour hour
this.minute minute this.second second

22
Final Instance variables Some instance variables
need to be modified, some do not. The keyword
final may be used to define a piece of data that
is not modifiable. private final int count 5
Such a data item is often referred to as a
constant.
Write a Comment
User Comments (0)
About PowerShow.com