Design a class named Triangle that extends GeometricObject . The class contains

Design a class named  Triangle that extends GeometricObject . Theclass contains:

■  Three double data fieldsnamed  side1 , side2 , and  side3 with default

values 1.0 to denote three sides of thetriangle.

■  A no-arg constructor thatcreates a default triangle.

■  A constructor that creates atriangle with the specified  side1 ,side2 , and

side3 .

■  The accessor methods for allthree data fields.

■  A method named  getArea() that returns the area of thistriangle.

■  A method named  getPerimeter() that returns the perimeter ofthis triangle.

■  A method named  toString() that returns a string descriptionfor the triangle.

For the formula to compute the area of atriangle, see Programming Exercise 2.19.

The toString() method is implemented asfollows:

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

" side3 = " + side3;

Write a test program that prompts the userto enter three sides of the triangle, a color, and a Boolean value to indicatewhether the triangle is filled. The program should create a  Triangle object with these sides and set thecolor and  filled properties using theinput. The program should display the area, perimeter, color, and true or falseto indicate whether it is filled or not.

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(String filled1) {
    	if (filled1.equals(false)) {
    		this.filled = false;
		}else {
        
			this.filled = true;
		}
    }
 
    public java.util.Date getDateCreated() {
        return dateCreated;
    }

    public String toString() {
        return "Created on " + dateCreated + "\n color: " + color + " and filled ";                 
    }   
 }

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 double setSide1() {
        return side1;
    }

    public double setSide2() {
        return side2;
    }

    public double setSide3() {
        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() {
        return (side1 + side2 + side3) / 2;
    }

    public double getPerimeter() {
        return side1 + side2 + side3;
    }

    public String toString() {
    return " Triangle: Side 1 = " + side1 + " Side 2 = " + side2
           + " Side 3 = " + side3;
    }
}


import java.util.Scanner;

public class TestTriangle  {

    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? Reply with 'true' or 'false' ");
        String filled = input.next(); 
    	
        Triangle t1 = new Triangle(); //创建默认三角形
        System.out.println("默认三角形描述: " + t1.toString());
        System.out.println("默认三角形面积 " + t1.getArea());
        System.out.println("默认三角形周长 " + t1.getPerimeter());
        System.out.println("默认三角形填充颜色 " + t1.getColor());
        System.out.println("默认三角形是否填充" + t1.isFilled() );
        System.out.println("默认三角形是创建时间" + t1.getDateCreated());
        
        Triangle t2 = new Triangle(side1, side2, side3);  // 创建有参数的三角形
        System.out.println("============================");
        t2.setColor(color);
        t2.setFilled(filled);
        System.out.println("有参三角形描述: " + t2.toString());
        System.out.println("有参三角形面积 " + t2.getArea());
        System.out.println("有参三角形周长 " + t2.getPerimeter());
        System.out.println("有参三角形填充颜色 " + t2.getColor());
        System.out.println("有参三角形是否填充" + t2.isFilled() );
        System.out.println("有参三角形是创建时间" + t2.getDateCreated());

    }
}




  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值