1.2.4 - Object Oriented Programming Redo
-
What attributes might be assigned to the following objects of the following classes?
Cat
hunger
affection
name
age
fur_colour
eye_colour
Rectangle
x1
y1
x2
y2
HotelBooking
occupant
start
end
room
rate
-
In the class definition for
Radio
(shown below) add the missing instance variables, and a procedure to set the volume.class Radio private volume private station private switch public procedure new(my_volume, my_station, my_switch) volume = my_volume station = my_station switch = my_switch endprocedure public procedure set_volume(new_volume) volume = new_volume endprocedure endclass
-
Write pseudocode statements to instantiate two new radio objects named
roberts_radio
andphilips_radio
.roberts_radio = new Radio(31.4, '88MHz', 'on') philips_radio = new Radio(27.2, '42MHz', 'off')
-
Class diagram involving inheritanceAll the animals in the superclass
Animal
share common attributes such asname
andposition
. Animals may also have common procedures (methods), such asmove_left
,move_right
. ACat
may have an extra attributesize
, and an extra methodpounce
. ARodent
may have an extra methodgnaw
. ABeaver
may have an extra method,make_dam
.What extra methods might
Mouse
have?squeak
scamper
-
The class
Animal
may be defined like this:class Animal private name private position public procedure new(my_name, my_position) name = my_name position = my_position endprocedure public procedure move_left(steps) position = position = steps endprocedure public function get_position() return position endfunction endclass
Cat
is a subclass ofAnimal
(and has an extra attribute - size); here is its class definition:class Cat inherits Animal private size public procedure new(my_name, my_position, my_size) super.new(my_name, my_position) size = my_size endprocedure public procedure move_left(steps) position = position - (steps * 3) endprocedure endclass
A rodent has an extra attribute
colour
.
Write the class definition forRodent
class Rodent inherits Animal private colour public procedure new(my_name, my_position, my_colour) super.new(my_name, my_position) colour = my_colour endprocedure endclass
-
All objects in the subclasses of
Animal
can execute the methodsmove_left
andmove_right
, causing the animal to move left or right by one space.But the subclasses may decide that a
Cat
can move three spaces with amove_left
ormove_right
message, and aRodent
moves two spaces. You would define different methods in each of the classes to implement these moves, but keeping the same method name. This would override the method in the superclass.Suppose that
tom
is an instance of theCat
class, andjerry
is an instance of theMouse
class. What will happen when each of these statements is executed?tom.move_right()
tom
'sposition
will increase by 3
jerry.move_right()
jerry
'sposition
will increase by 2
Exercises
-
A sports club keeps details of its members. Each member has a unique membership number, first name, surname, and telephone number recorded. Three classes have been identified:
Member
JuniorMember
SeniorMember
The classes
JuniorMember
andSeniorMember
are related, by single inheritance, to the classMember
.-
Draw an inheritance diagram for the given classes.
Member ╱ ╲ JuniorMember SeniorMember
-
Programs that use objects of the class
Member
need to create a new member, edit a member's details, delete a member's details, and show a member's details. No other form of access is to be allowed.Complete the definition of the attributes and the procedure new for the
Member
class.class Member private number public forename public surname public tel public procedure new(my_number, my_forename, my_surname, my_tel) number = my_number forename = my_forename surname = my_surname tel = my_tel endprocedure endclass
-
In object-oriented programming, what is meant by encapsulation?
- Encapsulation is where an object's state is protected, and all access and modifications are performed through the class' getters and setters, which can perform validation on the values assigned or calculation on the values retrieved