Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- abstract class Shape{
- public float area, perimeter;
- abstract public void area();
- abstract public void perimeter();
- }
- class Rectangle extends Shape{
- private int length;
- private int breadth;
- Scanner sc = new Scanner(System.in);
- public void area(){
- System.out.println("Enter the length and breadth of rectangle");
- length = sc.nextInt();
- breadth = sc.nextInt();
- area = length * breadth;
- System.out.println("Length: " + length + " Breadth: " + breadth);
- System.out.println("The area of rectangle is:" + area);
- }
- public void perimeter(){
- System.out.println("Enter the length and breadth of rectangle");
- length = sc.nextInt();
- breadth = sc.nextInt();
- perimeter = 2 * (length + breadth);
- System.out.println("The perimeter is: " + perimeter);
- }
- }
- class Triangle extends Shape{
- private float base;
- private float height;
- Scanner sc = new Scanner(System.in);
- public void area(){
- System.out.println("Enter the base and height of triangle: ");
- base = sc.nextInt();
- height = sc.nextInt();
- area = (base * height/2);
- System.out.println("Base: " + base + " Height: " + height);
- System.out.println("The area of Triangle is:" + area);
- }
- public void perimeter(){
- System.out.println("Enter the base and height of triangle: ");
- base = sc.nextInt();
- height = sc.nextInt();
- }
- }
- class Circle extends Shape{
- private int radius;
- Scanner sc = new Scanner(System.in);
- public void area(){
- System.out.println("Enter the radius of circle:");
- radius = sc.nextInt();
- area= (22/7) * radius * radius;
- System.out.println("Radius: " + radius);
- System.out.println("The area of Circle is:" + area);
- }
- public void perimeter(){
- System.out.println("Enter the radius of circle:");
- radius = sc.nextInt();
- perimeter = 2 * (22/7) * radius;
- System.out.println("The perimeter is: " + perimeter);
- }
- }
- public class shapearea
- {
- public static void main(String[] args)
- {
- Rectangle r = new Rectangle();
- r.area();
- Triangle t = new Triangle();
- t.area();
- Circle c = new Circle();
- c.area();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement