Calculate the surface area and volume of box and pyramid
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
Calculate the surface area and volume of the following solid figure.
When viewed from the graph, its common attributes can be extracted into the parent class Rect: length: l width: h height: z
In the parent class Rect, define the method length() to find the perimeter of the bottom surface and the method area() to find the area of the bottom surface.
Define the subclass cube class Cubic of the parent class Rect, and calculate the surface area and volume of the cube. Where surface area() overrides the method of the parent class.
Define Pyramid, a subclass of Rect, and calculate the surface area and volume of the Pyramid. Where surface area() overrides the method of the parent class.
Input the length (l), width (h) and height (z) data of the solid figure, and output the surface area, volume of the box, surface area and volume of the pyramid respectively.
Input
Input multiple rows of numerical data (double);
Three values in each row, representing l h z
If there is a non positive number in the input data, it does not represent any graph, and the surface area and volume are both 0.
Output
The number of lines corresponds to the input, and the value is the surface area of cuboid cuboid volume of quadrangular pyramid surface area of quadrangular pyramid volume (there is a space in the middle as the interval, and the value retains two decimal places)
Sample Input
1 2 3
0 2 3
-1 2 3
3 4 5
Sample Output
22.00 6.00 11.25 2.00
0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00
94.00 60.00 49.04 20.00
Hint
Pyramid formula: V=1/3Sh, S -- bottom area h -- height
Source
zhouxq
import java.text.DecimalFormat;
import java.util.Scanner;
class Rect{
double l,h,z;
Rect(double l,double h,double z){
if(l > 0 && h > 0 && z > 0) {
this.l = l;
this.h = h;
this.z = z;
}
else {
l = h = z = 0;
}
}
public double length() {
return (l+h)*2;
}
public double area() {
return l*h;
}
}
//Subclass inheritance
class Cubic extends Rect{
Cubic(double a,double b,double c){
super(a, b, c);
}
//Surface area of cuboid
public double area() {
return 2*super.area()+length()*z;
}
//New method of cuboid volume
public double v() {
return super.area()*z;
}
}
//Pyramid class inherits parent class
class Pyramid extends Rect{
Pyramid(double l,double h,double z){
super(l, h, z);
}
//Surface area of pyramid four side areas plus one base area rewrite parent class method
public double area() {
double s1 = Math.sqrt((l/2)*(l/2) + z*z); //Height of side area 1
double s2 = Math.sqrt((h/2)*(h/2) + z*z); //Height of side area
return h*s1+l*s2+super.area();
}
//A new method of adding volume to pyramid
public double v() {
return 1.0*super.area()*z/3;
}
}
public class Main {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
while(input.hasNext()) {
double x = input.nextDouble();
double y = input.nextDouble();
double z = input.nextDouble();
Cubic cc = new Cubic(x,y,z);
Pyramid pp = new Pyramid(x,y,z);
double s1 = cc.area();
double v1 = cc.v();
double s2 = pp.area();
double v2 = pp.v();
System.out.println(df.format(s1)+" "+df.format(v1)+" "+df.format(s2)+" "+df.format(v2));
}
}
}