java Design a class named MyCube to represent a cube. A cube is a three-dimensional solid object bounded by six square faces. Implement the MyCube class using composition. The MyCube class contains the following:
- A private instance variable named base (of type MySquare2D) that specifies the base square of a cube with default top left corner at (0, 0) and 10 for side length.
- A no-arg default constructor that creates a cube with default values.
- An overloaded constructor that creates a cube with the specified side length.
- An overloaded constructor that creates a cube with the specified top left corner coordinates (of type Point).
- An overloaded constructor that creates a cube with the specified top left corner (of type Point) and side length.
- An overloaded constructor that creates a cube with the specified x, y coordinates and side length.
- An overloaded constructor that creates a cube with the specified base (of type MySquare2D).
- The toString() method that returns a string describing the object as shown in the examples below. Note: the toString() of the MySquare2D class returns MySquare2D:(x, y), side=d.
Write the MyCube class in the answer box below assuming that the MySquare2D class is given.
Composition: A cube has 6 squares faces.
For example:
Test | Result |
---|---|
MyCube c1 = new MyCube(5);System.out.println(c1);MyCube c2 = new MyCube(10,20,5);System.out.println(c2);MyCube c3 = new MyCube(new Point(10,20));System.out.println(c3);MyCube c4 = new MyCube(new Point(10,20), 20);System.out.println(c4);MyCube c5 = new MyCube();System.out.println(c5); |
MyCube: MySquare2D:(0, 0), side=5MyCube: MySquare2D:(10, 20), side=5MyCube: MySquare2D:(10, 20), side=10MyCube: MySquare2D:(10, 20), side=20MyCube: MySquare2D:(0, 0), side=10 |
add the following methods to your MyCube class to extend its functionality:
- The accessor and mutator methods for the base.
- The getVolume() method that returns the volume of a MyCube object.
- The getSurfaceArea() method that returns the surface area of a MyCube object. The surface area of a Mycube object is: 6 * base area.
- The getBase() method that returns the base of a MyCube object.
Write the entire MyCube class in the answer box below assuming that the MySquare class is given.
For example:
Test | Result |
---|---|
MyCube c1 = new MyCube(15);System.out.println(c1.getBase());System.out.printf("volume=%d ", c1.getVolume());System.out.printf("surface area=%d ", c1.getSurfaceArea()); |
MySquare2D:(0, 0), side=15volume=3375surface area=1350 |
MyCube c1 = new MyCube (10, 20, 30);System.out.println(c1.getBase());System.out.printf("volume=%d ", c1.getVolume());System.out.printf("surface area=%d ", c1.getSurfaceArea()); |
MySquare2D:(10, 20), side=30volume=27000surface area=5400 |
MySquare2D s1 = new MySquare2D(12, 23, 8);MyCube c1 = new MyCube(s1);System.out.printf("volume=%d ", c1.getVolume());System.out.printf("surface area=%d ", c1.getSurfaceArea());MySquare2D base = c1.getBase();System.out.println(base); |
volume=512surface area=384MySquare2D:(12, 23), side=8 |
Reviews
There are no reviews yet.