[java]代码库
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double length = scan.nextDouble();
double wide = scan.nextDouble();
Rectangle r = new Rectangle(length,wide);
System.out.printf("%.2f ",r.getPerimeter());
System.out.printf("%.2f",r.getArea());
System.out.println();
length = scan.nextDouble();
wide = scan.nextDouble();
double height = scan.nextDouble();
Cuboid c = new Cuboid (length, wide, height);
System.out.printf("%.2f ",c.getPerimeter());
System.out.printf("%.2f ",c.getArea());
System.out.printf("%.2f",c.getVolume());
scan.close();
}
}
class Rectangle {
private double length;
private double wide;
public Rectangle(double len, double wi) {
length = len;
wide = wi;
}
public double getLength() {
return length;
}
public double getWide() {
return wide;
}
public double getPerimeter() {
return 2*(length + wide);
}
public double getArea() {
return length*wide;
}
}
class Cuboid extends Rectangle{
private double height;
public Cuboid(double len, double wi, double hei) {
super(len, wi);
this.height = hei;
}
public double getPerimeter() {
return 4*(super.getLength() + super.getWide() + height);
}
public double getArea() {
return 2*(super.getLength()*super.getWide() + super.getLength()*height + super.getWide()*height);
}
public double getVolume() {
return super.getLength()*super.getWide()*height;
}
}