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

根据周长求面积

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

import java.util.Scanner;

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 {

    //write your code here......
    public Circle(double s)//父类
    {
        super(s);
    }
    public double getArea()
    {
        return Math.PI*Math.pow((getS())/(2*Math.PI),2);
    }

}

// 方形
class Square extends Shape implements Area {

    //write your code here......
    public Square(double s)
    {
        super(s);
    }
    public double getArea() {
        return Math.pow((getS()/4),2);
    }

}

全部评论

相关推荐

07-11 13:16
湖南工学院 Java
坚定的芭乐反对画饼_...:谁也不知道,毕竟现在的互联网和十年前已经完全不同了,谁都无法预测未来
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务