1.2.4 - Object Oriented Programming Redo
-
What attributes might be assigned to the following objects of the following classes?
Cathungeraffectionnameagefur_coloureye_colour
Rectanglex1y1x2y2
HotelBookingoccupantstartendroomrate
-
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_radioandphilips_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
Animalshare common attributes such asnameandposition. Animals may also have common procedures (methods), such asmove_left,move_right. ACatmay have an extra attributesize, and an extra methodpounce. ARodentmay have an extra methodgnaw. ABeavermay have an extra method,make_dam.What extra methods might
Mousehave?squeakscamper
-
The class
Animalmay 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 endclassCatis 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 endclassA rodent has an extra attribute
colour.
Write the class definition forRodentclass 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
Animalcan execute the methodsmove_leftandmove_right, causing the animal to move left or right by one space.But the subclasses may decide that a
Catcan move three spaces with amove_leftormove_rightmessage, and aRodentmoves 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
tomis an instance of theCatclass, andjerryis an instance of theMouseclass. What will happen when each of these statements is executed?tom.move_right()tom'spositionwill increase by 3
jerry.move_right()jerry'spositionwill 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:
MemberJuniorMemberSeniorMember
The classes
JuniorMemberandSeniorMemberare 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
Memberneed 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
Memberclass.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