BCS 345 Lab 8
CRN 21177, XXXXXXXXXXJL Spring 2020
------------------------------------------------------------------------------------------------------------------------
Objectives: Use class inheritance to create new classes.
Tasks:
In our lecture, we wrote a program that defines and implements a class Rectangle. The source code
can be found on Blackboard -> Learning Modules -> Week 8 -> Demo Program: Rectangle class.
In this lab, we will use class inheritance to write a new class called Cube.
1. Create a new project with the main class called BCS345Lab8.
2. Copy the file Rectangle.java into the src directory of the new project or make a new class
Rectangle and copy the content of Rectangle.java into the new file. Note, the length and width
fields in the Rectangle class are private.
3. Make a new class called Cube. The Cube class inherits from the Rectangle class. The Cube class
has one extra field: private double height;
Write the following public methods for the Cube class and use the constructors and methods of the
parent class whenever possible.
1) setHeight method to set the height according to the argument. The valid value for the height is
non-negative, otherwise use 1.
2) getHeight method to return the height.
3) A no-arg constructor to create a Cube object and initialize the length, width, and height to
1.
4) A parameterized constructor to create a Cube object and initialize length, width, and height
according to the method parameters. All argument values must be non-negative. If a value is
negative, use 1 instead.
5) A copy constructor to create a Cube object and instantiate it with an existing Cube object.
6) getVolume method to calculate and return the volume of a Cube object.
7) Ove
ide the toString method to output the length, width, and height of a Cube object.
8) equals method to check if two Cube objects have the same dimension. Return true if they are
equal in length, width, and height; otherwise, return false.
4. In the client program BCS345Lab8.java, write statements to declare objects of Cube and test all
the methods/constructors of the Cube class.
5. In the client program, make an a
ay of Rectangle of size 4 and instantiate it with the following data:
A Rectangle object with length 10 and width 20,
A Rectangle object with length -4 and width -5 (to test data validation),
A Cube object with length 2, width 3, and height 4.
A Cube object with length 11, width 12, and height -13.
Use a loop to call the toString method implicitly to display the dimensions of the above objects.