如何在另一个类中使用来自测试类的JOptionPane输入?

时间:2017-10-04 23:51:08

标签: java joptionpane

我目前有一个包含所有计算的Triangle类。

public class Triangle
{
   private double x1;
   private double y1;
   private double x2;
   private double y2;
   private double x3;
   private double y3;
   private double lengthA;
   private double lengthB;
   private double lengthC;
   private double angleA;
   private double angleB;
   private double angleC;
   private double perimeter;
   private double height;
   private double area;

   public double calcArea()
   {
      area = .5 * lengthC * height;
      return area;
   }

   public double calcPerimeter()
   {
      perimeter = lengthA + lengthB + lengthC;
      return perimeter;
   }

   public double lengthA()
   {
      lengthA = Math.sqrt(Math.pow((x2 - x3),2) + Math.pow(height,2));
      return lengthA;
   }

   public double lengthB()
   {
      lengthB = Math.sqrt(Math.pow((x3 - x1),2) + Math.pow(height,2));
      return lengthB;
   }

   public double lengthC()
   {
      lengthC = x2 - x1;
      return lengthC;
   }

   public double getHeight()
   {
      height = y3 - y1;
      return height; 
   }

   public double angleA()
   {
      angleA = Math.abs(Math.toDegrees(Math.asin(height / lengthB)));
      return angleA;
   }

   public double angleB()
   {
      angleB = Math.abs(Math.toDegrees(Math.asin(height / lengthA)));
      return angleB;
   }

   public double angleC()
   {
      angleC = 180 - angleA - angleB;
      return angleC;
   }
}

我还有一个TriangleTester类,它使用JOptionPane来获取三角形的坐标。

import javax.swing.*;
public class TriangleTester
{
   public static void main (String [] args)
   {
      double x1;
      double y1;
      double x2;
      double y2;
      double x3;
      double y3;
      String v1;
      String v2;
      String v3;
      String v4;
      String v5;
      String v6;

      v1 = JOptionPane.showInputDialog("Enter x1 for point A");
      v2 = JOptionPane.showInputDialog("Enter y1 for point A");
      v3 = JOptionPane.showInputDialog("Enter x2 for point B");
      v4 = JOptionPane.showInputDialog("Enter y2 for point B");
      v5 = JOptionPane.showInputDialog("Enter x3 for point C");
      v6 = JOptionPane.showInputDialog("Enter y3 for point C");
      x1 = Integer.parseInt(v1);
      y1 = Integer.parseInt(v2);
      x2 = Integer.parseInt(v3);
      y2 = Integer.parseInt(v4);
      x3 = Integer.parseInt(v5);
      y3 = Integer.parseInt(v6);

      Triangle tri = new Triangle();
      double lengthA = tri.lengthA();
      double lengthB = tri.lengthB();
      double lengthC = tri.lengthC();
      double angleA = tri.angleA();
      double angleB = tri.angleB();
      double angleC = tri.angleC();
      double perimeter = tri.calcPerimeter();
      double height = tri.getHeight();
      double area = tri.calcArea();

      System.out.printf("Set up triangle with coordinates (" + x1 + "," + y1 + "), (" + x2 + "," + y2 + "), (" + x3 + "," + y3 + ")");
      System.out.printf("\nArea:\t\t\t\t" + area);
      System.out.printf("\nPerimeter:\t\t" + perimeter);
      System.out.printf("\nLength side a:\t" + lengthA);
      System.out.printf("\nLength side b:\t" + lengthB);
      System.out.printf("\nLength side c:\t" + lengthC);
      System.out.printf("\nHeight h:\t\t" + height);
      System.out.printf("\nAngle A:\t\t\t" + angleA);
      System.out.printf("\nAngle B:\t\t\t" + angleB);
      System.out.printf("\nAngle C:\t\t\t" + angleC);
   }
}

当我运行测试器代码时,它会打印带有正确x和y值的坐标,但是其他所有内容最终打印为0.我相信这是因为我没有在x和y值之间建立任何关联Triangle类和TriangleTester类。如何让Triangle类使用TriangleTester类中输入的x和y值来计算答案?感谢。

1 个答案:

答案 0 :(得分:0)

这里的主要想法是你使用吸气剂作为制定者。 您没有在类之间传递setter方法的数据 主程序

package triangles;

import javax.swing.*;
public class Triangles {

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
   double x1;
  double y1;
  double x2;
  double y2;
  double x3;
  double y3;
  String v1;
  String v2;
  String v3;
  String v4;
  String v5;
  String v6;

  v1 = JOptionPane.showInputDialog("Enter x1 for point A");
  v2 = JOptionPane.showInputDialog("Enter y1 for point A");
  v3 = JOptionPane.showInputDialog("Enter x2 for point B");
  v4 = JOptionPane.showInputDialog("Enter y2 for point B");
  v5 = JOptionPane.showInputDialog("Enter x3 for point C");
  v6 = JOptionPane.showInputDialog("Enter y3 for point C");
  x1 = Integer.parseInt(v1);
  y1 = Integer.parseInt(v2);
  x2 = Integer.parseInt(v3);
  y2 = Integer.parseInt(v4);
  x3 = Integer.parseInt(v5);
  y3 = Integer.parseInt(v6);

  Triangle tri = new Triangle();
  //set all needed data
  tri.setLengthA(x2,x3);
  tri.setLengthB(x3,x1);
  tri.setLengthC(x2,x1);
  tri.setHeight(y3,y1);
  // set calculations off the data
  tri.setAngleA();
  tri.setAngleB();
  tri.setAngleC();

  double perimeter = tri.calcPerimeter();

  double area = tri.calcArea();

  System.out.printf("Set up triangle with coordinates (" + x1 + "," + y1 + "), (" + x2 + "," + y2 + "), (" + x3 + "," + y3 + ")");
  System.out.printf("\nArea:\t\t\t\t" + area);
  System.out.printf("\nPerimeter:\t\t" + perimeter);
  System.out.printf("\nLength side a:\t" + tri.lengthA);
  System.out.printf("\nLength side b:\t" + tri.lengthB);
  System.out.printf("\nLength side c:\t" + tri.lengthC);
  System.out.printf("\nHeight h:\t\t" + tri.height);
  System.out.printf("\nAngle A:\t\t\t" + tri.angleA);
  System.out.printf("\nAngle B:\t\t\t" + tri.angleB);
  System.out.printf("\nAngle C:\t\t\t" + tri.angleC);

}

}

和你的班级

  /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package triangles;

/**
 *
 * @author jstil
 */
public class Triangle {

    private double x1;
   private double y1;
   private double x2;
   private double y2;
   private double x3;
   private double y3;
   public double lengthA;
   public double lengthB;
   public double lengthC;
   public double angleA;
   public double angleB;
   public double angleC;
   private double perimeter;
   public double height;
   private double area;

   public double calcArea()
   {
      area = .5 * lengthC * height;
      return area;
   }

   public double calcPerimeter()
   {
      perimeter = lengthA + lengthB + lengthC;
      return perimeter;
   }

   public void setLengthA( double x2 , double x3)
   {
      lengthA = Math.sqrt(Math.pow((x2 - x3),2) + Math.pow(height,2));

   }

   public void setLengthB(double x3, double x1)
   {
      lengthB = Math.sqrt(Math.pow((x3 - x1),2) + Math.pow(height,2));

   }

   public void setLengthC(double x2, double x1)
   {
      lengthC = x2 - x1;

   }

   public void setHeight(double y3, double y1)
   {
      height = y3 - y1;

   }

   public void setAngleA()
   {
      angleA = Math.abs(Math.toDegrees(Math.asin(height/ lengthB)));

   }

   public void setAngleB()
   {
      angleB = Math.abs(Math.toDegrees(Math.asin(height / lengthA)));

   }

   public void setAngleC()
   {
      angleC = 180 - angleA - angleB;

   }
}