keymasterviriya1150

Untitled

Feb 17th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. //ข้อสอบOOPเรื่องการสืบทอด by อามมี่หัวเสีย
  2. import java.util.ArrayList;
  3.  
  4. public class Shape {
  5.     private String name;
  6.     public Shape (String name)
  7.     {
  8.         this.name = name;
  9.     }
  10.     public String getName() {
  11.         return name;
  12.     }
  13.     public double getArea(){
  14.         return 0;
  15.     }
  16. }
  17.  
  18. class Rectangular extends Shape {
  19.    
  20.      double Width;
  21.      double Height;
  22.      double Rec;
  23.  
  24.     public Rectangular(double Width, double Height,String name) {
  25.         super(name);
  26.         this.Width = Width;
  27.         this.Height = Height;
  28.         Rec=this.Width*Height;
  29.     }
  30.    
  31.      @Override
  32.    public double getArea()
  33.    {
  34.        return  Rec;
  35.    }
  36.    
  37.    
  38. }
  39. class Circle extends Shape
  40. {
  41.     double r;
  42.     double Rec;
  43.  
  44.     public Circle(double r,String name) {
  45.         super(name);
  46.         this.r = r;
  47.         this.Rec = r*r*Math.PI;
  48.     }
  49.     public double getArea()
  50.    {
  51.        return  Rec;
  52.    }
  53.  
  54.  
  55.    
  56. }
  57.        
  58. class Main1
  59. {
  60.     public static void main(String[] args) {
  61.         ArrayList<Shape> A=new ArrayList<Shape>();
  62.    
  63.         A.add(new Rectangular(5,10,"A"));
  64.         A.add(new Circle(3,"B"));
  65.         A.add(new Circle(4,"C"));
  66.         System.out.println(findBiggest(A).getName());
  67.                
  68.        
  69.        
  70.        
  71.     }
  72.     public static Shape findBiggest(ArrayList<Shape>  S)
  73.     {
  74.         double max = S.get(0).getArea();
  75.         int temp = 0;
  76.         for (int i = 0; i < S.size(); i++) {
  77.             if(max < S.get(i).getArea() )
  78.             {
  79.                 max = S.get(i).getArea();
  80.                 temp=i;
  81.             }
  82.         }
  83.         return S.get(temp);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment