修改好了,文件名为Testshape.java
abstract class shape{
String type;
public shape(String type){};
public abstract double getsurfaceArea();
public abstract double getVolume();
public abstract void tostring();
}
class sphere extends shape{
double r;
public sphere(double r){
super("sphere");
this.r=r;
}
public double getsurfaceArea(){
return 4*Math.PI*r*r;
}
public double getVolume(){
return 4/3*Math.PI*r*r*r;
}
public void tostring(){
System.out.println("This is a sphere with radius"+r+",surface area"+getsurfaceArea()+"and volume"+getVolume());
}
}
class cone extends shape{
double r,h;
public cone(double r,double h){
super("cone");
this.r=r;
this.h=h;
}
public double getsurfaceArea(){
return Math.PI*r*(r+Math.sqrt(r*r+h+h));
}
public double getVolume(){
return Math.PI*r*r*h/3;
}
public void tostring(){
System.out.println("This is a cone with height"+h+"radius"+r+",surface area"+getsurfaceArea()+"and volume"+getVolume());
}
}
class cylinder extends shape{
double r,h;
public cylinder(double r,double h){
super("cylinder");
this.r=r;
this.h=h;
}
public double getsurfaceArea(){
return 2*Math.PI*r*(r+h);
}
public double getVolume(){
return Math.PI*r*r*h;
}
public void tostring(){
System.out.println("This is a cylinder with height"+h+"radius"+r+",surface area"+getsurfaceArea()+"and volume"+getVolume());
}
}
public class Testshape{
public static void main(String args[]){
int i;
shape d[]={new sphere(10d),new cone(10d,8d),new cylinder(10d,8d)};
for(i=0;i<3;i++)
d[i].tostring();
}
}
public abstract class shape{
String type;
public shape(String type){};
public abstract double getsurfaceArea();
public abstract double getVolume();
public void tostring(){
}
}
class sphere extends shape{
double r;
public sphere(double r){
super.type="sphere";
this.r=r;
}
public double getsurfaceArea(double r){
return 4*Math.PI*r*r;
}
public double getVolume(double r){
return 4/3*Math.PI*r*r*r;
}
public void tostring(){
System.out.println("This is a sphere with radius"+r+",surface area"+getsurfaceArea(r)+"and volume"+getVolume(r));
}
}
class cone extends shape{
double r,h;
public cone(double r,double h){
super.type="cone";
this.r=r;
this.h=h;
}
public double getsurfaceArea(double r,double h){
return Math.PI*r*(r+Math.sqrt(r*r+h+h));
}
public double getVolume(double r,double h){
return Math.PI*r*r*h/3;
}
public void tostring(){
System.out.println("This is a cone with height"+h+"radius"+r+",surface area"+getsurfaceArea(r,h)+"and volume"+getVolume(r,h));
}
}
class cylinder extends shape{
double r,h;
public cylinder(double r,double h){
super.type="cylinder";
this.r=r;
this.h=h;
}
public double getsurfaceArea(double r,double h){
return 2*Math.PI*r*(r+h);
}
public double getVolume(double r,double h){
return Math.PI*r*r*h;
}
public void tostring(){
System.out.println("This is a cylinder with height"+h+"radius"+r+",surface area"+getsurfaceArea(r,h)+"and volume"+getVolume(r,h));
}
}
public class Testshape{
public static void main(String args[]){
int i;
shape d[]={new sphere(10d),new cone(10d,8d),new cylinder(10d,8d)};
for(i=0;i<3;i++)
d[i].tostring();
}
}
abstract class shape{
String type;
public abstract double getsurfaceArea(double r,double h);
public abstract double getVolume(double r,double h);
}
class sphere extends shape{
double r,h;
public sphere(double r,double h){
super.type="sphere";
this.r=r;
}
public double getsurfaceArea(double r,double h){
return 4*Math.PI*r*r;
}
public double getVolume(double r,double h){
return 4/3*Math.PI*r*r*r;
}
public void tostring(){
System.out.println("This is a sphere with radius"+r+",surface area"+getsurfaceArea(r,h)+"and volume"+getVolume(r,h));
}
}
class cone extends shape{
double r,h;
public cone(double r,double h){
super.type="cone";
this.r=r;
this.h=h;
}
public double getsurfaceArea(double r,double h){
return Math.PI*r*(r+Math.sqrt(r*r+h+h));
}
public double getVolume(double r,double h){
return Math.PI*r*r*h/3;
}
public void tostring(){
System.out.println("This is a cone with height"+h+"radius"+r+",surface area"+getsurfaceArea(r,h)+"and volume"+getVolume(r,h));
}
}
class cylinder extends shape{
double r,h;
public cylinder(double r,double h){
super.type="cylinder";
this.r=r;
this.h=h;
}
public double getsurfaceArea(double r,double h){
return 2*Math.PI*r*(r+h);
}
public double getVolume(double r,double h){
return Math.PI*r*r*h;
}
public void tostring(){
System.out.println("This is a cylinder with height"+h+"radius"+r+",surface area"+getsurfaceArea(r,h)+"and volume"+getVolume(r,h));
}
}
public class Testshape{
public static void main(String args[]){
int i;
sphere a1 = new sphere(10d,1d);
a1.tostring();
cone a2 = new cone(10d,8d);
a2.tostring();
cylinder a3 = new cylinder(10d,8d);
a3.tostring();
}
}