Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Program using the INTERFACE concept in Java.

import java.util.Scanner;

interface Shape {
double getArea();
}

class Triangle implements Shape {


double base, height;

public Triangle(double b, double h) {


base = b;
height = h;
}

public double getArea() {


return 0.5 * base * height;
}
}

public class Main {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the base of the triangle: ");
double base = input.nextDouble();
System.out.print("Enter the height of the triangle: ");
double height = input.nextDouble();

Triangle t = new Triangle(base, height);


System.out.println("The area of the triangle is " + t.getArea());
}
}

You might also like