UNIVERSITY OF KWAZULU-NATAL
COMP102: Compute Programming
Practical 7: Consolidating Classes & Objects
Friday, 20 September 2024
Introduction
Consider the following 3D shapes:
[1] A right cylinder has a circle base with radius r and a height h, as shown in
Figure 1.
Figure 1: Illustration of right cylinder (credit: google.com)
[2] A right rectangular prism is a six-sided object with a rectangular base and
height h, as shown in Figure 2.
Figure 2: An illustration of a right rectangular prism (credit: google.com).
1
[3] A cone has a circular base with some radius r, and rises to a single point at a
height h, as shown in Figure 3.
Figure 3: Illustration of a cone (credit: google.com).
[4] A right regular pentagonal prism has a pentagonal base with an edge length of
a, and a height of h, as shown in Figure 4.
Figure 4: An illustration of a right regular pentagonal prism (credit: google.com).
Question One: 3D Shape Class
For each 3D shape described above:
1. Create a class for the shape. Include a constructor that allows a user of the
class to create objects and initialise them appropriately. The user should be
able to define the height, radius, width, length, etc. of the relevant shape.
Make sure that they cannot create objects with attributes equal to or less than
0
2. Provide get and set methods for each attribute in your class. Ensure that the
user cannot alter an attribute to have a value equal to or less than 0
3. Implement the toString() method
4. For each 3D shape class, create a method called getVolume() which will
return the volume of the object
5. For each 3D shape class, create a method called getSurfaceArea() which
will return the surface area of the object
6. Each 3D shape has a particular base shape, i.e. circle, rectangle or pentagon.
Write a method for each 3D shape class that will return the area of the base.
Name this method getBaseArea().
2
Question Two: Test Classes
For each 3D shape class you created above, write a class to test the methods. For
each test class:
1. Create an array of ten objects using randomly generated values
2. Create a method called getLargestVolume() that will return the 3D shape
with the largest volume
3. Create a method called getSmallestSurfaceArea() that will return the
3D shape object with the smallest surface area
-