0% found this document useful (0 votes)
37 views

Worksheet 6 Object oriented programming

Uploaded by

xxsunsetstingxx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Worksheet 6 Object oriented programming

Uploaded by

xxsunsetstingxx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Worksheet 6 Object oriented programming

Unit 11 Programming techniques

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

public procedure bark(barkTimes)


for n = 1 to barktimes
print (“Woof!”)
next n
endprocedure
endclass

(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

Class Puppy inherits Dog


private shoesChewed
shoesChewed = 0

(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.

Class Puppy inherits Dog


private shoesChewed
shoesChewed = 0
public procedure new (myName, myColour, myDob)
super.new(myName, myColour)
dob = myDob

(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!”

public procedure bark(barkTimes)


for n = 1 to barktimes
print (“Yap!”)
next n
endprocedure

(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

Public procedure setDOB(myDob)


DOB = myDob
Public procedure getDOB()
Return DOB
Endprocedure
(e) Print out all the details of the new 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?

(c) The class definition for Shape is given below.


class Shape
private colourFill
private colourOutline
private area
public procedure new (myColourFill, myColourOutline)
colourFill = myColourFill
colourOutline = myColourOutline
endprocedure

public procedure calculateArea(mySide)


area = mySide * mySide
endprocedure
endclass

class Rectangle inherits Shape


private height
private width
public procedure new (myColourFill, myColourOutline, myHeight,

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.

You might also like