

复习考试题库,代码编写
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
// PerimeterInterface.java
public interface PerimeterInterface {
public abstract double getPerimeter();
}
// AreaInterface.java
public interface AreaInterface {
public abstract double getArea();
}
// Shape.java
public abstract class Shape implements PerimeterInterface, AreaInterface {
public static void getInfo(Shape shape) {
System.out.println("Perimeter: " + shape.getPerimeter());
System.out.println("Area: " + shape.getArea());
}
}
// Point.java
public class Point extends Shape {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public double getPerimeter() {
return 0.0;
}
@Override
public double getArea() {
return 0.0;
}
}
// Line.java
public class Line extends Shape {
private Point start;
private Point end;
public Line(Point start, Point end) {
this.start = start;
this.end = end;
}
@Override
public double getPerimeter() {
return start.getPerimeter() + end.getPerimeter();
}
@Override
public double getArea() {
return 0.0;
}
}
// Circle.java
public class Circle extends Shape {
private Point center;
private double radius;
public Circle(Point center, double radius) {
this.center = center;
this.radius = radius;
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
}
// RightTriangle.java
public class RightTriangle extends Shape {
private Point a;
private Point b;
private Point c;
public RightTriangle(Point a, Point b, Point c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public double getPerimeter() {
double sideA = Math.sqrt(Math.pow((b.getX() - a.getX()), 2) + Math.pow((b.getY() - a.getY()), 2));
double sideB = Math.sqrt(Math.pow((c.getX() - b.getX()), 2) + Math.pow((c.getY() - b.getY()), 2));
double sideC = Math.sqrt(Math.pow((a.getX() - c.getX()), 2) + Math.pow((a.getY() - c.getY()), 2));
return sideA + sideB + sideC;
}
@Override
public double getArea() {
double sideA = Math.sqrt(Math.pow((b.getX() - a.getX()), 2) + Math.pow((b.getY() - a.getY()), 2));
double sideB = Math.sqrt(Math.pow((c.getX() - b.getX()), 2) + Math.pow((c.getY() - b.getY()), 2));
return (sideA * sideB) / 2;
}
}
// Main.java
public class Main {
public static void main(String[] args) {
Point p = new Point(0, 0);
Line l = new Line(new Point(0, 0), new Point(3, 4));
Circle c = new Circle(new Point(0, 0), 5);
RightTriangle r = new RightTriangle(new Point(0, 0), new Point(3, 0), new Point(0, 4));
Shape.getInfo(p);
Shape.getInfo(l);
Shape.getInfo(c);
Shape.getInfo(r);
}
}
abstract class Shapes { //定义抽象类 Shapes
abstract public double area(); //周长
abstract public double square(); //面积
}
class Triangle extends Shapes { //继承 Shapes,定义三角型
private double a, b, c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double square() {
double p = (a + b +c) / 2;
return Math.sqrt(p * (p - a) * (p- b) * (p- c));
}
public double area() {
double cir = (a + b +c);
return cir;
}
public String toString() {
return "三角形:边长"+a+","+b+","+c;
}
}
class Rectangle extends Shapes { //定义矩形
private double width, height;//长,宽
public Rectangle(double j, double k) {
width = j; height = k;
}
public double area() {
double Rectangle =(width + height)*2;
return Rectangle;
}
public String toString() {
return "矩形:宽 "+width+" 高 "+height;
}
public double square() {
double Rectangle =width* height;
return Rectangle;
}
}
class Circle extends Shapes { //定义圆
private double r;
public Circle(double r) {
this.r = r;
}
public double area() {
return 3.14 * 2 * r;
}
public double square() {
return 3.14 * r * r;
}
public String toString() {
return "圆:半径= "+ r;
}
}