Computers and Technology
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

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 22:00, blackjack73
3. (6 pts) internally in the computer, with few exceptions, all numerical computation is done using binary numbers. output, however, often uses ascii, which is formed by appending 011 to the left of a bcd code. thus, an algorithm that directly converts a binary integer to a bcd integer is very useful. here is one such algorithm 1) draw lines to the left of the binary number to bound the expected bcd decades. (each decade is a group of 4 bits.) move the binary number one bit to the left. add 0011 to each bcd decade containing a binary value> 0100 repeat steps 2-3 until the last bit in the binary number has been moved into the least significant decade position. (note that when the last bit has been shifted into bcd decade, step 3 is not repeated.) read the bcd result. 2) 3) 4) 5) a) execute the algorithm for the binary number 1101101 b) execute the algorithm for the binary number 01110101110 4. (4 pts) represent the decimal number 3568 in bcd; excess-3 code; ascil; and hex.
Answers: 1
image
Computers and Technology, 23.06.2019 13:00, dimondqueen511
Which one of the following voltages should never be measured directly with a vom? a. 1200 v b. 500 v c. 800 v d. 100v
Answers: 2
image
Computers and Technology, 23.06.2019 15:20, manarhizam12
An ou structure in your domain has one ou per department, and all the computer and user accounts are in their respective ous. you have configured several gpos defining computer and user policies and linked the gpos to the domain. a group of managers in the marketing department need different policies that differ from those of the rest of the marketing department users and computers, but you don't want to change the top-level ou structure. which of the following gpo processing features are you most likely to use? a, block inheritance b, gpo enforcement c, wmi filtering d, loopback processing
Answers: 3
image
Computers and Technology, 23.06.2019 17:30, cxttiemsp021
Per the municipal solid waste report, what are the most common sources of waste (trash
Answers: 3
Do you know the correct answer?
Java programming. Implement the Shape hierarchy shown in Fig. 9.3 of the textbook. Each TwoDimensio...

Questions in other subjects:

Konu
History, 30.03.2021 01:00