0% found this document useful (0 votes)
174 views6 pages

OOP Worksheet: Dog & Shape Classes

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)
174 views6 pages

OOP Worksheet: Dog & Shape Classes

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

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)
[Link](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)
[Link](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.

Common questions

Powered by AI

The Dog class uses encapsulation, one of the primary principles of object-oriented programming, to restrict access to the attributes 'name' and 'colour' by defining them as private. The class provides public procedures such as 'setColour', 'getColour', and 'getName' to modify or retrieve these attributes, thereby enforcing control over how these data members are accessed and modified. This design ensures that the attributes can only be accessed or changed through the specified methods, which provides flexibility as it allows the manipulation of dog attributes while maintaining data integrity .

To track the number of times each dog barks, you can add a new private attribute 'barkCount' to the Dog class. Modify the 'bark' procedure to increment 'barkCount' each time the procedure is called. Provide public methods 'getBarkCount' to retrieve the count value and 'resetBarkCount' if a reset is needed. This encapsulates the tracking mechanism within the Dog class, following encapsulation and ensuring that direct access to the 'barkCount' variable is restricted, thereby maintaining control over the associated data integrity .

Method overriding enables a subclass to provide a specific implementation of a method already defined in its superclass. In the Puppy class, overriding 'bark' allows for a new behavior ('Yap!') that distinguishes it from the Dog class's 'Woof!' sound. This promotes flexibility and specificity in behavior replication. However, challenges include ensuring consistent performance across subclasses and potential for increased complexity if not managed correctly, as changes in the superclass may require corresponding changes in all subclasses .

Encapsulation helps protect the integrity of data by limiting external access to the class attributes, permitting access only through well-defined interfaces. In classes like Dog and Puppy, encapsulation ensures that attributes like 'name', 'colour', and 'shoesChewed' are not directly accessed or modified by client code, which helps maintain data integrity and supports the class's internal logic without interference. It also enables the developer to change the internal implementation without affecting external code that interacts with the class .

Polymorphism is evident in how each subclass of the Shape class can redefine the method 'calculateArea' to fit its geometrical specifics: a Rectangle uses height and width, while a Circle would use radius to calculate its area. This ability allows different shapes to implement their logic while sharing a common interface, enhancing flexibility and scalability. Polymorphism allows for dynamic method invocation, enabling general code to work with objects of different types as long as they share the same interface, which facilitates code reuse and robustness .

In Task 4, inheritance is used by having Rectangle and Circle as subclasses of Shape, which means they inherit attributes like 'colourFill', 'colourOutline', and the method 'calculateArea'. This allows both subclasses to use and adapt shared functionality and enables code reusability, where common characteristics are managed in one place (Shape class). This design reduces redundancy, aligns with the DRY principle (Don't Repeat Yourself), and fosters scalability, as changes to common behavior such as 'calculateArea' only need to be made in one place while subclass-specific functionality can extend or customize inherited methods .

Effective strategies include unit testing each overridden method with various inputs to confirm expected behaviors. For Puppy, tests would involve creating instances and verifying that the 'bark' method outputs 'Yap!' as expected. Similarly, each Shape subclass should be instantiated and 'calculateArea' should be tested with valid dimensions to ensure correct area calculations. Mocking or stubbing can simulate superclass methods to ensure overridden functions operate correctly. Maintaining a comprehensive test suite for inherited functionality ensures robustness and early detection of regressions or errors due to modifications .

The Dog class's instantiation involves setting the 'name' and 'colour' attributes via its constructor. The Puppy class, however, extends this by calling the base class (Dog) constructor using 'super.new()' to handle inherited attributes ('name', 'colour'), and adds its own attribute 'dob' directly in its constructor. This approach allows the subclass to initialize its specific attribute while leveraging the existing logic for inherited attributes, showcasing how constructors are extended to accommodate subclass-specific needs without duplicating code .

Adding a new method to the Shape class would automatically make it available to its subclasses, such as Rectangle and Circle, unless they override it. This can enhance subclass capabilities by providing new shared functionality without modifying subclass code. However, it could also introduce dependencies if the method’s logic conflicts with or needs to integrate into the subclass-specific behavior, which would require careful assessment of its impact on existing subclass methods .

The Puppy class inherits from the Dog class, which means it gains all the methods and attributes of the Dog class unless it overrides them. Inheritance allows the Puppy class to use the procedures from the Dog class like 'bark', 'getName', and others unless explicitly redefined. The Puppy class redefines the 'bark' method to print 'Yap!' instead of 'Woof!', showcasing polymorphism, where a subclass modifies the behavior of a method defined in its superclass .

You might also like