Question 3
Write a Java program to allow user to create instances of Rectangle, Cuboid and Pyramid; and search the instances by different criteria. The details of the classes are given as follows:
class | Rectangle |
Instance variables | Length and width of a rectangle. You may decide the type of each instance variable. You may include additional variables as you deem fit. |
Constructor | A non default constructor that receives parameters and initialises the instance variables. |
Get and set methods | Include get and set methods for all instance variables. |
getArea | Instance method to calculate and return the rectangle's area, which is length x width. |
toString | Return a descriptive String of the instance. |
class | Cuboid (subclass of Rectangle) |
Instance variables | Height of a cuboid. You may decide the type of each instance variable. You may include additional variables as you deem fit. |
Constructor | A non default constructor that receives parameters and initialises the instance variables, including the instance variables of the base class. |
Get and set methods | Include get and set methods for all instance variables. |
getArea | Instance method to calculate and return the surface area (6 sides) of a cuboid. |
getVolume | Instance method to calculate and return the volume of a cuboid, which is base area x height. |
toString | Return a descriptive String of the instance. |
class | Pyramid (subclass of Rectangle) |
Instance variables | Height of a pyramid. You may decide the type of each instance variable. You may include additional variables as you deem fit. |
Constructor | A non default constructor that receives parameters and initialises the instance variables, including the instance variables of the base class. |
Get and set methods | Include get and set methods for all instance variables. |
getVolume | Instance method to calculate and return the volume of a pyramid, which is (base area x height) / 3. |
toString | Return a descriptive String of the instance. |
Note: The Pyramid class does not override the getArea method.
The program must read a text file containing the instance data, create an appropriate instance (Rectangle, Cuboid or Pyramid) for each data, and store it in an ArrayList for subsequent use. A sample of the text file is shown below.
C,10.5,20.5,30 P,5,10,15.3 R,2,5.5 P,2,5,10 C,5,A,20 P,0,10,-1 C,8,10,15 |
The program will skip erroneous lines in the file. After the instances are created and stored in an ArrayList, the program will allow user to perform the following operations repeatedly:
1 Search by length
2 Search by height
3 Quit
Option 1
The program will prompt the user to enter a number (length) and display all instances with length >= the length entered by the user. The program will display an appropriate message if there is no matching instance. The program must handle incorrect data input: non-numeric input and invalid length (0 or negative).
Option 2
The program will prompt the user to enter a number (height) and display all instances with height >= the height entered by the user. The program will display an appropriate message if there is no matching instance. The program must handle incorrect data input: non-numeric input and invalid height (0 or negative).