设计一个名为Triangle的类,它扩展了GeometricObject 它必须包含:
三个双重数据字段,名为side1,side2和side3,默认值为1.0,表示三角形的三个边
使用指定的side1,side2和side3创建三角形的无参数构造函数。
所有三个数据字段的访问器方法。
一个名为getArea()的方法,返回该三角形的区域。
一个名为getPerimeter()的方法,它返回此三角形的周长。
*一个名为toString()的方法,它返回三角形的字符串描述。
有关计算三角形面积的公式,请参阅ProgrammingExercise 2.15(在java编程第9版简介中)toString()方法实现如下:
返回"三角形:side1 =" + side1 +" side2 =" + side2 +" side3" + side3;
编写一个测试程序,提示用户输入三角形的三个边,颜色和布尔值,以指示三角形是否已填充。程序应该使用这些边创建一个三角形对象,并使用输入设置颜色和填充属性。程序应显示区域,周长,颜色以及真或假,以指示是否填充。
我的问题是它没有输出区域,周长,颜色以及真或假来指示它是否已填满。它会在#34;三角形填满之后停止吗? '真正的'或者'错误'"。有人帮帮我吗?
三角
public class Triangle extends GeometricObject {
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;
Triangle triangle = new Triangle(side1, side2, side3);
public Triangle() {
}
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public void setSide3(double side3) {
this.side3 = side2;
}
public double getArea() {
int p = (int) getPerimeter() / 2;
return Math.sqrt(p * ((p - side1) * (p - side2) * (p - side3)));
}
public double getPerimeter() {
return side1 + side2 + side3;
}
public String toString() {
return " Triangle: Side 1 = " + side1 + " Side 2 = " + side2
+ " Side 3 = " + side3;
}
}
GeometricObject
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 + "\n color: " + color + " and filled ";
}
}
TestTriangle
import java.util.Scanner;
public class TestTriangle {
private double Side1 = 1.0;
private double Side2 = 1.0;
private double Side3 = 1.0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter three sides of the Triangle");
double Side1 = input.nextDouble();
double Side2 = input.nextDouble();
double Side3 = input.nextDouble();
System.out.println("Enter the color of the Triangle");
String color = input.next();
System.out.println("Is the Triangle filled? 'True' or 'False' ");
String filled = input.next();
}
{
Triangle triangle = new Triangle(Side1, Side2, Side3);
System.out.println("The Triangle Sides are \n side 1: " + triangle.getSide1() + "\n Side 2: " + triangle.getSide2() + "\n Side 3: " + triangle.getSide3());
System.out.println("The Triangle's Area is " + triangle.getArea());
System.out.println("The Triangle's Perimeter is " + triangle.getPerimeter());
System.out.println("The Triangle's Color is " + triangle.getColor());
System.out.println("Is the Triangle filled? " + triangle.isFilled());
}
}