温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java编程三角形(Triangle类)继承

发布时间:2020-07-18 00:21:48 来源:网络 阅读:629 作者:梦T醒 栏目:编程语言
import java.util.Scanner;

public class Test 
{
    public static void main(String[] args) 
    {
        System.out.println("请输入三角形的三条边,颜色,是否填充(true or false)");
        Scanner input=new Scanner(System.in);
        double side1=input.nextDouble();
        double side2=input.nextDouble();
        double side3=input.nextDouble();
        String color=input.next();
        boolean filled=input.nextBoolean();
        Triangle tri=new Triangle(side1,side2,side3,color,filled);
        System.out.println(tri);//调用Triangle类
        System.out.println("Area:"+tri.getArea());
        System.out.println("Perimeter:"+tri.getPerimeter());
        System.out.println("Color:"+tri.getColor());
        System.out.println("IsFilled:"+tri.getFilled());
    }  
}
class GeometricObject
{
    private String color="white";
    private boolean filled=false;
    public GeometricObject()
    {
    }
    public GeometricObject(String color,boolean filled)
    {
        this.color=color;
        this.filled=filled;
    }
    public String getColor()
    {
        return color;
    }
    public boolean getFilled()
    {
        return filled;
    }
    public void setColor(String color)
    {
        this.color=color;
    }
    public void setFilled(boolean filled)
    {
        this.filled=filled;
    }
    public String toString()
    {
        return "Color:"+color+"Filled:"+filled;
    }
}
class Triangle extends GeometricObject
{
    private double side1=1;
    private double side2=1;
    private double side3=1;
    public Triangle()
    {
    }
    public Triangle(double a,double b,double c,String color,boolean filled)
    {
        super(color,filled);
        if(a+b>c&&a+c>b&&b+c>a)
        {
            side1=a;
            side2=b;
            side3=c;
        }
        else
        {
            System.out.println("输入的不是三角形!");
        }
    }
    public double getSide1()
    {
        return side1;
    }
    public double getSide2()
    {
        return side2;
    }
    public double getSide3()
    {
        return side3;
    }
    public double getArea()
    {
        double s=(side1+side2+side3)/2;
        return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));//面积公式
    }
    public double getPerimeter()
    {
        return side1+side2+side3;
    }
    public String toString()
    {
        return "Triangle:side1="+side1+"  side2="+side2+"  side3="+side3;
    }
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI