Unit 7 – Chapter 14: Example -Service
class with Selection
Problem 1: Class Elephant. Includes Fields
and methods with their access types. Include Constructors, mutators, accessors, and
other methods to print field values and get field values from user via
keyboard. Valid elephant size is between 6 and 24 inclusive. Make sure to check
for the size when setting the sizes.
- Add a method to display the size of the elephant if it
is a big elephant. An elephant is big if it’s size is greater than 20
Elephant
// Class
name or data type |
//Fields
or attributes also called data
member or instance data -origin: string |
// methods – behavior //constructors +Elephant(Int nsize, String norigin) //second
constructor //mutators +setSize(Int nsize) +setOrigin(String norigin) //accessors +getSize(): Int size +getOrigin(): String origin //other methods +printAllInfo() +inputFields() +diaplayBigElephant() |
Class Elephant
//Fields or attributes
Private Int size
Private String origin
//Default Constructor
Public Module Elephant()
Set size = 0
Set origin = “ “
End Module
//Second Constructor
Public Module Elephant (Int nsize, String
norigin)
If
(nsize >= 6 AND nsize <= 24) then
Set size = nsize
Else
Set size = 0
Display “Invalid size”
Endif
Set origin = norigin
End Module
//mutators
Public Module setSize(Int nsize)
If
(nsize >= 6 AND nsize <= 24) then
Set size = nsize
Else
Display “Invalid size”
Endif
End Module
Public Module setType(string norigin)
Set origin = norigin
End Module
//accessors
Public
Function Int getSize()
Return
size
End Function
Public Function String getOrigin()
Return origin
End Function
//other
methods
Public Module printAttributes()
Display “Elephant Size: “, size
Display “Elephant Origin: “,origin
End Module
Public Module inputFields()
Display “Enter Elephant Size between 6 -24 “
Input size
Get Free Quote!
329 Experts Online