Java Interface: Shape with the getArea() method, implement the Shape interface
Java Interface: Exercise-1 with Solution
Write a Java program to create an interface Shape with the getArea() method. Create three classes Rectangle, Circle, and Triangle that implement the Shape interface. Implement the getArea() method for each of the three classes.
Sample Solution:
Java Code:
// Shape.java
// Interface Shape
public interface Shape {
double getArea();
}
// Rectangle.java
// Class Rectangle that implements the Shape interface
public class Rectangle implements Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double getArea() {
return length * width;
}
}
// Circle.java
// Class Circle that implements the Shape interface
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
// Triangle.java
// Class Triangle that implements the Shape interface
public class Triangle implements Shape {
private double base;
private double height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
@Override
public double getArea() {
return 0.5 * base * height;
}
}
// Main.java
// Main class that creates objects of Rectangle, Circle, and Triangle
public class Main {
public static void main(String[] args) {
//length=10,width=12
Rectangle rectangle = new Rectangle(10, 12);
//radius=3
Circle circle = new Circle(3);
//base=4,height=6
Triangle triangle = new Triangle(4, 6);
System.out.println("Area of the Rectangle: " + rectangle.getArea());
System.out.println("Area of the Circle: " + circle.getArea());
System.out.println("Area of the Triangle: " + triangle.getArea());
}
}
Sample Output:
Area of the Rectangle: 120.0 Area of the Circle: 28.274333882308138 Area of the Triangle: 12.0
Flowchart of Interface Shape:

Flowchart of Class Rectangle:

Flowchart of Class Circle:

Flowchart of Class Triangle:

Flowchart of Main Class:

Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Java Interface Exercises Home.
Next: Implement Animal interface with a method bark().
What is the difficulty level of this exercise?
Java: Tips of the Day
How do I remove repeated elements from ArrayList?
If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:
Set<String> set = new HashSet<>(yourList); yourList.clear(); yourList.addAll(set);
Of course, this destroys the ordering of the elements in the ArrayList.
Ref: https://bit.ly/3bYIjNC
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook