Question

Write a Java program with the following requirements: Rectangle that implements Shape2D - instance variables height,...

Write a Java program with the following requirements:

Rectangle that implements Shape2D
- instance variables height, width

Block that implements Shape3D
- instance variables height, width, depth

Add appropriate parameterized constructors for each class.
Implement the abstract methods to make each class a Shape2D, Shape3D, or both.

Make the main method use each of these to print out the area of a rectangle, and the
area and volume of a block.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

abstract class Shape

{

abstract void Shapes();

Shape()

{

System.out.println("Inside cosntructor of class Shape");

}

}

class Rectangle extends Shape

{

int height,width;

Rectangle(int h,int w)

{

System.out.println("Inside comstructor of class Rectangle");

height=h;

width=w;

}

void Shapes()

{

System.out.println("This is a 2D Shape ");

}

void Shape2D()

{

System.out.println("Area of 2D shape is "+(height*width));

}

}

class Block extends Shape

{

int height,width,depth;

void Shapes(){

System.out.println("This is a 3d Shape ");}

Block(int h,int w,int d)

{

height=h;width=w;depth=d;

System.out.println("Inside constructor of class Block");

}

void Shape3D()

{

System.out.println("Area of 3D shape is "+(height*width*depth));

}

}

public class Main {

public static void main(String[] args) {

Rectangle r=new Rectangle(10,5);

r.Shape2D();

Block b=new Block(10,5,2);

b.Shape3D();

}

}

Main.java* 1 abstract class Shape 2 { abstract void Shapes(); Shape() { System.out.println(Inside cosntructor of class ShapeMain.java* 19 System.out.printIn(This is a 2D Shape ); } void Shape2D() { System.out.println(Area of 2D shape is + (heighTAB Inside cosntructor of class Shape Inside comstructor of class Rectangle Area of 2D shape is 50 Inside cosntructor of clasPlease upvote... Thanks

Add a comment
Know the answer?
Add Answer to:
Write a Java program with the following requirements: Rectangle that implements Shape2D - instance variables height,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT