Skip to content

Creating Objects - Task

Estimated time to read: 2 minutes

Exercise One

Step One

Create a new class named BoxDriver in the com.lq.exercises package.

Ensure that this class has a main() method defined.

Step Two

In the main method of BoxDriver, create two Box objects using the keyword new.

  1. A box named box1 with a length of 5, a width of 6 and a height of 7
  2. A box named box2 where all three sides are 10

Step Three

Using the get methods, print out the value for each attribute of both boxes.

Ensure that the attributes were set correctly by the Box constructors.

Execute the main method for BoxDriver. The output should be like the following.

If there are any errors in your Box code at this point, correct them and re-run the test.

Screenshot 2022-07-25 at 13.28.05.png

Exercise Two

Step One

Ensure that your set methods work correctly. Call each set method for box1 changing the values to the following and then use the get methods to ensure that the attributes were set correctly

Length = 3
Width = 4
Height = 5

Step Two

Call the getVolume()and getSurfaceArea() methods on box1 and ensure they are functioning correctly. If not, fix the code and re-test.

Step Three

Execute the printBox() for box1. Ensure that it is functioning correctly. If not, fix the code and re-test.

Step Four

Using the setLength()method, change the length of box1 to -5. Execute the printBox() method again. What happens? You should receive a message stating that the box contains invalid attributes.

Step Five

In order to avoid setting attributes to invalid values, re-code the set methods of the Box class to only accept values greater than 0. If a value less than or equal to zero is detected, print an error message.

Step Six

Re-run your BoxDriver main method. You should now receive an error message when you try and set the length to -5. Test the other two set methods in the same manner.