日期:2014-05-20 浏览次数:20681 次
class Exercise8_1
{
public static void main(String[] args){
Triangle tr = new Triangle();
tr.setColor = "yellow";
tr.isFilled = true;
System.out.println(tr.toString());
}
}
class Triangle extends GeometricObject {
double side1, side2, side3;
/** Constructor */
public Triangle() {
}
/** Implement the abstract method findArea in GeometricObject */
public double getArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
/** Implement the abstract method findCircumference in
* GeometricObject
**/
public double getPerimeter() {
return side1 + side2 + side3;
}
/** Override the toString method */
public String toString() {
return "Triangle: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;
}
}
// GeometricObject.java: The abstract GeometricObject class
class GeometricObject {
private String color = "white";
private boolean filled;
/**Default construct*/
GeometricObject() {
}
/**Getter method for color*/
public String getColor() {
return color;
}
/**Setter method for color*/
public void setColor(String color1) {
color = color1;
}
/**Getter method for filled. Since filled is boolean,
so, the get method name is isFilled*/
public boolean isFilled() {
return filled;
}
/**Setter method for filled*/
public void setFilled(boolean filled1) {
filled = filled1;
}
}
class Exercise8_1 {