java定义一个接口Circlearea,声明一个方法用于计算园的面积,在定义Circle表示园,
java定义一个接口Circlearea,声明一个方法用于计算园的面积,在定义Circle表示园、类Cylinder表示圆柱体,分别去实现这个接口,再编写测试类,创建Ci...
java定义一个接口Circlearea,声明一个方法用于计算园的面积,在定义Circle表示园、类Cylinder表示圆柱体,分别去实现这个接口,再编写测试类,创建Circle对象、Cylinder对象,并运行
展开
2个回答
展开全部
接口Circlearea.java:
package test;
public interface Circlearea {
public static double PI = 3.14;
public double area(double r);
}
圆类Circle.java:
package test;
public class Circle implements Circlearea {
public double r;
public Circle(double r) {
this.r = r;
}
//圆面积
public double area(double r) {
return Circlearea.PI * r * r;
}
}
圆柱体类Cylinder.java:
package test;
public class Cylinder implements Circlearea {
public double r;
public double h;
public Cylinder(double r, double h) {
this.r = r;
this.h = h;
}
//圆柱体底面积
public double area(double r) {
return Circlearea.PI * r * r;
}
//圆柱体表面积
public double area(double r,double h) {
return 2 * area(r) + 2 * Circlearea.PI * r * h;
}
}
测试类Test.java:
package test;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Circle c = new Circle(2.0);
System.out.println("圆的面积为:" + c.area(c.r));
Cylinder cl = new Cylinder(2.0, 3.0);
System.out.println("圆柱体的底面积为:" + cl.area(cl.r));
System.out.println("圆柱体的表面积为:" + cl.area(cl.r, cl.h));
}
}
运行结果:
圆的面积为:12.56
圆柱体的底面积为:12.56
圆柱体的表面积为:62.8
package test;
public interface Circlearea {
public static double PI = 3.14;
public double area(double r);
}
圆类Circle.java:
package test;
public class Circle implements Circlearea {
public double r;
public Circle(double r) {
this.r = r;
}
//圆面积
public double area(double r) {
return Circlearea.PI * r * r;
}
}
圆柱体类Cylinder.java:
package test;
public class Cylinder implements Circlearea {
public double r;
public double h;
public Cylinder(double r, double h) {
this.r = r;
this.h = h;
}
//圆柱体底面积
public double area(double r) {
return Circlearea.PI * r * r;
}
//圆柱体表面积
public double area(double r,double h) {
return 2 * area(r) + 2 * Circlearea.PI * r * h;
}
}
测试类Test.java:
package test;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Circle c = new Circle(2.0);
System.out.println("圆的面积为:" + c.area(c.r));
Cylinder cl = new Cylinder(2.0, 3.0);
System.out.println("圆柱体的底面积为:" + cl.area(cl.r));
System.out.println("圆柱体的表面积为:" + cl.area(cl.r, cl.h));
}
}
运行结果:
圆的面积为:12.56
圆柱体的底面积为:12.56
圆柱体的表面积为:62.8
展开全部
1、public interface Circlearea {
public double getArea();
}
2、public class Circle implements Circlearea{
private double r;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public Circle(double r){
super();
this.r = r;
}
public double getArea() {
double area = Math.PI*r*r;
return area;
}
}
3、public class Cylinder implements Circlearea{
private double r;
private double h;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
public Cylinder(double r, double h) {
super();
this.r = r;
this.h = h;
}
public double getArea() {
double area = Math.PI*r*r;
return area;
}
}
4、public class MethodUtil {
public static void main(String[] args){
Circle circle = new Circle(4.0);
Cylinder cylinder = new Cylinder(4.0, 5.0);
System.out.println("圆的面积:"+circle.getArea());
double volume = cylinder.getArea()*cylinder.getH();
System.out.println("圆柱体的体积:"+volume);
}
}
public double getArea();
}
2、public class Circle implements Circlearea{
private double r;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public Circle(double r){
super();
this.r = r;
}
public double getArea() {
double area = Math.PI*r*r;
return area;
}
}
3、public class Cylinder implements Circlearea{
private double r;
private double h;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
public Cylinder(double r, double h) {
super();
this.r = r;
this.h = h;
}
public double getArea() {
double area = Math.PI*r*r;
return area;
}
}
4、public class MethodUtil {
public static void main(String[] args){
Circle circle = new Circle(4.0);
Cylinder cylinder = new Cylinder(4.0, 5.0);
System.out.println("圆的面积:"+circle.getArea());
double volume = cylinder.getArea()*cylinder.getH();
System.out.println("圆柱体的体积:"+volume);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询