# (Triangle类)设计一个名为Triangle的类来继承GeometricObject类。

(Triangle类)设计一个名为Triangle的类来继承GeometricObject类。该类包括：

计算三角形面积的公式参见编程练习题2.19.toString()方法的实现如下所示：

return "Triangle:side1="+side1+"side2+"side2="+side2+"side3="+side3;

//GeometricObject.java

package demo2;

public class GeometricObject

{

private String color="white";

private boolean filled;

private java.util.Date dateCreated;

public GeometricObject()

{

dateCreated=new java.util.Date();

}

public GeometricObject(String color,boolean filled)

{

dateCreated=new java.util.Date();

this.color=color;

this.filled=filled;

}

public String getColor()

{

return color;

}

public void setColor(String color)

{

this.color=color;

}

public boolean isFilled()

{

return filled;

}

public void setFilled(boolean filled)

{

this.filled=filled;

}

public java.util.Date getDateCreated()

{

return dateCreated;

}

public String toString()

{

return "created on"+dateCreated+"\ncolor:"+color+"and filled:"+filled;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

//Triangle.java

package demo2;

import java.util.*;

public class Triangle extends GeometricObject

{

private double side1=1.0;

private double side2=1.0;

private double side3=1.0;

public Triangle()

{

}

public Triangle(double side1,double side2,double side3)

{

this.side1=side1;

this.side2=side2;

this.side3=side3;

}

public void setSide1(double side1)

{

this.side1=side1;

}

public void setSide2(double side2)

{

this.side2=side2;

}

public void setSide3(double side3)

{

this.side3=side3;

}

public double getSide1()

{

return side1;

}

public double getSide2()

{

return side2;

}

public double getSide3()

{

return side3;

}

public double getArea()

{

double result;

double s=(side1+side2+side3)/2;

result=Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));

return result;

}

public double getPerimeter()

{

return side1+side2+side3;

}

public String toString()

{

return "Triangle:side1="+side1+"side2="+side2+"side3="+side3;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input=new Scanner(System.in);

Triangle triangle=new Triangle();

System.out.println("请输入三角形的三边");

double side1=input.nextDouble();

double side2=input.nextDouble();

double side3=input.nextDouble();

triangle=new Triangle(side1,side2,side3);

System.out.println("请输入三角形的颜色");

input.nextLine();

String color=input.nextLine();

System.out.println("请输入是否填充");

boolean filled=input.nextBoolean();

triangle.setColor(color);

triangle.setFilled(filled);

System.out.println("三角形面积为:"+triangle.getArea());

System.out.println("三角形周长为:"+triangle.getPerimeter());

System.out.println("三角形颜色为:"+triangle.getColor());

System.out.println("三角形是否填充:"+filled);

}

}

06-18

05-09 4846
04-03 6730
11-22 43
03-01 5475
05-25 3957