Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sample Solution: Java Code: / / Shape.java - Base class Shape public class Shape { public double calculateArea ( ) { } return 0 ;

Sample Solution:
Java Code:
// Shape.java- Base class Shape
public class Shape {
public double calculateArea(){
}
return 0 ; // Default implementation returns
}
// Circle.java- Subclass Circle
public class Circle extends Shape {
private double radius;
public Circle(double radius){
this.radius = radius;
}
@override
public double calculateArea(){
return Math.PI ** radius ** radius; // Calculate area of circle
}
// Rectangle.java- Subclass Rectangle
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height){
this.width = width;
}
this. height = height;
@override
public double calculateArea(){
}
return width * height; // Calculate area of rectangle
}
// Triangle.java- Subclass Triangle
public class Triangle extends Shape {
private double base;
private double height;
public Triangle(double base, double height){
this. base = base;
}
this.height = height;
@override
public double calculateArea(){
}
}
// Main.java- Main class
public class Main {
public static void main(String[] args){
Circle circle = new Circle(4);
System.out.println("Area of Circle: "+ circle.calculateArea());
Rectangle rectangle = new Rectangle(12,34);
System.out.println("
Area of Rectangle: "+
rectangle.calculateArea());
Triangle triangle = new Triangle(5,9);
System.out.println("
Area of Triangle: "+
triangle.calculateArea());
}
}
Output:
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Microsoft Visual Basic 2017 For Windows Web And Database Applications

Authors: Corinne Hoisington

1st Edition

1337102113, 978-1337102117

More Books

Students also viewed these Databases questions