a) 该类有一个Point的类型的私有属性center,表示shape的中心点坐标
b) 二个成员方法:double getArea()获取图形的面积,默认返回值为0,double getPerimeter() 获取图形的周长,默认返回值为0
a) 重写Shape的getArea和getPerimer方法,以计算圆的周长和面积
b) 重写圆的toString方法,使得该方法能够显示圆心坐标和圆的半径
c) 重写圆的equals方法,当两个圆的圆心坐标相同,并且半径相同就认为两个圆相等
d) 重写hashCode方法,确保当equals方法为true时,hashCode值相等
e) 给每个圆定义编号属性,表明该圆是生成的第几个
f) 定义一个属性MIN_RADIUS,表示半径的最小值0.5(不可修改),当给半径赋值时,如果值小于该最小值,则将半径设为该最小值。
a) 该类有两个私有属性:width(宽),heighth(高)
b) 创建一个无参的构造方法,该方法创建一个中心在原点,宽高都为1的长方形
c) 创建一个有参的构造方法,该方法创建一个指定宽高和中心点的长方形
d) 重写父类的getArea和getPerimeter方法,计算长方形的周长和面积
e) 重写toString方法,使得该方法能显示长方形的宽、高和中心点坐标
a) 该类有三个私有属性:a,b,c表示三角形的三个边
b) 定义一个无参构造方法,该方法创建一个三角形,中心点在原点,a,b,c的值都为1
c) 定义一个有参的构造方法,该方法创建一个指定边长和中心点的三角形
d) 重写父类的getArea和getPerimeter方法,计算三角形的周长和面积
e) 重写toString方法,使得该方法能显示三角形的三边和中心点坐标
好的,以下是Java代码实现:
import java.awt.Point;
public class Shape {
private Point center;
public Shape(Point center) {
this.center = center;
}
public double getArea() {
return 0;
}
public double getPerimeter() {
return 0;
}
}
public class Circle extends Shape {
private static final double MIN_RADIUS = 0.5;
private static int count = 0;
private int id;
private double radius;
public Circle(Point center, double radius) {
super(center);
this.radius = Math.max(radius, MIN_RADIUS);
count++;
id = count;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public String toString() {
return "Circle " + id + ": center = " + center + ", radius = " + radius;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Circle)) {
return false;
}
Circle other = (Circle) obj;
return center.equals(other.center) && radius == other.radius;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + center.hashCode();
result = 31 * result + Double.hashCode(radius);
return result;
}
}
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle() {
this(new Point(0, 0), 1, 1);
}
public Rectangle(Point center, double width, double height) {
super(center);
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
@Override
public String toString() {
return "Rectangle: center = " + center + ", width = " + width + ", height = " + height;
}
}
public class Triangle extends Shape {
private double a;
private double b;
private double c;
public Triangle() {
this(new Point(0, 0), 1, 1, 1);
}
public Triangle(Point center, double a, double b, double c) {
super(center);
this.a = a;
this.b = b;
this.c = c;
}
public double getArea() {
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
public double getPerimeter() {
return a + b + c;
}
@Override
public String toString() {
return "Triangle: center = " + center + ", a = " + a + ", b = " + b + ", c = " + c;
}
}
public class TestShape {
public static void main(String[] args) {
Shape shape;
shape = new Circle(new Point(0, 0), 2);
System.out.println(shape.toString());
System.out.println("Area: " + shape.getArea());
System.out.println("Perimeter: " + shape.getPerimeter());
shape = new Rectangle(new Point(0, 0), 2, 3);
System.out.println(shape.toString());
System.out.println("Area: " + shape.getArea());
System.out.println("Perimeter: " + shape.getPerimeter());
shape = new Triangle(new Point(0, 0), 3, 4, 5);
System.out.println(shape.toString());
System.out.println("Area: " + shape.getArea());
System.out.println("Perimeter: " + shape.getPerimeter());
}
}
以下是Java代码实现(续):
public class Circle extends Shape {
private static final double MIN_RADIUS = 0.5;
private static int count = 0;
private int id;
private double radius;
public Circle(Point center, double radius) {
super(center);
this.radius = Math.max(radius, MIN_RADIUS);
count++;
id = count;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public String toString() {
return "Circle " + id + ": center = " + center + ", radius = " + radius;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Circle)) {
return false;
}
Circle other = (Circle) obj;
return center.equals(other.center) && radius == other.radius;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + center.hashCode();
result = 31 * result + Double.hashCode(radius);
return result;
}
}
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle() {
this(new Point(0, 0), 1, 1);
}
public Rectangle(Point center, double width, double height) {
super(center);
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
@Override
public String toString() {
return "Rectangle: center = " + center + ", width = " + width + ", height = " + height;
}
}
public class Triangle extends Shape {
private double a;
private double b;
private double c;
public Triangle() {
this(new Point(0, 0), 1, 1, 1);
}
public Triangle(Point center, double a, double b, double c) {
super(center);
this.a = a;
this.b = b;
this.c = c;
}
public double getArea() {
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
public double getPerimeter() {
return a + b + c;
}
@Override
public String toString() {
return "Triangle: center = " + center + ", a = " + a + ", b = " + b + ", c = " + c;
}
}
public class TestShape {
public static void main(String[] args) {
Shape shape;
shape = new Circle(new Point(0, 0), 2);
System.out.println(shape.toString());
System.out.println("Area: " + shape.getArea());
System.out.println("Perimeter: " + shape.getPerimeter());
shape = new Rectangle(new Point(0, 0), 2, 3);
System.out.println(shape.toString());
System.out.println("Area: " + shape.getArea());
System.out.println("Perimeter: " + shape.getPerimeter());
shape = new Triangle(new Point(0, 0), 3, 4, 5);
System.out.println(shape.toString());
System.out.println("Area: " + shape.getArea());
System.out.println("Perimeter: " + shape.getPerimeter());
}
}
可以参考:
public class Shape {
private int center;
private double area=0;
private double perimeter=0;
public double getArea(){
return this.area;
}
public double getPerimeter(){
return this.perimeter;
}
}