Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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).

imageimageimageimage

// 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 ... 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

Artificial Intelligence Structures And Strategies For Complex Problem Solving

Authors: George Luger

6th Edition

0321545893, 9780321545893

Students also viewed these Programming questions