Escolar Documentos
Profissional Documentos
Cultura Documentos
Technique
Topic 10 Object Oriented Programming
Chapter Topics
14.1 Procedural and Object-Oriented Programming
14.2 Classes
14.3 Using the Unified Modeling Language to Design
Classes
14.4 Finding the Classes and Their Responsibilities in a
Problem
14.5 Inheritance
14.6 Polymorphism
Procedural Programming
Fields (private)
current second
current minute
current hour
alarm set time
alarm set
can access
Methods (private)
increment the current second
increment the current minute
increment the current hour
sound alarm
Methods (public)
OOP
Fundamentals
set time
set alarm time
turn alarm on
turn alarm off
can access
14.2 Classes
OOP
Fundamentals
14.2 Classes
Creating a class type
Class ClassName
//Field declarations and method declarations
Private DataType VarName
Public Module ModuleName(DataType ParmName)
// body of module
End Module
Public Function ReturnType FuncName(DataType ParmName)
// body of function with return statement
End Function
End Class
fields
and
methods
The first line starts with Class, followed by the name of the class
The programmer names the class following the same rules as naming
variables
The field declarations (variables) and methods are defined within the
class
CIS1400 Programming Logic and Technique
14.2 Classes
Access specifiers
Public allows for all parts of the code to access the class
members
Internal implementation
External Interface
14.2 Classes
class type
variable name
memory address
is assigned to
VariableName
9
creating an object
in memory using
the ClassName
class as the
blueprint
10 -- Object Oriented Programming
14.2 Classes
Inside Class Listing 14-3
10
14.2 Classes
Inside Program 14-1
Creating a class variable
Creating an object and
assigning it to class
variable
Using mutators to set
field values
Using accessors to
display field values
Dot notation (.) used to
associate instance with
class member
11
14.2 Classes
Constructor is a method that is automatically called when
an object is created
12
14.2 Classes
Class Listing 14-4
Class CellPhone
// fields
Private String manufacturer
Private String modelNumber
Private Real retailPrice
// constructor
Public Module CellPhone(String manufact, String modNum,
Real retail)
Set manufacturer = manufact
Set modelNumber = modNum
Set retailPrice = retail
End Module
13
14.2 Classes
Class Listing 14-4 (contd)
// mutator methods
Public Module setManufacturer(String manufac)
Set manufacturer = manufact
End Module
Public Module setModelNumber(String modNum)
Set modelNumber = modNum
End Module
Public Module setRetailPrice(Real retail)
Set retailPrice = retail
End Module
14
14.2 Classes
Class Listing 14-4 (contd)
// accessor functions
Public Function String getManufacturer()
Return manufacturer
End Function
Public Function String getModelNumber()
Return modelNumber
End Function
Public Function Real getRetailPrice()
Return retailPrice
End Function
End Class
15
14.2 Classes
Program14-2
Module main()
Declare CellPhone myPhone
Set myPhone = New CellPhone(Motorola, M1000, 199.99)
Display The manufacturer is , myPhone.getManufacturer()
Display The model number is , myPhone.getModelNumber()
Display The retail price is , myPhone.getRetailPrice()
End Module
ProgramOutput
The manufacturer is Motorola
The model number is M1000
The retail price is 199.99
16
OOP
Fundamentals
17
// field
// accessor method
setManufacturer(manufact : String)
// mutator method
18
manufacturer : String
getRetailPrice() : Real
- manufacturer : String
+ setManufacturer(manufact : String)
CIS1400 Programming Logic and Technique
// private field
// public method
10 -- Object Oriented Programming
Visio Example
19
OOP
Fundamentals
Classes
Class responsibilities
Determining classes:
Write description of problem domain
Physical objects
Identified roles
Results of an event
Recordkeeping items
In The
Spotlight
(p539)
20
Determining classes:
Identify nouns
21
Customer
Car
ServiceQuote
- name:String
- address:String
- phone:String
- make:String
- model:String
- year:Integer
- partsCharges:Real
- laborCharges:Real
+ Customer(n:String,
a:String, p:String)
+ setName(n:String)
+ setAddress(a:String)
+ setPhone(p:String)
+ getName():String
+ getAddress():String
+ getPhone():String
+ Car(carMake:String,
carModel:String,
carYear:Integer)
+ setMake(m:String)
+ setModel(m:String)
+ setYear(y:Integer)
+ getMake():String
+ getModel():String
+ getYear():Integer
+ ServiceQuote(pc:Real, lc:Real)
+ setPartsCharge(pc:Real)
+ setLaborCharge(lc:Real)
+ getPartsCharge():Real
+ getLaborCharge():Real
+ getSalesTax(taxRate:Real):Real
+ getTotalCharge():Real
22
fields
methods
23
14.5 Inheritance
OOP
Fundamentals
is-a relationship
superclass
subclass
14.5 Inheritance
Class Listing 14-8
Course Grader
The department grading scale is based on the
standard 90/80/70/60 points for A/B/C/D/F grades.
Class GradedActivity
Private Real score
GradedActivity
Public Module setScore(Real s)
- score: Real
Set score = s
End Module
Public Function Real getScore()
Return score
+ setScore(s : Real)
+ getScore() : Real
+ getGrade() : String
End Function
25
14.5 Inheritance
Class Listing 14-8 (contd)
Public Function String getGrade()
Declare String grade
GradedActivity
- score: Real
+ setScore(s : Real)
+ getScore() : Real
+ getGrade() : String
14.5 Inheritance
Program 14-4
Module main()
Declare Real testScore
Declare GradedActivity test
Set test = new GradedActivity()
Display Enter a numeric test score.
Input testScore
test.setScore(testScore)
Display The grade for that test is , test.getGrade()
End Module
Output
Enter a numeric test score.
89 [Enter]
The grade for that test is B
27
14.5 Inheritance
GradedActivity
- score: Real
Course Grader
The department grading scale is based on the
standard 90/80/70/60 points for A/B/C/D/F grades.
The Final Exams are worth a maximum of 100
points but can have any number of questions; each
question is equally weighted.
+ setScore(s : Real)
+ getScore() : Real
+ getGrade() : String
is-a
FinalExam
28
- numQuestions : Integer
- pointsEach : Real
- numMissed : Integer
+ FinalExam(questions :
Integer, missed :
Integer)
+ getPointsEach() : Real
+ getNumMissed() :
Integer
10 -- Object Oriented Programming
14.5 Inheritance
FinalExam
- numQuestions : Integer
- pointsEach : Real
- numMissed : Integer
+ FinalExam(questions :
Integer, missed :
Integer)
+ getPointsEach() : Real
+ getNumMissed() :
Integer
14.5 Inheritance
Class Listing 14-9 (contd)
FinalExam
Public Function Real getPointsEach()
Return pointsEach
End Function
Public Function Integer getNumMissed()
Return numMissed
End Function
End Class
30
- numQuestions : Integer
- pointsEach : Real
- numMissed : Integer
+ FinalExam(questions :
Integer, missed :
Integer)
+ getPointsEach() : Real
+ getNumMissed() :
Integer
14.5 Inheritance
GradedActivity
Program 14-5
- score: Real
Module main()
+ setScore(s : Real)
+ getScore() : Real
+ getGrade() : String
is-a
FinalExam
Input missed
- numQuestions : Integer
- pointsEach : Real
- numMissed : Integer
+ FinalExam(questions :
Integer, missed :
Integer)
+ getPointsEach() : Real
+ getNumMissed() :
Integer
14.5 Inheritance
GradedActivity
- score: Real
Output
+ setScore(s : Real)
+ getScore() : Real
+ getGrade() : String
50 [Enter]
Enter the number of questions that the student missed.
5 [Enter]
Each question on the exam counts 2 points
is-a
FinalExam
- numQuestions : Integer
- pointsEach : Real
- numMissed : Integer
+ FinalExam(questions :
Integer, missed :
Integer)
+ getPointsEach() : Real
+ getNumMissed() :
Integer
32
14.6 Polymorphism
OOP
Fundamentals
33
14.6 Polymorphism
Graded Activity
- score: Real
Course Grader
The department grading scale is based on the standard
90/80/70/60 points for A/B/C/D/F grades. Lab
assignments have a maximum value of 10 points and
use the department grading scale. The Final Exams are
worth a maximum of 100 points but can have any
number of questions; each question is equally weighted.
The Final Exam is more selective with the grading scale
and uses 95/85/75/65 points for A/B/C/D/F grades.
+ setScore(s : Real)
+ getScore() : Real
+ getGrade() : String
is-a
is-a
LabAssign
FinalExam
- numQuestions : Integer
- pointsEach : Real
- numMissed : Integer
+ LabAssign(points : Real)
34
+ FinalExam(questions :
Integer, missed :
Integer)
+ getPointsEach() : Real
+ getNumMissed() :
Integer
+getGrade() : String
14.6 Polymorphism
Graded Activity
- score: Real
+ setScore(s : Real)
+ getScore() : Real
+ getGrade() : String
is-a
End Class
LabAssign
+ LabAssign(points : Real)
35
14.6 Polymorphism
Class FinalExam Extends GradedActivity
Graded Activity
. . .
Public Function String getGrade()
- score: Real
+ setScore(s : Real)
+ getScore() : Real
+ getGrade() : String
is-a
FinalExam
- numQuestions : Integer
- pointsEach : Real
- numMissed : Integer
+ FinalExam(questions :
Integer, missed :
Integer)
+ getPointsEach() : Real
+ getNumMissed() :
Integer
+getGrade() : String
End Class
36
14.6 Polymorphism
Sample Program
37
14.6 Polymorphism
Sample Program
Module main()
Declare Real labScore
Declare Integer questions, missed
Declare LabAssign lab
Declare FinalExam exam
Display Enter lab score (on 10 point scale).
Input labScore
Set lab = new LabAssign(labScore)
38
14.6 Polymorphism
Sample Program
Display Enter the number of questions on the exam.
Input questions
Display Enter the number of missed questions.
Input missed
Set exam = new FinalExam(questions, missed)
Display Lab Details
DisplayDetails(OOPLab)
Display Exam Details
DisplayDetails(exam)
End Module
39
14.6 Polymorphism
Output
Enter lab score (on 10 point scale).
9 [Enter]
Enter the number of questions on the exam.
50 [Enter]
Enter the number of questions that the student missed.
5 [Enter]
Lab Details
Score = 90
Grade = A
appropriate grading scale used
for object type
Exam Details
Score = 90
Grade = B
40