源程序:
import java.util.*;
interface Volume{
public double Volume();
}
interface Area{
public double Area();
}
public class yuanzhui extends Object implements Area,Volume
{
private double radius;
private double length;
private double height;
public interface Volume{
public double volume();
}
public interface Area{
public double Area();
interface Volume{
public double Volume();
}
interface Area{
public double Area();
}
public class yuanzhui extends Object implements Area,Volume
{
private double radius;
private double length;
private double height;
public interface Volume{
public double volume();
}
public interface Area{
public double Area();
}
public yuanzhui(double radius,double length,double height)
{
this.radius = radius;
this.length = length;
this.height = height;
}
public yuanzhui()
{
this(0,0,0);
}
public double Area() //计算圆锥的表面积,实现Area接口中的抽象方法
{
return Math.PI*this.radius*this.length+Math.PI*this.radius* this.radius ;
}
public double Volume() //计算圆锥的体积,实现Volume接口中的抽象方法
{
return Math.PI * this.radius * this.radius * this.height/3;
}
public String toString()
{
return "一个圆锥,半径"+this.radius+",高"+this.height+",斜边,"+this.length+"表面积为"+this.Area()+",体积为"+this.Volume();
}
public static void main(String args[])
{yuanzhui x=new yuanzhui(12,6,5);
System.out.println(x);
yuanzhui y=new yuanzhui(10,7,8);
System.out.println(y);
if(x.Volume()>y.Volume())
System.out.println("第一个圆锥的体积大于第二个圆锥的体积");
else
System.out.println("第一个圆锥的体积小于第二个圆锥的体积");
}
}
public yuanzhui(double radius,double length,double height)
{
this.radius = radius;
this.length = length;
this.height = height;
}
public yuanzhui()
{
this(0,0,0);
}
public double Area() //计算圆锥的表面积,实现Area接口中的抽象方法
{
return Math.PI*this.radius*this.length+Math.PI*this.radius* this.radius ;
}
public double Volume() //计算圆锥的体积,实现Volume接口中的抽象方法
{
return Math.PI * this.radius * this.radius * this.height/3;
}
public String toString()
{
return "一个圆锥,半径"+this.radius+",高"+this.height+",斜边,"+this.length+"表面积为"+this.Area()+",体积为"+this.Volume();
}
public static void main(String args[])
{yuanzhui x=new yuanzhui(12,6,5);
System.out.println(x);
yuanzhui y=new yuanzhui(10,7,8);
System.out.println(y);
if(x.Volume()>y.Volume())
System.out.println("第一个圆锥的体积大于第二个圆锥的体积");
else
System.out.println("第一个圆锥的体积小于第二个圆锥的体积");
}
}
运行结果:
一个圆锥,半径12.0,高5.0,斜边,6.0表面积为678.5840131753953,体积为753.9822368615504
一个圆锥,半径10.0,高8.0,斜边,7.0表面积为534.0707511102648,体积为837.7580409572782
第一个圆锥的体积小于第二个圆锥的体积
一个圆锥,半径10.0,高8.0,斜边,7.0表面积为534.0707511102648,体积为837.7580409572782
第一个圆锥的体积小于第二个圆锥的体积
实验心得:需要注意接口的建立以及引用;最后两个圆锥体积的大小比较也要合理设计。