2014-02-26 32 views
0

我爲班級編寫了一個程序,我相信這個問題是我在測試其他班級的時候。如何處理Java中的對象中的浮點?

這裏是我粘貼的ideone.com的鏈接。

https://ideone.com/UB7x87 -

https://ideone.com/watbZq - 測試

import static java.lang.Math.sqrt; 


public class ThreePoints { 

    float x0; 
    float y0; 
    float x1; 
    float y1; 
    float x2; 
    float y2; 


    public ThreePoints(){ 
     this.x0 = 0; 
     this.y0 = 0; 
     this.x1 = 0; 
     this.y1 = 0; 
     this.x2 = 0; 
     this.y2 = 0; 

    } 
    public ThreePoints(float x0, float y0, float x1, float y1, float x2, float y2){ 
     this.x0 = x0; 
     this.y0 = y0; 
     this.x1 = x1; 
     this.y1 = y1; 
     this.x2 = x2; 
     this.y2 = y2; 
    } 
    public double getLength(int side){ 


    if(side == 0 && isTriangle()){ 
     return Math.sqrt((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1)); 
    } else if (side == 1 && isTriangle()){ 
     return Math.sqrt((x2-x0) * (x2-x0) + (y2-y0) * (y1-y0)); 
    } else if (side == 2 && isTriangle()){ 
     return Math.sqrt((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0)); 
    }else{ return 0; 

    } 
    } 
    public double getAngle(int vertex){ 

      if(vertex == 0 && isTriangle()) { 
    double angle = Math.acos((-Math.pow(this.getLength(0),2) 
       +Math.pow(this.getLength(1),2) 
       +Math.pow(this.getLength(2),2)) 
       /(2*this.getLength(1)*this.getLength(2))); 
       return angle; 
      } else if(vertex == 1 && isTriangle()) { 
    double angle = Math.acos((Math.pow(this.getLength(0),2)- Math.pow(this.getLength(1),2) 
       +Math.pow(this.getLength(2),2))/ 
       (2*this.getLength(0)*this.getLength(2))); 
       return angle; 
      } else if(vertex == 2 && isTriangle()){ 
    double angle = Math.acos((Math.pow(this.getLength(0),2) 
       +Math.pow(this.getLength(1),2)- Math.pow(this.getLength(2),2)) /(2*this.getLength(0)*this.getLength(1))); 
       return angle; 
      } else { 
       return 0; 
        } 


      } 
    public boolean isTriangle(){ 

if (!(x0 * (y1-y2) + x1 * (y2-y0) + x2 * (y0-y1) == 0)){ 
    return true; 
} 
    return false; 
    } 
    public boolean isEquilateral(){ 
      if (getLength(0) != getLength(1)) 
       return false; 
      if (getLength(0) != getLength(2)) 
       return false; 
      if (getLength(1) != getLength(2)) 
       return false; 
      return true; 
    } 
    public boolean isIsosceles(){ 
     if (getLength(0) == getLength(1)) 
      return true; 
     if (getLength(0) == getLength(2)) 
      return true; 
     if (getLength(1) == getLength(2)) 
      return true; 
     return false; 

    } 
    public boolean isScalene(){ 
     if (getLength(0) == getLength(1)) 
      return true; 
     if (getLength(0) == getLength(2)) 
      return true; 
     if (getLength(1) == getLength(2)) 
      return true; 
     return false; 
    } 
    public boolean isAcute(){ 
     if (getAngle(0) < 90 && getAngle(1) < 90 && getAngle(2) < 90){ 
      return true; 
     } 
     return false; 
    } 
    public boolean isObtuse(){ 
     if (getAngle(0) > 90 && getAngle(1) > 90 && getAngle(2) > 90){ 
      return true; 
     }  
     return false; 
    } 
    public double getPerimeter(){ 
     double perimeter; 
     perimeter = (getLength(0) + getLength(1) + getLength(2)); 
     return perimeter; 
    } 
    public boolean isRight(){ 
    if (getAngle(0) == 90 && getAngle(1) == 90 && getAngle(2) == 90){ 
    return true; 
    } 
    return false; 
    } 
    public double getArea(){ 
    double s = ((getLength(0) + getLength(1) + getLength(2))/2);  
    double area = sqrt(s * (s-getLength(0))*(s-getLength(1))*(s-getLength(2))); 

     return area; 
    } 
    private boolean approxEqual (double x, double y) { 
     return Math.abs(x - y) <= 1E-12; 
    } 
    } 

這裏是測試類

 public class ThreePointsTester { 
    public static void main(String[] args) { 

    ThreePoints tp = new ThreePoints(); 

    double tpAngle0 = tp.getAngle(0); 
    double tpAngle1 = tp.getAngle(1); 
    double tpAngle2 = tp.getAngle(2); 

System.out.println("tp angle 0 is " + tpAngle0); 
System.out.println("tp angle 1 is " + tpAngle1); 
System.out.println("tp angle 2 is " + tpAngle2); 

    double tpLength0 = tp.getLength(0); 
    double tpLength1 = tp.getLength(1); 
    double tpLength2 = tp.getLength(2); 

System.out.println("tp side 0 is " + tpLength0); 
System.out.println("tp side 1 is " + tpLength1); 
System.out.println("tp side 2 is " + tpLength2); 

tp.isTriangle(); 
tp.isEquilateral(); 
tp.isIsosceles(); 
tp.isScalene(); 
tp.isAcute(); 
tp.isObtuse(); 
tp.isRight(); 

---------------- - ----------------------------- *

ThreePoints tp2 = new ThreePoints(3.32, 2.32, 2.12, 4.54, 5.43, 4.23); 

    double tpAngle0 = tp2.getAngle(0); 
    double tpAngle1 = tp2.getAngle(1); 
    double tpAngle2 = tp2.getAngle(2); 

    System.out.println("tp2 angle 0 is " + tpAngle0); 
    System.out.println("tp2 angle 1 is " + tpAngle1); 
    System.out.println("tp2 angle 2 is " + tpAngle2); 

    double tpLength0 = tp2.getLength(0); 
    double tpLength1 = tp2.getLength(1); 
    double tpLength2 = tp2.getLength(2); 

    System.out.println("tp2 side 0 is " + tpLength0); 
    System.out.println("tp2 side 1 is " + tpLength1); 
    System.out.println("tp2 side 2 is " + tpLength2); 

tp2.isTriangle(); 
tp2.isEquilateral(); 
tp2.isIsosceles(); 
tp2.isScalene(); 
tp2.isAcute(); 
tp2.isObtuse(); 
tp2.isRight(); 






} 

} 

看到---------- ----------------

當我試圖創建對象,Java正在給我一個錯誤。我對Java還是比較新的,所以我只是想明白我做錯了什麼。另外我的布爾方法似乎不打印真或假?

+5

你得到了什麼錯誤? – Kakarot

+0

方法不會打印任何東西,除非它們有打印內容的語句。這些方法返回「true」或「false」,但你只是拋出結果。嘗試'System.out.println(「isTriangle =」+ tp.isTriangle())'或類似的東西。 – ajb

+0

「給我一個錯誤」不是錯誤描述。 Java產生了一些相當不錯的錯誤消息。在這裏複製郵件(編輯原始帖子並粘貼),並指出郵件標識哪一行。 –

回答

2

試試這個。

new ThreePoints(3.32f, 2.32f, 2.12f, 4.54f, 5.43f, 4.23f)

這樣的數字文字是由默認雙打。
您需要添加一個f以使它們明確浮動。

不確定你如何測試布爾方法
只要你的代碼不能編譯。

+0

太棒了,謝謝!我很少使用浮動。現在我明白了! – Frightlin

0

它看起來像你複製你的變量在這裏(tpAngleXtpLengthX):

double tpAngle0 = tp.getAngle(0); 
double tpAngle1 = tp.getAngle(1); 
double tpAngle2 = tp.getAngle(2); 

... 

double tpAngle0 = tp.getAngle(0); 
double tpAngle1 = tp.getAngle(1); 
double tpAngle2 = tp.getAngle(2); 

如果你想在同樣的方法重複使用您的變量,你只需要一次聲明它:

double tpAngle0 = tp.getAngle(0); 

... 

tpAngle0 = tp.getAngle(0);