JAVA   28

Untitled

Guest on 12th January 2022 07:41:42 AM

  1. import java.util.Scanner;
  2.  
  3. // define abstract class Shape.
  4. abstract class Shape
  5. {
  6.     // declare ineteger class variables.
  7.     int length;
  8.     int breadth;
  9.     int radius;
  10.    
  11.     // declare abstract function printArea().
  12.     public abstract void printArea();
  13. }
  14.  
  15. // define Rectangle class that extends Shape class.
  16. class Rectangle extends  Shape
  17. {
  18.     // define printArea() method.
  19.     public void printArea()
  20.     {
  21.         double area;
  22.        
  23.         Scanner sc = new Scanner(System.in);
  24.        
  25.         // input length and breadth of rectangle from the user.
  26.         System.out.print("Enter length of rectangle: ");
  27.         length = sc.nextInt();
  28.        
  29.         System.out.print("Enter breadth of rectangle: ");
  30.         breadth = sc.nextInt();
  31.        
  32.         // find area of rectangle using length * breadth.
  33.         area = length * breadth;
  34.        
  35.         // display area of the rectangle.
  36.         System.out.println("Area of rectangle: "+area);
  37.     }
  38. }
  39.  
  40. // define Triangle class that extends Shape class.
  41. class Triangle extends  Shape
  42. {
  43.     // define printArea() method.
  44.     public void printArea()
  45.     {
  46.         double area;
  47.        
  48.         Scanner sc = new Scanner(System.in);
  49.        
  50.         // input base of triangle.
  51.         System.out.print("\nEnter base of triangle: ");
  52.         breadth = sc.nextInt();
  53.        
  54.         // input height of triangle.
  55.         System.out.print("Enter height of triangle: ");
  56.         length = sc.nextInt();
  57.        
  58.         // find area of triangle using 1/2 * base * height.
  59.         area = 0.5 * length * breadth;
  60.        
  61.         // display area of triangle.
  62.         System.out.println("Area of triangle: "+area);
  63.     }
  64. }
  65.  
  66. // define Circle class that extends Shape class.
  67. class Circle extends  Shape
  68. {
  69.     // define printArea().
  70.     public void printArea()
  71.     {
  72.         double area;
  73.        
  74.         Scanner sc = new Scanner(System.in);
  75.        
  76.         // input radius of the circle from the user.
  77.         System.out.print("\nEnter radius of circle: ");
  78.         radius = sc.nextInt();
  79.        
  80.         // find area of circle using pi * radius ^ 2.
  81.         area =  3.14 * radius * radius;
  82.        
  83.         // display area of the circle.
  84.         System.out.println("Area of circle: "+area);
  85.     }
  86. }
  87.  
  88.  
  89. // Main class.
  90. public class Main
  91. {
  92.     // define main() method.
  93.         public static void main(String[] args) {
  94.  
  95.     // create Object of Rectangle class.
  96.     Rectangle obj1 = new Rectangle();
  97.     // call printArea() method of Rectangle class.
  98.     obj1.printArea();
  99.    
  100.     // create Object of Triangle class.
  101.     Triangle obj2 = new Triangle();
  102.     // call printArea() method of Triangle class.
  103.     obj2.printArea();
  104.    
  105.     // create Object of Circle class.
  106.     Circle obj3 = new Circle();
  107.     // call printArea() method of Circle class.
  108.     obj3.printArea();
  109.    
  110.         }
  111. }

Raw Paste


Login or Register to edit or fork this paste. It's free.