该类包含:

  • double 数据字段命名为side1 ,side2 , 和side3 使用默认值1.0表示三angular形的三边

  • 一个无参数构造函数,创建一个具有指定的三angular形side1 ,side2 ,和side3

  • 所有三个数据字段的访问器方法。

  • 一个名为**getArea()**返回该三angular形的面积。

  • 一个名为**getPerimeter()**返回该三angular形的周长。

*一个名为**toString()**返回三angular形的字符串描述。

有关计算三angular形面积的公式,请参阅ProgrammingExercise 2.15(《Java 编程入门第 9 版》中)**toString()**方法实现如下:

return " 三angular形: side1 = " side1 " side2 = " side2 " side3 " side3;

绘制类的 UML 图TriangleGeometricObject 并实施课程。triangle 对象具有这些边并设置colorfilled使用输入的属性。


我遇到问题的练习部分是测试程序。

如果您能指导我如何根据输入制作三angular形来帮助我,我将非常感激。

我现在的代码是这样的:

The 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 ";                 
    }   
 }

The Triangle program

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;
    }
}

The Testprogram.

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? Reply with 'True' or 'False' ");

        String filled = input.next(); 
    }
    {
        new Triangle(side1, side2, side3);
        //How do i get the information into theTriangle?

        System.out.println("The Triangle Sides are \n side 1: " + side1 + "\n Side 2: " + side2 + "\n Side 3: " + side3);
        System.out.println("The Triangle's Area is " + (side1 + side2 + side3) / 2);
        System.out.println("The Triangle's Perimeter is "
                           + (side1 + side2 + side3));
        System.out.println("The Triangle's Color is " + //what goes here?);
        System.out.println("Is the Triangle filled? " + //what goes here?);

    }
}

答案

您需要创建一个新的Triangle像这样的对象,以便你有一个参考

Trangle triangle = new Triangle(side1, side2, side3);

  //     ^^^^^^ This is the most important thing you're missing.  You need a reference
  //            point for your object.  That's the only way you can access it's
  //            properties.    

您还需要设置它的filledcolor特性

triangle.setFilled(filled);
triangle.setColor(color);

然后,您可以像这样调用它的方法:

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());

您可以访问GeometricObjectisFilled(),setFilled(),getColor(), 和setColor()因为一个Triangle是一个GeometricObjectextends),所以它继承了它的所有方法。

顺便说一句,这不是计算三角形面积的方法:

public double getArea() {
    return (side1 + side2 + side3) / 2;   // This is so wrong
}

看看这个关联为了正确的公式

编辑:代码有另一个问题

public double setSide1() {
    return side1;
}

public double setSide2() {
    return side2;
}

public double setSide3() {
    return side3;
}

/**** Should Be ******/

public double getSide1() {
    return side1;
}

public double getSide2() {
    return side2;
}

public double getSide3() {
    return side3;
}

编辑:三角形公式

public double getArea() {
    int p = getPerimeter() / 2
    return Math.sqrt(p * ((p - side1) * (p - side2) * (p - side3));
}

来自: stackoverflow.com