Advertisement
Eternoseeker

Ass 3 SHAPE

Mar 29th, 2023
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | Source Code | 0 0
  1. import java.util.Scanner;
  2.  
  3. abstract class Shape{
  4.     public float area, perimeter;
  5.     abstract public void area();
  6.     abstract public void perimeter();
  7. }
  8.  
  9. class Rectangle extends Shape{
  10.     private int length;
  11.     private int breadth;
  12.     Scanner sc = new Scanner(System.in);
  13.  
  14.     public void area(){
  15.         System.out.println("Enter the length and breadth of rectangle");
  16.         length = sc.nextInt();
  17.         breadth = sc.nextInt();
  18.         area = length * breadth;
  19.         System.out.println("Length: " + length + " Breadth: " + breadth);  
  20.         System.out.println("The area of rectangle is:" + area);
  21.     }
  22.  
  23.     public void perimeter(){
  24.         System.out.println("Enter the length and breadth of rectangle");
  25.         length = sc.nextInt();
  26.         breadth = sc.nextInt();
  27.         perimeter = 2 * (length + breadth);
  28.         System.out.println("The perimeter is: " + perimeter);
  29.     }
  30. }
  31.  
  32. class Triangle extends Shape{
  33.     private float base;
  34.     private float height;
  35.    
  36.     Scanner sc = new Scanner(System.in);
  37.  
  38.     public void area(){
  39.         System.out.println("Enter the base and height of triangle: ");
  40.         base = sc.nextInt();
  41.         height = sc.nextInt();
  42.         area = (base * height/2);
  43.         System.out.println("Base: " + base + " Height: " + height);
  44.         System.out.println("The area of Triangle is:" + area);
  45.     }
  46.  
  47.     public void perimeter(){
  48.         System.out.println("Enter the base and height of triangle: ");
  49.         base = sc.nextInt();
  50.         height = sc.nextInt();
  51.     }
  52. }
  53.  
  54. class Circle extends Shape{
  55.     private int radius;
  56.    
  57.     Scanner sc = new Scanner(System.in);
  58.  
  59.     public void area(){
  60.         System.out.println("Enter the radius of circle:");
  61.         radius = sc.nextInt();
  62.         area= (22/7) * radius * radius;
  63.         System.out.println("Radius: " + radius);   
  64.         System.out.println("The area of Circle is:" + area);
  65.     }
  66.  
  67.     public void perimeter(){
  68.         System.out.println("Enter the radius of circle:");
  69.         radius = sc.nextInt();
  70.         perimeter = 2 * (22/7) * radius;
  71.         System.out.println("The perimeter is: " + perimeter);
  72.     }
  73. }
  74.  
  75. public class shapearea
  76. {
  77.  public static void main(String[] args)
  78. {
  79.     Rectangle r = new Rectangle();
  80.     r.area();
  81.      Triangle t = new Triangle();
  82.      t.area();
  83.     Circle c = new Circle();
  84.     c.area();
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement