题解 | #根据周长求面积#

根据周长求面积

http://www.nowcoder.com/practice/f67cf4ac46dc45769aea8d94e0e2f6bd


public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextDouble()) {
            double s = scanner.nextDouble();
            // Circle和Square是需要你定义的类
            System.out.println(String.format("%.3f",new Circle(s).getArea()));
            System.out.println(String.format("%.3f", new Square(s).getArea()));
        }
    }

}

class Shape {

    private double s; // 周长

    public Shape(double s) {
        this.s = s;
    }

    public double getS() {
        return s;
    }

}

interface Area {
    double getArea(); // 面积
}

// 圆形
class Circle extends Shape implements Area {
    
    public Circle(double s){
        super(s);
    }
    
    @Override
    public double getArea(){
        return Math.pow(super.getS()/(2*Math.PI),2)*Math.PI;
    }

    //write your code here......
    

}

// 方形
class Square extends Shape implements Area {
    
    public Square(double s){
        super(s);
    }
    
    @Override
    public double getArea(){
        return Math.pow(super.getS()/4,2);
    }

    //write your code here......
    

}
全部评论

相关推荐

头像
11-07 01:12
重庆大学 Java
精致的小松鼠人狠话不多:签哪了哥
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务