需要在Java中添加两个由Feet组成的对象;英寸

时间:2014-09-12 19:59:51

标签: java object

我有一个问题,我无法完全理解。也许有人可以帮助我。

我创建了一个DISTANCE类,其中包含两个变量FEET&英寸。我需要在该类中添加一个Method,它添加了两个独立的Feet& DISTANCE对象。英寸。

到目前为止,这是我的课程:

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;

}

4 个答案:

答案 0 :(得分:1)

不要尝试将int分配给Distance对象,而是使用构造函数

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

此外,您可能不应将脚和英寸声明为静态变量,因为无论何时修改它,它都会更改为Distance对象的所有。 改变自:

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

为:

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

答案 1 :(得分:0)

您在对象中使用静态变量。每次实例化Distance对象时,都将所有Distance对象设置为这些值。

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

的System.out.println(a.getFeet()); //应该返回3

答案 2 :(得分: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() + "]";
    }
}

答案 3 :(得分: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 + "]";
    }
}