Java 代码示例:计算圆桌和矩形桌的面积
public class Test { public static void main(String[] args) { int r = 50; int width = 40; int len = 60;
double roundArea = 0;
double rectArea = 0;
TableInfo roundTable = new TableInfo('Round', 4, 100);
TableInfo rectTable = new TableInfo('Rect', 4, 100);
roundArea = roundTable.tableArea(r); // 计算圆桌面积
rectArea = rectTable.tableArea(width, len); // 计算矩形桌面积
System.out.println('Round Table Area =' + roundArea);
System.out.println('Rect Table Area=' + rectArea);
}
}
class TableInfo { String shape; int legs; int hight;
public TableInfo(String shape, int legs, int hight) {
this.shape = shape;
this.legs = legs;
this.hight = hight;
}
public void print() {
System.out.println('Shape=' + shape);
}
// 计算圆桌面积的方法
public double tableArea(int r) {
return 3.14 * r * r;
}
// 计算矩形桌面积的方法
public double tableArea(int a, int b) {
return a * b;
}
}
原文地址: https://www.cveoy.top/t/topic/nYl0 著作权归作者所有。请勿转载和采集!