【问题标题】:Finding length and angles of a triangle求三角形的长和角
【发布时间】:2014-11-08 17:25:35
【问题描述】:

我的工作是计算给定三角形的以下属性:所有边的长度、所有角的角度、周长和面积。我有一个三角形类和一个三角形测试器类。我认为我已经正确编码了周长和面积?但是我开始考虑,而不是为我的周长设置一个恒定的可变边,我应该使用所有边的长度来找到我的周长。我坚持的是找到长度和角度。出于某种原因,当我运行我的测试程序类时,它们都以 1.0 的形式出现。任何意见,将不胜感激!谢谢!

    import java.util.Scanner;

public class Triangle 
    {
        Scanner in = new Scanner(System.in);

        /**
         * Variables
         */
        double x1;
        double x2;
        double x3;
        double y1;
        double y2;
        double y3;
        double lengthA;
        double lengthB;
        double lengthC;
        double angleA;
        double angleB;
        double angleC;
        double area;
        double perimeter;
        double base;
        double height;
        double p;

        /**
        Constructs x and y coordinates
        @param x1, x2, x3 to x1, x2, x3
        @param y1, y2, y3 to y1, y2, y3
     */
     public Triangle(double x1, double x2, double x3, double y1, double y2, double y3)
     {
        this.x1 = x1;
        this.x2 = x2;
        this.x3 = x3;
        this.y1 = y1;
        this.y2 = y2;
        this.y3 = y3;
     }

        /**
         *Find lengths of all sides
         */
        public double getLengthA()
        {
            lengthA = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
            return lengthA;
        }

        public double getLengthB()
        {
            lengthB = Math.sqrt(Math.pow((x3 - x2), 2) + Math.pow((y3 - y2), 2));
            return lengthB;
        }

        public double getLengthC()
        {
            lengthC = Math.sqrt(Math.pow((x1 - x3), 2) + Math.pow((y1 - y3), 2));
            return lengthC;
        }

        /**
         * Find angles at all corners
         @return angles at all corners
         */
        public double getAngleA()
        {
            angleA = lengthA + lengthB + lengthC - (lengthB * lengthC);
            return angleA;
        }

        public double getAngleB()
        {
            angleB = lengthB + lengthA + lengthC - (lengthA * lengthC);
            return angleB;
        }

        public double getAngleC()
        {
            angleC = lengthC + lengthA + lengthB - (lengthA * lengthB);
            return angleC;
        }


        /**
         * Constant Variables
         */
        public Triangle()
        {   
            base = 5;
            height = 15;
        }

        /**
         * Find perimeter of triangle
         */
        public double getPerimeter()
        {
            perimeter = lengthA + lengthB + lengthC;
            return perimeter;
        }

        public double getHalfPerimeter()
        {
            p = perimeter / 2;
            return p;
        }

        /**
         * Find area of triangle
         */
        public double getArea()
        {
            double area = Math.sqrt(p * (p - lengthA) * (p - lengthB) * (p - lengthC));
            return area;
        }
}

这是我的测试类:

    import java.util.Scanner;

import javax.swing.JOptionPane;


public class TriangleSimulator 
{
public static void main (String[] args)
{
    Scanner in = new Scanner(System.in);


 String input = JOptionPane.showInputDialog("Enter X coordinate for the first corner of the triangle: ");
 double x1 = Double.parseDouble(input);
 input = JOptionPane.showInputDialog("Enter Y coordinate for the first corner of the triangle: ");
 double y1 = Double.parseDouble(input);
 input = JOptionPane.showInputDialog("Enter X coordinate for the second corner of the triangle: ");
 double x2 = Double.parseDouble(input);
 input = JOptionPane.showInputDialog("Enter Y coordinate for the second corner of the triangle: ");
 double y2 = Double.parseDouble(input);
 input = JOptionPane.showInputDialog("Enter X coordinate for the third corner of the triangle: ");
 double x3 = Double.parseDouble(input);
 input = JOptionPane.showInputDialog("Enter Y coordinate for the third corner of the triangle: ");
 double y3 = Double.parseDouble(input);

 Triangle t = new Triangle(x1, x2, x3, y1, y2, y3);

 System.out.println("Length A is: " + t.getLengthA());
 System.out.println("Length B is: " + t.getLengthB());
 System.out.println("Length C is: " + t.getLengthC());
 System.out.println("Angle A is: " + t.getAngleA());
 System.out.println("Angle B is: " + t.getAngleB());
 System.out.println("Angle C is: " + t.getAngleC());
 System.out.println("Area: " + t.getArea());
 System.out.println("Perimeter: " + t.getPerimeter());

 in.close();
}
}

【问题讨论】:

    标签: java


    【解决方案1】:

    您正在显示提示用户输入的 JOptionPanes,但您没有收到输入,因此您没有使用输入来设置三角形的状态,而是创建了一个默认值使用其无参数构造函数的三角形对象。了解 Java 编程没有魔法,您的 Triangle 对象不会神奇地知道用户输入了哪些数字并相应地改变自己。相反,必须将该信息提供给您的三角形。

    您必须做的是分配从 JOptionPanes 返回的结果,将它们解析为双精度数,然后使用这些数字创建一个三角形,使用带有数值参数的构造函数,而不是默认构造函数。这样做,你应该会很好。

    例如

    String input = JOptionPane.showInputDialog("Enter X coordinate for the first corner of the triangle: ");
    double x1 = Double.parseDouble(input);
    input = JOptionPane.showInputDialog("Enter Y coordinate for the first corner of the triangle: 
    double y1 = Double.parseDouble(input);
    //.... etc repeat...
    
    Triangle triangle = new Triangle(x1, x2, x3, y1, y2, y3);
    

    编辑

    此构造函数忽略传入的值:

    公共三角形(双 x1,双 x2,双 x3,双 y1,双 y2,双 y3) { x1 = 0; x2 = 0; x3 = 0; y1 = 0; y2 = 0; y3 = 0; }

    像您需要的那样的构造函数应该接受传入的值,并使用这些值来设置类字段。例如这里:

    public class Foo {
       private int bar;  // the bar field
    
       public Foo(int bar) {
          this.bar = bar;
       }  
    }
    

    请注意,我在上面使用了this.bar,以便Java 知道我想用bar 参数保存的值设置bar 字段(bar 没有 this)。您只需要使用 6 个参数而不是一个参数来执行类似的操作。

    然后,您在称为初始化程序块的单独代码块中进行所有计算:

    { 
        /**
         * Find lengths of all sides
         */
        lengthA = Math.pow(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2) * .05, lengthA);
        lengthB = Math.pow(Math.pow((x3 - x2), 2) + Math.pow((y3 - y2), 2) * .05, lengthB);
        lengthC = Math.pow(Math.pow((x1 - x3), 2) + Math.pow((y1 - y3), 2) * .05, lengthC);
    }
    

    此代码在您的构造函数之前被调用,因此即使您正确设置了 Triangles 字段,此代码也无法工作。您不想使用初始化程序块,您可能会忘记我什至提到它们,只是告诉您不要使用它们。而是在构造函数中进行计算,并在设置所有字段之后进行。

    请注意,我特意没有针对您的问题发布解决方案,因为我坚信我们大多数人需要的是理解我们遇到的任何问题背后的概念,然后利用这种理解来创建我们自己的代码解决方案。

    最重要的是,阅读你的课文,最好学习你的课文,因为你所犯的错误涉及基本概念,并表明你还没有理解这些概念并求助于猜测。这永远不会奏效,因为如果您要在本课程中取得进步,您需要了解所有这些并很好地理解它。

    祝你好运!


    编辑 2
    对于 Tom,运行以下命令:

    public class TestInitializerBlock {
       public TestInitializerBlock() {
          System.out.println("Inside of constructor");
       }
    
       {
          System.out.println("Inside of initializer block");
       }
    
       public static void main(String[] args) {
          new TestInitializerBlock();
       }
    }
    

    【讨论】:

    • 我再次进行了更改。现在我的计算结果为 0.0
    • @Courtney 你应该使用你的新方法。例如,此 System.out.println("Length A is: " + t.lengthA) 应更新为 System.out.println("Length A is: " + t.getLengthA())
    • 我做到了。现在所有的长度和角度都是 1.0,周长是 3.0,面积是 0.0。我每次运行都输入了多个数字,但仍然是相同的计算。
    • @Courtney:这看起来很可疑:lengthA = Math.pow(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2) * .05, lengthA);。乘以 1/2 再将乘积提升到 lengthA 次方??重新思考你的数学和逻辑。
    • @Courtney 我不确定你是否还在做这个。我想我参加晚会有点晚了。但是您引用的角度公式(这个角度上方的四个 cmets)是不正确的。一旦你有了长度,要计算出角度,你需要一种叫做“余弦规则”或“余弦定律”的东西。我建议你谷歌这个。
    猜你喜欢
    • 2023-01-30
    • 2023-01-31
    • 2013-09-17
    • 1970-01-01
    • 2014-12-06
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    相关资源
    最近更新 更多