Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
44 views

Practical 3 - Introduction To Object-Oriented Programming (Amended) PDF

This document provides instructions for three programming assignments involving object-oriented programming in Python. The first asks students to create a BankAccount class with methods for depositing, withdrawing, and checking balances. The second asks students to create a Rectangle class to represent 2D rectangles, with methods for calculating perimeter, area, and comparing areas. The third modifies an existing Time class to add methods for comparing times, calculating time differences in minutes, and adding minutes to a time. Students are instructed to write test classes to validate each class.

Uploaded by

Darian Chetty
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Practical 3 - Introduction To Object-Oriented Programming (Amended) PDF

This document provides instructions for three programming assignments involving object-oriented programming in Python. The first asks students to create a BankAccount class with methods for depositing, withdrawing, and checking balances. The second asks students to create a Rectangle class to represent 2D rectangles, with methods for calculating perimeter, area, and comparing areas. The third modifies an existing Time class to add methods for comparing times, calculating time differences in minutes, and adding minutes to a time. Students are instructed to write test classes to validate each class.

Uploaded by

Darian Chetty
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

COMP102: Computer Programming

Practical 3: Introduction to Object-Oriented Programming

Thursday, 9 September 2021

Question 1: Bank Account


Create a class named BankAccount which has the following attribute:

• Balance

Implement methods that will allow objects of the class to perform the following
functions:

• Deposit money into the account


• Withdraw a certain amount from the account if the balance allows for it
• Check how much money is left in the account

All methods should display appropriate messages to the user, such as the new
balance when a deposit or withdrawal is made. None of these methods return any
value.

In addition to the methods listed above, create a constructor which allows someone
to safely create a BankAccount object by supplying an initial balance.

Create a class called TestBankAccount and test all the functionality of the
BankAccount class.

Question 2: Rectangle Class


Create a class named Rectangle that represents a 2D rectangle. It should contain
the following attributes:

• Length
• Breath

Implement the following set of methods for the Rectangle class:

• A method that returns the perimeter of the Rectangle object


• A method that returns the area of the Rectangle object
• A method that determines whether another Rectangle object passed in as an
argument has a greater area than this object or not. This method should
return a Boolean value
In addition to the methods above, create a constructor that allows for the safe
construction of a Rectangle object. For example, we should not be allowed to
create a rectangle with a length or breath that is negative or equal to 0.

Finally, create a class called TestRectangle that tests all of the methods of the
Rectangle class.

Question 3: Time Class


Modify the Time class we’ve been using as an example this week by adding the
following methods:

• A method that determines whether another Time object passed in as an


argument represents a time after the time represented by the current object.
If the time is after, the method should return 1, is the time is the same the
method should return 0, and if the time is before the method should return -1
• A method that returns the duration between this Time object and another
Time object passed in as argument. The duration should be in minutes
• A method adds the number of minutes passed in as an argument to the time
represented by the current object. The method should display the new time
to the user, and say whether the addition caused the time to roll over to the
next day or not

Modify the TestTime class to test the three methods above.

You might also like