Question
Take a GOOD look at the java files given and then compile and run them. We have a Point, Square, and Cube. The Point has
Take a GOOD look at the java files given and then compile and run them. We have a Point, Square, and Cube. The Point has three doubles for the X and Y and Z coordinates. This is an example of a 'has-a' relationship "Point 'has' three doubles". The Square 'has-a' Point and 1 sides (all sides have equal length in a Square). The Cube 'has-a' Square and a depth (also a double). These files make a Point, Square, and Cube with composition ('has-a' relationship). The Square uses a Point as a member variable, it has-a Point. The Cube like-wise uses a Square (has a Square) and a depth. They CONTAIN them. They are composed of one upon another. They are created with composition or aggregation. Inheritance is different, objects can still contain other objects (e.g. a double for length of a side or x coordinate) but the building on one another is done differently. We want to now think of a Square 'as a' Point with a side (not a point and a side). Not as in above where the Square has a Point (contains a point) and contains a side (double). BUT think of a square 'as-a' Point with one double for (all) the sides. Continuing with this line of thinking, we have a Cube as-a Square with a depth. So what you need to do is create same the set of shapes () but using inheritance. First, look closely at the new class Shape, then the Point class that extends Shape (a Point is-a shape). This actually almost exactly the same as the class Point above. Next, after that Point, develop a class for Square that extends point, (a square is-a point with one double for both sides). This new Square must NOT have a Point as a variable. It is an extension of Point, Point is its base class. And lastly, create the new Cube class that extends Square, (a Cube is-a Square with a depth). This cube class will NOT have a Square (or Point) as a member variable. A Cube is a Square (with depth).
// Driver.java // Driver for point, square, cube composition program import java.util.*; public class Driver { public static void main(String[] args) { Cube cube; cube new Cube ( 10, 10, 10, 3.3 ); System.out.println( cube.getSPointName() + cube.getSPointString()); } System.out.println( cube.getSquareName() + cube.getSquareString() ); System.out.println( cube.getName () + ": System.out.println( cube.getSPointName() + ": " + cube.getSPointString() ); + + cube.toString() ); System.out.println( cube.getSquareName() + ": " + cube.getSquareString () ); System.out.println( "Area = " + cube.getSquareArea() ); System.out.println( cube.getName () + ": " + cube.toString() ); System.out.println( "Area = "+cube.area () ); System.out.println( "Volume = " + cube. volume () );
Step by Step Solution
3.47 Rating (160 Votes )
There are 3 Steps involved in it
Step: 1
I have updated the given code to use inheritance rather than aggregation Squarejava Squarejava Definition of class Square This class uses Composition ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started