Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ข้อสอบOOPเรื่องการสืบทอด by อามมี่หัวเสีย
- import java.util.ArrayList;
- public class Shape {
- private String name;
- public Shape (String name)
- {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public double getArea(){
- return 0;
- }
- }
- class Rectangular extends Shape {
- double Width;
- double Height;
- double Rec;
- public Rectangular(double Width, double Height,String name) {
- super(name);
- this.Width = Width;
- this.Height = Height;
- Rec=this.Width*Height;
- }
- @Override
- public double getArea()
- {
- return Rec;
- }
- }
- class Circle extends Shape
- {
- double r;
- double Rec;
- public Circle(double r,String name) {
- super(name);
- this.r = r;
- this.Rec = r*r*Math.PI;
- }
- public double getArea()
- {
- return Rec;
- }
- }
- class Main1
- {
- public static void main(String[] args) {
- ArrayList<Shape> A=new ArrayList<Shape>();
- A.add(new Rectangular(5,10,"A"));
- A.add(new Circle(3,"B"));
- A.add(new Circle(4,"C"));
- System.out.println(findBiggest(A).getName());
- }
- public static Shape findBiggest(ArrayList<Shape> S)
- {
- double max = S.get(0).getArea();
- int temp = 0;
- for (int i = 0; i < S.size(); i++) {
- if(max < S.get(i).getArea() )
- {
- max = S.get(i).getArea();
- temp=i;
- }
- }
- return S.get(temp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment