Advertisement
brandblox

Java lab 1 (16/10/2023)

Oct 16th, 2023
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1.  
  2. abstract class Shape {
  3.     abstract void areaOfRect(int x, int y);
  4.     abstract void areaOfCircle(double r);
  5.     abstract void areaOfSquare(int x);
  6. }
  7.  
  8. class Rectangle extends Shape {
  9.  
  10.     void areaOfRect(int x, int y) {
  11.         System.out.println("Area of Rectangle: " + (x * y));
  12.     }
  13.     void areaOfCircle(double r){}
  14.  
  15.     void areaOfSquare(int x) {}
  16. }
  17. class Circle extends Shape {
  18.  
  19.     void areaOfRect(int x, int y){}
  20.  
  21.     void areaOfCircle(double r) {
  22.         System.out.println("Area of Circle: " + (Math.PI * r * r));
  23.     }
  24.  
  25.     void areaOfSquare(int x) {}
  26. }
  27.  
  28. // Concrete class Square extending Shape
  29. class Square extends Shape {
  30.  
  31.     void areaOfRect(int x, int y) {}
  32.  
  33.     void areaOfCircle(double r) {}
  34.  
  35.     void areaOfSquare(int x) {
  36.         System.out.println("Area of Square: " + (x * x));
  37.     }
  38. }
  39. public class ShapeDemo {
  40.     public static void main(String[] args) {
  41.         Rectangle rectangle = new Rectangle();
  42.         Circle circle = new Circle();
  43.         Square square = new Square();
  44.         rectangle.areaOfRect(5, 10);
  45.         circle.areaOfCircle(3.5);
  46.         square.areaOfSquare(4);
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement