Project Description
For this week's project, you'll create a shape hierarchy using inheritance. This will help reinforce the concepts of inheritance, method overriding, and the use of the super keyword.
Requirements:
- Create a base class called
Shape with methods for calculating area and perimeter
- Create subclasses for at least three different shapes (e.g., Circle, Rectangle, Triangle)
- Override the area and perimeter calculation methods in each subclass
- Use the
super keyword to call the superclass constructor
- Implement a method to display information about each shape
- In the main method, create instances of different shapes and demonstrate polymorphism
Here's a template to get you started:
abstract class Shape {
String name;
Shape(String name) {
this.name = name;
}
abstract double calculateArea();
abstract double calculatePerimeter();
void displayInfo() {
System.out.println("Shape: " + name);
System.out.println("Area: " + calculateArea());
System.out.println("Perimeter: " + calculatePerimeter());
}
}
class Circle extends Shape {
// Implement Circle class
}
class Rectangle extends Shape {
// Implement Rectangle class
}
class Triangle extends Shape {
// Implement Triangle class
}
public class Main {
public static void main(String[] args) {
// Create instances of different shapes
// Call methods to demonstrate inheritance and polymorphism
}
}
Implement the missing parts of the Shape hierarchy. Create instances of different shapes, calculate their areas and perimeters, and display their information. Use the online compiler to write and test your code. Once you're satisfied with your program, paste a sample output showing the information for at least three different shapes in the autograder to check your work.