题解 | #根据周长求面积#
根据周长求面积
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......
}