Java实现:设计一个名为Shape的类,一个float getArea(int r)方法,能够计算并返回半径为r的圆形面积;一个float getArea(int width, int height)方法,能够计算并返回一个宽度为width,高度为height的矩形面积(利用Math.PI获取π值)
例如:
测试 | Result |
---|---|
Shape shape=new Shape(); System.out.println(shape.getArea(5)); System.out.println(shape.getArea(3,4)); | 78.53982 12.0 |
import java.util.*;
public class Main {
public static void main(String args[]) {
Shape shape=new Shape();
System.out.print(shape.getArea(5));
System.out.print(shape.getArea(4,3));
}
}
public class Shape {
float getArea(int r) {
return (float)(Math.PI*r*r);
}
float getArea(int width, int height) {
return (float)(width*height);
}
}