不是很会。。。
package Javatest;
import java.util.Scanner;
//@author: cc
public class Cone implements Comparable<Cone>,Area, Volume{
//V = 1 / 3 * pi * r^2 * h
private double radius;
private double height;
public Cone(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
@Override
public double volume() {
return 1.0 / 3.0 * Math.PI * radius * radius * height;
}
public int compareTo(Cone c){
if(this.volume()>c.volume())
return 1;
else if(this.volume()<c.volume())
return -1;
else
return 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入半径和高度");
double radius = sc.nextDouble();
double height = sc.nextDouble();
Cone cone = new Cone(radius, height);
System.out.println("面积为:" + cone.area());
System.out.println("体积为:" + cone.volume());
System.out.println("请输入第二个的半径和高度");
Cone cone2 = new Cone(sc.nextDouble(), sc.nextDouble());
if(cone.compareTo(cone2) > 0){
System.out.println("第一个圆锥体积大 " + "体积是 " + cone.volume());
}else if(cone.compareTo(cone2) < 0){
System.out.println("第二个圆锥体积大 " + "体积是 "+ cone2.volume());
}else{
System.out.println("两个圆锥体积相等 " + "体积是 " + cone.volume());
}
}
}
interface Area {//计算面积
public abstract double area();
}
interface Volume {
public abstract double volume();
}