2017-10-04 141 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; 

    } 
} 
相關問題