2017-10-04 142 views
0

我目前有一个三角形类与我所有的计算。如何在另一个类中的测试类中使用JOptionPane输入?

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和之间的任何连接Triangle类和TriangleTester类的y值。我如何才能让Triangle类使用TriangleTester类中输入的x和y值来计算答案?谢谢。

+0

是否使用该包装的IDE类 主程序之间的setter方法? –

+0

我没有得到角度B,C计算工作,但其余完成 –

回答

0

这里的主要想法是你使用了一个吸气剂作为设置器。 你没有传递数据

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; 

    } 
} 
相关问题