subject
Computers and Technology, 19.09.2020 01:01 astra

Java programming. Implement the Shape hierarchy shown in Fig. 9.3 of the textbook. Each TwoDimensionalShape should contain method getArea to calculate the area of the two-dimensional shape. Each ThreeDimensionalShape should have methods getArea and getVolume to calculate the surface area and volume, respectively, of the three-dimensional shape. Do not implement a class for Tetrahedron
Create a program that uses an array of Shape references to objects of each concrete class in the hierarchy. The program should print a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If a shape is a TwoDimensionalShape, display its area. If a shape is a ThreeDimensionalShape, display its area and volume.
Hints:
• Create an abstract (base) class, called Shape, as follows:
public abstract class Shape
{
private int x; // x coordinate
private int y; // y coordinate
// two-argument constructor
public Shape( int x, int y )
{
this. x = x;
this. y = y;
} // end two-argument Shape constructor
// set x coordinate
public void setX( int x )
{
this. x = x;
} // end method setX
// set y coordinate
public void setY( int y )
{
this. y = y;
} // end method setY
// get x coordinate
public int getX()
{
return x;
} // end method getX
// get y coordinate
public int getY()
{
return y;
} // end method getY
// return String representation of Shape object
public String toString()
{
return String. format( "(%d, %d)", getX(), getY() );
}
// abstract methods
public abstract String getName();
} // end class Shape
The constructors of the subclasses will have the following arguments:
public abstract class TwoDimensionalShape extends Shape
{
private int dimension1;
private int dimension2;
public TwoDimensionalShape( int x, int y, int d1, int d2 )
{
super( x, y );
dimension1 = d1;
dimension2 = d2;
} // end four-argument TwoDimensionalShape constructor


public class Circle extends TwoDimensionalShape
{
// three-argument constructor
public Circle( int x, int y, int radius )
{
super( x, y, radius, radius );
} // end three-argument Circle constructor
……

public class Square extends TwoDimensionalShape
{
// three-argument constructor
public Square( int x, int y, int side )
{
super( x, y, side, side );
} // end three-argument Square constructor
……

public abstract class ThreeDimensionalShape extends Shape
{
private int dimension1;
private int dimension2;
private int dimension3;
// five-argument constructor
public ThreeDimensionalShape(int x, int y, int d1, int d2, int d3 )
{
super( x, y );
dimension1 = d1;
dimension2 = d2;
dimension3 = d3;
} // end five-argument ThreeDimensionalShape constructor
……

public class Sphere extends ThreeDimensionalShape
{
// three-argument constructor
public Sphere( int x, int y, int radius )
{
super( x, y, radius, radius, radius );
} // end three-argument Shape constructor
……

public class Cube extends ThreeDimensionalShape
{
// three-argument constructor
public Cube( int x, int y, int side )
{
super( x, y, side, side, side );
} // end three-argument Cube constructor
……
 Use the following class as a driver class
public class ShapeTest
{
// create Shape objects and display their information
public static void main( String args[] )
{
Shape shapes[] = new Shape[ 4 ];
shapes[ 0 ] = new Circle( 22, 88, 4 );
shapes[ 1 ] = new Square( 71, 96, 10 );
shapes[ 2 ] = new Sphere( 8, 89, 2 );
shapes[ 3 ] = new Cube( 79, 61, 8 );
// call method print on all shapes
for ( Shape currentShape : shapes )
{
System. out. printf( "%s: %s",currentShape. getName(), currentShape );
if ( currentShape instanceof TwoDimensionalShape )
{
TwoDimensionalShape twoDimensionalShape =
( TwoDimensionalShape ) currentShape;
System. out. printf( "%s's area is %s\n",
currentShape. getName(), twoDimensionalShape. getArea() );
} // end if
if ( currentShape instanceof ThreeDimensionalShape )
{
ThreeDimensionalShape threeDimensionalShape =
( ThreeDimensionalShape) currentShape;
System. out. printf( "%s's area is %s\n",
currentShape. getName(), threeDimensionalShape. getArea() );
System. out. printf( "%s's volume is %s\n",
currentShape. getName(),
threeDimensionalShape. getVolume() );
}
System. out. println();
} // end for
} // end main
} // end class ShapeTest

ansver
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 09:00, 19elbatawisaly
What is one way in which tablets differ from laptops and notebooks? tablets are designed for touch-based interaction. tablets are designed to be used as desktops. tablets are designed for input via a keyboard and mouse. tablets are designed to be larger than laptops.
Answers: 1
image
Computers and Technology, 22.06.2019 09:50, shadow29916
What is a rush associated with alcohol?
Answers: 1
image
Computers and Technology, 23.06.2019 14:00, savannnab1890
Select the correct answer. andre was recently hired by an organization to check for system vulnerabilities. he is supposed to exploit these vulnerabilities and create a report on the extent of damage to which the system was susceptible. what position does andre hold in this organization? a. information security analyst b. information assurance manager c. penetration tester d. network security engineer e. chief information security officer
Answers: 2
image
Computers and Technology, 24.06.2019 16:00, deepunalli300p3ur3i
Your is an example of personal information that you should keep private.
Answers: 2
You know the right answer?
Java programming. Implement the Shape hierarchy shown in Fig. 9.3 of the textbook. Each TwoDimensio...

Questions in other subjects:

Konu
Mathematics, 18.09.2020 14:01
Konu
Mathematics, 18.09.2020 14:01
Konu
Mathematics, 18.09.2020 14:01
Konu
Mathematics, 18.09.2020 14:01
Konu
Mathematics, 18.09.2020 14:01
Konu
History, 18.09.2020 14:01
Konu
Mathematics, 18.09.2020 14:01
Konu
Mathematics, 18.09.2020 14:01
Konu
Mathematics, 18.09.2020 14:01
Konu
Mathematics, 18.09.2020 14:01