Java语言程序设计 例题11.1(Triangel类)

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.
For the formula to compute the area of a triangle, see Programming Exercise 2.19.
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.

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

三个名为side1、side2和side3的double类型数据域表示这个三角形的三条边,他们的默认值是1.0。
 ■ 一个无参构造方法,创建一个默认的三角形。
 ■ 一个创建指定side1、side2和side3值的三角形的构造方法。
 ■ 所有三个数据域的访问器方法。
 ■ 一个名为getArea()的方法返回三角形的面积。
 ■ 一个名为getPerimeter()的方法返回三角形的周长。
 ■ 一个名为toString()的方法返回三角形的字符串描述。
计算三角形面积的公式参照编程习题2.19。toString()方法的实现如下所示:
 return "Create on " + super.getDateCreated() + "\ncolor: " + super.getColor() + "\nArea: " + this.getArea() + "\nPerimeter: " + this.getPerimeter();
画出Triangle类和GeometricObject类的UML图,并实现这些类。编写一个测试程序,提示用户输入三角形的三条边、颜色以及一个Boolean值表明该三角形是否填充。程序需要根据输入创建一个具有指定边的三角形,并设置color和filled属性。程序需要显示面积、周长、颜色以及表明是否填充的真或假值。

代码如下:

import java.util.*;
public class Unite11Test1
{
	public static void main(String[] args) 
	{
		Scanner input=new Scanner(System.in);
		System.out.println("请输入三个边,一个颜色,布尔值true: ");
		double s1=input.nextDouble();
		double s2=input.nextDouble();
		double s3=input.nextDouble();
		String s4=input.next();
		boolean s5=input.nextBoolean();
		Triangel t = new Triangel(s1,s2,s3);
		t.setColor(s4);
		t.setFilled(s5);
		System.out.println("面积是: "+t.getArea()+"\n周长是: "+t.getPerimrter()+"\n颜色是: "
		+t.getColor()+"\n是否被填充?"+t.isFilled());
		
		
	}
}
class Triangel extends SimpleGeometricObject
{
	double side1=1.0;
	double side2=1.0;
	double side3=1.0;
	public Triangel() 
	{
		
	}
	public double getSide1() {
		return side1;
	}

	public void setSide1(double side1) {
		this.side1 = side1;
	}

	public double getSide2() {
		return side2;
	}

	public void setSide2(double side2) {
		this.side2 = side2;
	}

	public double getSide3() {
		return side3;
	}

	public void setSide3(double side3) {
		this.side3 = side3;
	}

	public Triangel(double side1,double side2,double side3) 
	{
		this.side1=side1;
		this.side2=side2;
		this.side3=side3;
	}
	public double getArea() 
	{
		double s = (this.side1+this.side2+this.side3)/2;
		return Math.sqrt(s*(s-this.side1)*(s-this.side2)*(s-this.side3));
	}
	public double getPerimrter() 
	{
		return this.side1+this.side2+this.side3;
	}
	public String toString() 
	{
		return "Triangel: side1:"+side1+" side2 = "+side2+" side3 = "+side3;
	}
}

结果如下:

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

差劲的厉害了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值