Worksheet 6 Object oriented programming
Worksheet 6 Object oriented programming
Task 1
1. The class definition for Dog has been defined below.
class Dog
private name
private colour
public procedure new (myName, myColour)
name = myName
colour = myColour
endprocedure
(a) Add new procedures to the Dog class to set the colour, get the colour and get the
name of the dog.
public procedure setColour(dogColour)
colour = dogColour
endprocedure
public procedure getColour()
return colour
endprocedure
(b) Write statements to instantiate two new Dog objects, myDog3 and myDog4, named
Mutt and Jeff, both having colour “Unknown”
Class myDog3
Name = Mutt
Colour = unknown
Class myDog4
Name = Jeff
Colour = unknown
1
Worksheet 6 Object oriented programming
Unit 11 Programming techniques
(c) Write statements which will check the colour of one of the dogs and if it is “Unknown”,
ask the user to enter its colour, and record this as the colour attribute of the dog. Print
the name of the dog and its colour.
if colour == ‘Unknown’:
colour = input(“enter the colour of the dog: “)
print(“name:”, name, “and colour: “, colour)
Task 2
2. Here is part of the class definition for Puppy
(a) Write a method chewShoe (numShoes) which will go in the class definition of
Puppy.
The method will add numShoes to shoeschewed
Public procedure chewShoe(numShoes)
If chewShoe = TRUE:
numShoes = numShoes + 1
(b) Write a method getShoesChewed which will go in the class definition of Puppy.
This method will return the number of shoes chewed
Public procedure setShoesChewed(numShoes)
Shoes = numShoes
Public procedure getShoesChewed()
Return shoes
(c) Write statements which will instantiate a Puppy object and call the method chewShoe
several times.
Call the method getName and getShoesChewed for the Puppy object and print out
the name and total number of shoes the puppy has chewed.
Public procedure getName()
Return name
End procedure
2
Worksheet 6 Object oriented programming
Unit 11 Programming techniques
Public procedure getShoesChewed()
Return chewShoe
Endprocedure
Task 3
3. The new class definition for Puppy is shown below.
(a) Write a new method in the Puppy class which redefines the bark method in the Dog
class so that it prints “Yap!” instead of “Woof!”
(b) Instantiate a new Puppy object named Malla, colour Light brown, date of birth
12/08/2016.
public procedure new(myName, myColour, myDob)
myName = Malla
myColour = Light brown
Dob = 12/08/2016
(c) Call the bark method to make the puppy bark twice.
public procedure bark(barkTimes)
for n = 1 to barktimes
print (“Yap!”)
print (“Yap!”)
next n
endprocedure
(d) Write a method to get the date of birth of the puppy
3
Worksheet 6 Object oriented programming
Unit 11 Programming techniques
4
Worksheet 6 Object oriented programming
Unit 11 Programming techniques
Task 4
4. A class called shape has subclasses named rectangle, triangle and circle.
(a) Draw an inheritance diagram showing these three classes. (Remember to use an
open-headed arrow, and make sure it is pointing in the correct direction.)
(b) The shape class has attributes colourFill, colourOutline and a method
calculateArea. Each of the inherited classes redefines the method called
calculateArea, and each class has additional attributes not found in the parent
class.
What is the name given to the programming language’s ability to redefine methods
defined in the parent class?
myWidth)
super.new(myColourFill, myColourOutline)
height = myHeight
width = myWidth
endprocedure
5
Worksheet 6 Object oriented programming
Unit 11 Programming techniques
Complete the class definition for the Rectangle class and write the class definition for
the Circle class to correctly calculate and return the area for each shape.
(d) Write statements to instantiate a rectangle and a circle and call the methods to
calculate their areas. Print the areas.