[java]代码库
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double length = scan.nextDouble();
Square s = new Square(length);
System.out.printf("%.2f ",s.getPerimeter());
System.out.printf("%.2f",s.getArea());
System.out.println();
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();
double radius = scan.nextDouble();
Circle c = new Circle (radius);
System.out.printf("%.2f ",c.getPerimeter());
System.out.printf("%.2f ",c.getArea());
scan.close();
}
}
abstract class Shape
{
public abstract double getPerimeter();
public abstract double getArea();
}
class Square extends Shape
{
private double length;
public Square(double l)
{
length = l;
}
public double getLength()
{
return length;
}
public double getPerimeter()
{
return length*4;
}
public double getArea()
{
return length*length;
}
}
class Rectangle extends Square
{
private double wide;
public Rectangle(double l, double w)
{
super(l);
wide = w;
}
public double getPerimeter()
{
return (super.getLength()+wide)*2;
}
public double getArea()
{
return super.getLength()*wide;
}
}
class Circle extends Shape
{
private double r;
public Circle(double _r)
{
r = _r;
}
public double getPerimeter()
{
return 3.14159*r*2;
}
public double getArea()
{
return 3.14159*r*r;
}
}