📄 solid.java
字号:
/** * demonstration of abstract classes */public abstract class Solid { public abstract double surfaceArea() ; public abstract double volume() ; } // end of class Solidclass Sphere extends Solid { private double radius ; public Sphere() { radius = 0.0 ; } // end of constructor method public Sphere(double r) { radius = r ; } // end of constructor method public double volume() { return 4.0/3.0 * Math.PI * radius * radius * radius ; } // end of method volume public double surfaceArea() { return 4 * Math.PI * radius * radius ; } // end of method surfaceArea public String toString() { return "Sphere radius " + radius + " surface area " + surfaceArea() + " volume " + volume() ; } // end of method toString } // end of class Sphereclass Cuboid extends Solid { private double height, breadth, length ; public Cuboid() { length = 0.0 ; breadth = 0.0 ; height = 0.0 ; } // end of constructor method public Cuboid(double l, double b, double h) { length = l ; breadth = b ; height = h ; } // end of constructor method public double volume() { return length * breadth * height ; } // end of method volume public double surfaceArea() { return (2 * length * breadth) + (2 * length * height) + (2 * breadth * height) ; } // end of method surfaceArea public String toString() { return "Cuboid length " + length + " breadth " + breadth + " height " + height + " surface area " + surfaceArea() + " volume " + volume() ; } // end of method toString } // end of class Cuboidclass Cylinder extends Solid { private double height, radius ; public Cylinder() { radius = 0.0 ; height = 0.0 ; } // end of constructor method public Cylinder(double r, double h) { radius = r ; height = h ; } // end of constructor method public double volume() { return Math.PI * radius * radius * height ; } // end of method volume public double surfaceArea() { return (2 * Math.PI * radius * radius) + (2 * Math.PI * radius * height) ; } // end of method surfaceArea public String toString() { return "Cylinder height " + height + " radius " + radius + " surface area " + surfaceArea() + " volume " + volume() ; } // end of method toString } // end of class Cylinder
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -