2014-09-12 18 views
0

我有一個問題,我不能籠罩我的腦海。也許有人可以幫助我。需要在Java中添加兩個對象,包括英尺和英寸

我創建了一個DISTANCE類,其中包含兩個變量FEET & INCHES。我需要爲該類添加一個方法,該方法添加兩個單獨的距離對象,英尺爲&英寸。

這是到目前爲止我的課:

public class Distance { 

    static int feet; // 0 - infinity 
    static int inches; // 0 - infinity 

    public Distance() { // default constructor 

     this.feet = 0; 
     this.inches = 0; 
    } 

    public Distance(int ft, int in){ // parametarized constructor 

     this.feet = ft; 
     this.inches = in; 

     System.out.println("constructor w/2 ints : " + feet + ", " + inches); 
    } 

    public void setDistance(int ft, int in){ 

     setFeet(ft); 
     setInches(in);  
    } 

    public static int getFeet() { 

     return (feet); 
    } 

    public static void setFeet(int feet) { 
     Distance.feet = feet; 
    } 

    public static int getInches() { 
     return inches; 
    } 

    public static void setInches(int inches) { 
     Distance.inches = inches; 

    } 



    public Distance(Distance d){ 

     //Distance total = d; 

     d.getDistance(); 

    } 

    private int getDistance() { 
     int totalInches; 

     totalInches = (feet * 12) + inches; 

     return totalInches; 

    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + feet; 
     result = prime * result + inches; 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Distance other = (Distance) obj; 
     if (feet != other.feet) 
      return false; 
     if (inches != other.inches) 
      return false; 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "Distance [feet=" + feet + ", inches=" + inches + "]"; 
    } 


} 

的方法我想寫需要添加到遠處的物體。這是我到目前爲止已經試過:如果

公衆距離加(距離d){

Distance total; 

    total = this.getDistance() + d.getDistance(); 

    return total; 

} 
+0

好的,它出了什麼問題? – qbit 2014-09-12 20:02:20

+0

我建議在內部只使用英寸。 – Fildor 2014-09-12 20:05:44

+0

以英寸爲單位執行算術運算,然後轉換回英尺和英寸。 – 2014-09-12 20:12:31

回答

1

不要試圖一個int分配給Distance對象,請使用您的構造

public Distance add(Distance d){ 
    Distance total = new Distance(this.feet + d.feet, this.inches + d.inches); 
    return total; 
} 

此外,您應該不會將英尺和英寸聲明爲靜態變量,因爲每當您修改它時,它都將更改爲您的距離對象的所有的。 變化來自:

static int feet; // 0 - infinity 
static int inches; // 0 - infinity 

到:

private int feet; // 0 - infinity 
private int inches; // 0 - infinity 
0

您正在使用對象靜態變量。每次您實例化一個距離對象時,您都將所有距離對象都設置爲這些值。

距離a =新距離(1,3); 距離b =新距離(3,1);

System.out.println(a.getFeet()); //應該返回3

0

爲此,您可以在您的代碼

public static Distance add(Distance d1,Distance d2){ 
    return new Distance(d1.getFeet() + d2.getFeet(), d1.getInches() + d2.getInces); 
} 

整個代碼應更改爲以下

public class Distance { 

    private int feet; 
    private int inches; 

    public Distance(int ft, int in){ // parametarized constructor 
     this.feet = ft; 
     this.inches = in; 
     System.out.println("constructor w/2 ints : " + feet + ", " + inches); 
    } 

    public int getFeet() { 

     return feet; 
    } 

    public int getInches() { 
     return inches; 
    } 

    public int getDistance() { 
     return (feet * 12) + inches; 
    } 

    public static Distance add(Distance d1,Distance d2){ 
     return new Distance(d1.getFeet() + d2.getFeet(), d1.getInches() + d2.getInces); 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + feet; 
     result = prime * result + inches; 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Distance other = (Distance) obj; 
     if (feet != other.getFeet()) 
      return false; 
     if (inches != other.getInches()) 
      return false; 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "Distance [feet=" + getFeet() + ", inches=" + getInches() + "]"; 
    } 
} 
0

你也可以做這樣的:

public class Distance { 

    private int feet; 
    private int inches; 

    public Distance(int feet, int inches) { 
     this.feet = feet; 
     this.inches = inches; 
    } 

    public int getDistanceInInches() { 
     return feet * 12 + inches; 
    } 

    public Distance add(Distance distance) { 
     return new Distance(this.feet + distance.feet, this.inches + distance.inches); 
    } 

    @Override 
    public String toString() { 
     return "Distance [feet=" + feet + ", inches=" + inches + "]"; 
    } 
} 
+0

注意通過改變調用對象來破壞其中的值。問題說要添加2個對象,並把它放在3個對象... – StackFlowed 2014-09-12 20:18:32

+0

好點。更正 – TheCodingFrog 2014-09-12 20:29:41

相關問題