Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in java, (box code pictured below) Create a new class named Cube that extends Box and place it in the com.lq.exercises package in the ClassExercises

in java, (box code pictured below) image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Create a new class named Cube that extends Box and place it in the com.lq.exercises package in the ClassExercises project. A Cube is like a Box however all three dimensions of a Cube must be the same. We simulated this in our Box class by providing a constructor that would set all 3 sides to the same value. We will formalize this here. We don't really need any new attributes since we inherit everything we need from Box. The first thing we need to do is define a constructor for Cube. Since a Cube must have all 3 sides set to the same value, our constructor only needs one parameter. We do not need to set any data in the Cube class so all we need is a call to our super class constructor (Hint: Use the super keyword as the first line in the Cube constructor). - Cube inherits all the public methods from Box. However, if we set the length of a Cube to a new value, we also want to guarantee that the height and width are updated as well. Override each of the following methods in the Cube class to guarantee this functionality. (Remember, an overridden method must have the exact same signature as the base class method it is overriding). a. setLength() b. setWidth() c. setHeight () 5. Add two new methods to get and set the sides of the Cube to a value. These methods work in a similar manner to the Box methods but will only be available to cubes. a. Create a setSide () method that takes one parameter of type double and returns a void. Ensure that the sides are set to a valid value (>0). b. Create a getSide () method that takes no parameters and returns a double. Exercise 3: Try out the Cube class 6. Create a new class named CubeDriver in the com.iq.exercises package. Ensure that this class has a main() method defined. 7. In the main method of Cube Test, create two Cube objects using the keyword new. a. A cube named cube1 with all sides set to 5 b. A cube named cube2 with all sides set to 8 8. Using the get methods, print out the value for each attribute of both cubes. Ensure that the attributes were set correctly by the Cube constructor. Execute the main() method for CubeDriver. The output should be similar to the following. If there are any errors in your Cube code at this point, correct them and re-run the test. Cube 1 length is + 5.0 Cube 1 width is + 5.0 Cube 1 height is + 5.0 Cube 2 length is + 8.0 Cube 2 width is + 8.0 Cube 2 height is + 8.0 Ensure that your set methods work correctly. Call the setLength() method for Cube cubel and change the length to 20. Use the inherited printBox() method to ensure that all sides were set correctly. Do the same test again calling the set Side () method with a value of 40. Your output should look as follows: Length = 20.0 Width = 20.0 Height = 20.0 Volume = 8000.0 Surface Area = 2400.0 Length = 40.0 Width = 40.0 Height = 40.0 Volume = 64000.0 Surface Area = 9600.0 Using the setWidth() method, change the width of Cube cube2 to -5. Did you receive the appropriate error message? Execute the printBox() method again and ensure that the value was not changed. Your output should look as follows: Height must be greater than o Length must be greater than 0 Width must be greater than 0 Length = 8.0 Width = 8.0 Height = 8.0 Volume = 512.0 Surface Area = 384.0 DUCCULUIDLJAV: package com. Iq.excercises; public class Box { NOUAWNE private double height; @param height param width @param length USUN public Box[double height double width double length) { super(); this.height = height; this.width = width; this.length = length; private double width; private double length; public Box(double height. double width. double length) { this.height = height; this.width = width; this.length = length; public Box(double value) { this(value, value, value); BUXUS GS public double getHeight() { double height = @; return height; public void setHeight(double height) { this.height = height; public double getWidth() { return width 38 39 public void setWidth(double width) { this.width - width; 42e public double getLength() 22 Java this(value, value, value); Dexeuse4.java u BasicCalculator public double getHeight() { double height = 0; return height; public void setHeight(double height) { this.height - height; public double getwidth() { return width; public void setWidth(double width) { this.width = width; public double getLength() { return length; public void setLength(double length) { this.length = length; public static void main(String[] args) { Box box=new Box(10); System.out.println(" Height =" +box.getHeight()); System.out.println("Length =" +box.getLength(); System.out.println("Width = "+box.getWidth()); box-new Box(10,20,30); System.out.println(" Height = "+box.getHeight()); System.out.println("Length = "+box.getLength()); System.out.println("Width="+box.getWidth())

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions

Question

6. Describe why communication is vital to everyone

Answered: 1 week ago