Practical 6 - Introduction To Object-Oriented Programming
Practical 6 - Introduction To Object-Oriented Programming
Question 1: Rectangle
Create a class named Rectangle that represents a rectangle. It should contain the
following attributes:
• Length
• Breath
Implement the following set of methods for the Rectangle class:
• 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
• A method that returns the perimeter of the Rectangle object
• A method that returns the area of the Rectangle object
Finally, create a class called TestRectangle that tests all of the methods of the
Rectangle class.
Question 2: TwoDLine
Create a class called TwoDLine that represents a line on a 2-dimension cartesian
plane. It must have the following attributes:
• The x and y coordinate of the point of origin
• The x and y coordinate of the end point
Create the following methods for the class:
• A constructor that will create a new line. You need to ensure that the x and y
coordinates for both the point of origin and end points are initialised. Also
ensure that the two points are not identical
• A method to change the coordinates of the point of origin
• A method to change the coordinates of the end point
• A method to calculate the length of the line
Write a class called TestTwoDLine that will test all of the methods of the TwoDLine
class.
1
Question 3: Triangle
Create a class that represents a triangle. It must have the following properties:
• Length of the first side,
• Length of the second side, and
• Length of the third side.
The following methods must be implemented:
• A constructor to create a triangle object. The constructor must accept three
values from the user which are then assigned to attributes of the class. The
constructor must ensure that we do not have any negative lengths. The length
of a side also cannot be 0.
• A method called getArea() that calculates and returns the area of the triangle
• A method called getPerimeter() that calculates and returns the perimeter of
the triangle
• A method called showClassification() which displays the classification of the
triangle as isosceles, equilateral or scalene on the console.
Write a class called TestTriangle that will test all of the methods of the Triangle
class.
-