11.1 (The Triangle class) Design a class named Triangle that extends GeometricObject. The class contains:
■ Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.
■ A no-arg constructor that creates a default triangle.
■ A constructor that creates a triangle with the specified side1, side2, and side3.
■ The accessor methods for all three data fields.
■ A method named getArea() that returns the area of this triangle.
■ A method named getPerimeter() that returns the perimeter of this triangle.
■ A method named toString() that returns a string description for the triangle.
The toString() method is implemented as follows:
return "Triangle: side1 = " + side1 + " side2 = " + side2 +" side3 = " + side3;
Draw the UML diagrams for the classes Triangle and GeometricObject and implement the classes. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not.
Eclipse插件生成UML图
这里我定义了两个自定义异常类与三角形类 父类几何图形类测试类 五个类来实现该题
import java.util.Date;
/**
* 几何图形实体类
* @author whd
*
*/
public class GeometricObject {
private String color="white";
private boolean filled;
private Date dateCreated;
//无参构造
public GeometricObject() {
//super();
dateCreated=new Date();
}
//有参构造
public GeometricObject(String color, boolean filled) {
//super();
this.color= color;
this.filled = filled;
dateCreated=new Date();
}
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 Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "create on " + dateCreated+"\ncolor: "+color+" and filled:"+filled;
}
}
/*
* 三角形实体类继承几何图形类
*/
public class Triangle extends GeometricObject {
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;
public Triangle() {
super();
}
public Triangle(double side1, double side2, double side3) {
super();
// 如果边长为负 抛出自定义异常 打印("边长不能为负")
if (side1 < 0 || side2 < 0 || side3 < 0)
throw new SideIsNegativeException("边长不能为负");
// 如果有任何两边之和不大于第三边 抛出自定义异常 打印"不能构成三角形"
if (!((side1 + side2) > side3 && (side2 + side3) > side1 && (side1 + side3) > side2))
throw new ProduceTriangleException("此三边无法构造三角形");
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 double getPerimeter() {
return side1 + side2 + side3;
}
// 计算面积
public double getArea() {
double p = (side1 + side2 + side3) / 2;
// 利用海伦公式计算三角形面积
return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
}
@Override
public String toString() {
return super.toString() + "\nTriangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;
}
}
/*
* 自定义异常 边长为负*
*/
public class SideIsNegativeException extends IllegalArgumentException{
//序列化
private static final long serialVersionUID = -6372928593269244107L;
public SideIsNegativeException() {
super();
// TODO Auto-generated constructor stub
}
public SideIsNegativeException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public SideIsNegativeException(String s) {
super(s);
// TODO Auto-generated constructor stub
}
public SideIsNegativeException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
/*
* 自定义异常 三边不满足构建三角形的条件
*/
public class ProduceTriangleException extends IllegalArgumentException{
//序列化
private static final long serialVersionUID = 1683443194340418835L;
public ProduceTriangleException() {
super();
// TODO Auto-generated constructor stub
}
public ProduceTriangleException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public ProduceTriangleException(String s) {
super(s);
// TODO Auto-generated constructor stub
}
public ProduceTriangleException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* 测试类
* @author whd
*
*/
public class Test {
public static void main(String[] args) {
Triangle triangle = null;
System.out.println("输入三角形的三边长:");
// 该方法生成符合要求的三角形对象并返回该对象
triangle = produceTriangle(triangle);
System.out.println("打印结果:\n" + triangle.toString());
System.out.println("此三角形周长是:"+triangle.getArea());
System.out.println("此三角形面积是"+triangle.getArea());
}
private static Triangle produceTriangle(Triangle triangle) {
double side1;
double side2;
double side3;
Scanner input = new Scanner(System.in);
while (true) {
try {
side1 = input.nextDouble();
side2 = input.nextDouble();
side3 = input.nextDouble();
triangle = new Triangle(side1, side2, side3);
} catch (InputMismatchException e1) {// 主动捕获输入不是浮点数或数字的异常 此异常是系统预定义的异常之一
input.next();
System.out.println("输入浮点数非法,请重新输入");
continue;
} catch (SideIsNegativeException e) {// 捕获到自定义异常 边长不能为负
System.out.println(e);
System.out.println("请重新输入");
continue;
} catch (ProduceTriangleException e) {// 捕获自定义异常 三边无法构成三角形
System.out.println(e);
System.out.println("请重新输入");
continue;
}
selectColor(triangle);// 设置三角形颜色
selectFilled(triangle);// 设置是否填充
break;
}
return triangle;
}
private static void selectColor(Triangle triangle) {
System.out.println("设置三角形颜色\n请选择\n1.红色 2.橙色 3.黄色 4.绿色 5.蓝色 6.青色 7.紫色");
Scanner input = new Scanner(System.in);
boolean flag = false;
int key = 0;
while (true) {
try {
key = input.nextInt();
} catch (InputMismatchException e) {
input.next();
System.out.println("输入非法,重新选择");
continue;
}
switch (key) {
case 1:
triangle.setColor("红色");
flag = true;
break;
case 2:
triangle.setColor("橙色");
flag = true;
break;
case 3:
triangle.setColor("黄色");
flag = true;
break;
case 4:
triangle.setColor("绿色");
flag = true;
break;
case 5:
triangle.setColor("蓝色");
flag = true;
break;
case 6:
triangle.setColor("青色");
flag = true;
break;
case 7:
triangle.setColor("紫色");
flag = true;
break;
default:
System.out.println("输入有误,重新输入");
break;
}
if (flag == true)
break;
}
}
private static void selectFilled(Triangle triangle) {
System.out.println("是否填充该三角形?\n请选择:\n1.是\n2.否");
Scanner input = new Scanner(System.in);
int key = 0;
boolean flag = false;// 控制跳出外层循环
while (true) {
try {
key = input.nextInt();
} catch (InputMismatchException e) {
input.next();
System.out.println("输入非法,重新选择");
continue;
}
switch (key) {
case 1:
triangle.setFilled(true);
flag = true;
break;
case 2:
triangle.setFilled(false);
break;
default:
System.out.println("输入有误,请重新选择");
break;
}
if (flag == true)
break;
}
}
}
运行截图: