Advertisement
deyanmalinov

2. Method Overriding

Jul 2nd, 2020
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. package DPM;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         int lines = Integer.parseInt(scan.nextLine());
  7.         for (int i = 0; i < lines; i++) {
  8.             String[] line = scan.nextLine().split(" ");
  9.             if (line.length > 1){
  10.                 double a = Double.parseDouble(line[0]);
  11.                 double b = Double.parseDouble(line[1]);
  12.                 Rectangle rectangle = new Rectangle(a,b);
  13.                 System.out.println(rectangle.area());
  14.             }else {
  15.                 double a = Double.parseDouble(line[0]);
  16.                 Square square = new Square(a);
  17.                 System.out.println(a * a);
  18.             }
  19.         }
  20.     }
  21. }-------------------------------------
  22. package DPM;
  23. public class Rectangle {
  24.     protected double a;
  25.     protected double b;
  26.     protected Rectangle(double a){
  27.         this.a = a;
  28.     }
  29.     protected Rectangle(double a, double b){
  30.         this.a = a;
  31.         this.b = b;
  32.     }
  33.     protected double area(){
  34.         return this.a*this.b;
  35.     }
  36. }-------------------------------------
  37. package DPM;
  38. public class Square extends Rectangle{
  39.     private double a;
  40.     public Square(double a) {
  41.         super(a);
  42.     }
  43.     @Override
  44.     protected double area() {
  45.         return this.a * this.a;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement