Advertisement
iNoobAvicena

Inheritance Pemlan

Apr 2nd, 2020
201
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. public class Solution {
  4.     public static void main(String[] args) {
  5.         Scanner in = new Scanner(System.in);
  6.         BidangDatar a = new BidangDatar();
  7.         Segitiga b = new Segitiga();
  8.  
  9.         System.out.print("Input Bidang Datar : ");
  10.         String nama = in.nextLine();
  11.         System.out.print("Input panjang : ");
  12.         int panjang = in.nextInt();
  13.         System.out.print("Input lebar : ");
  14.         int lebar = in.nextInt();
  15.  
  16.         if (nama.equalsIgnoreCase("segitiga")) {
  17.             b.getLuas(panjang, lebar);
  18.         } else {
  19.             a.getLuas(panjang, lebar);
  20.         }
  21.     }
  22. }
  23.  
  24. public class BidangDatar {
  25.     private String nama;
  26.     private int panjang, lebar;
  27.  
  28.     float getLuas(int panjang, int lebar) {
  29.         float luas = panjang * lebar;
  30.         System.out.println("Luas Persegi Panjang : " + luas);
  31.         return luas;
  32.     }
  33. }
  34.  
  35. public class Segitiga extends BidangDatar {
  36.     @Override
  37.     float getLuas(int panjang, int lebar) {
  38.         float luas =(float) ((0.5) * panjang * lebar);
  39.         System.out.println("Luas Segitiga : "+luas);
  40.         return luas;
  41.     }
  42. }
Advertisement
RAW Paste Data Copied
Advertisement