求助代码报错 要咋改呢
题目:1、定义GeometricObject类,要求如下:
1) 3个私有实例变量,分别是字符串类型的color,布尔类型的filled,Date类型的createdDate
2) 2个构造方法:一个无参,一个带参
3) 实例变量color和filled的访问器和修改器
4) 实例变量createdDate的修改器
5) 重写父类的toString、equals和hashCode方法
2、定义Triangle的类继承自GeometricObject,要求如下:
1) 三个名为side1,side2和side3的double类型私有数据域,初始值均为1.0
2) 2个构造方法
3) side1,side2和side3的访问器和修改器
4) getArea()的方法返回三角形的面积
5) getPerimeter()的方法返回三角形的周长
6) 重写父类的toString、equals和hashCode方法(按照教材给定的规范写)
3、定义Cricle的类继承自GeometricObject,要求如下:
1)1个名为radius的double类型私有数据域,初始值均为1.0
2)2个构造方法
3)radius的访问器和修改器
4)getArea()的方法返回圆的面积
5)getPerimeter()的方法返回圆的周长
6)重写父类的toString、equals和hashCode方法(按照教材给定的规范写)
程序代码:
package text; import java.util.Date; class GeometricObject { private String color; private boolean filled; Date createdDate; public GeometricObject(String color,boolean filled,Date createdDate) { this.color=color; this.filled=filled; this.createdDate=createdDate; } public GeometricObject(){} public String getColor() { return color; } public void setColor(String co) { color =co; } public boolean isFilled() { return filled; } public void setFilled(boolean fill) { filled = fill; } public Date getCreatedDate() { return createdDate; } public String toString() { return "Created on " + createdDate + "\ncolor " + color + " and filled" + filled; } } class Triangle extends GeometricObject { private double side1, side2, side3; public Triangle(String color,boolean filled,double side1, double side2, double side3){ setColor(color); setFilled(filled); this.side1=side1; this.side2=side2; this.side3=side3; } public double getSide1() { return side1; } public void setSide1(double s1) { side1 =s1; } public double getSide2() { return side2; } public void setSide2(double s2) { side2 =s2; } public double getSide3() { return side3; } public void setSide3(double s3) { side3 =s3; } public Object setArea() { return getArea(); } public double getArea() { return ((side2+side3-side1)/2*(side1+side3-side2)/2*(side1+side2-side3)/2); } public Object setPerimeter() { return getPerimeter(); } public double getPerimeter() { return side1+side2+side3; } public void printTriangle() { System.out.println(getCreatedDate()); } public String toString() { return "Triangle"+side1+getArea()+getPerimeter(); } } class Circle extends GeometricObject { private double radius; public Circle(String color,boolean filled,double radius) { setColor(color); setFilled(filled); this.radius=radius; } public Circle(){} public double getRadius() { return radius; } public void setRadius(double rad) { radius =rad; } public Object setArea() { return getArea(); } public double getArea() { return radius*radius*Math.PI; } public double getPerimeter() { return 2*radius*Math.PI; } public void printCircle() { System.out.println(getCreatedDate()); } public String toString() { return "Circle"+getArea()+getPerimeter(); } } public class main { Public static double getArea(GeometricObject[] obj){ return } public static void main(String[] args) { GeometricObject trigle = new Triangle("red",true,3,4,5); System.out.println(trigle.getCreatedDate()); System.out.println(trigle.isFilled()); System.out.println(((Triangle) trigle).getSide1()); System.out.println(((Triangle) trigle).getArea()); System.out.println(((Triangle) trigle).getPerimeter()); GeometricObject circle = new Circle("blue",false,12.34); System.out.println(circle.getCreatedDate()); System.out.println(circle.isFilled()); System.out.println(circle.getColor()); System.out.println(((Circle) circle).getRadius()); System.out.println(((Circle) circle).getArea()); System.out.println(((Circle) circle).getPerimeter()); GeometricObject[] objArray = new GeometricObject[5]; objArray[0] = new Circle("blue",false,1.1); objArray[1] = new Circle("yellow",true,2.1); objArray[2] = new Circle("green",false,3.1); objArray[3] = new Triangle("green",false,3.1,3.1,3.1); objArray[4] = new Triangle("pink",true,5.1,4.1,8.1); System.out.println(getArea(objArray)); } }