简体   繁体   English

两个对象之间的距离使用方法比较

[英]Distance between two objects using method Comparison

After researching a little bit, I couldn't figure out how to create a obj1 distance to be able to compare with obj2. 经过一番研究后,我无法弄清楚如何创建obj1距离以便能够与obj2进行比较。 All these methods were given in assessment I had so, no chance to change logic of it. 所有这些方法都是在评估中给出的,没有机会改变它的逻辑。 I suppose to return 3 Strings answer depending of the data. 我想根据数据返回3个字符串答案。 Thanks a lot in advance guys. 非常感谢你们。 I've attached a pease of pic. 我附上了一张照片。

enter image description here 在此输入图像描述

public class Main {

    public static void main(String[] args) {


        Distance dist1 = new DistanceImplementation();
        Distance obj2 = new DistanceImplementation();

        dist1.setFeetAndInches(1, 8);
        obj2.setFeetAndInches(3, 5);

        System.out.println(dist1.getDistanceComparison(obj2));

    }
}

public abstract class Distance {

    protected int feet;
    protected float inches;

    abstract public void setFeetAndInches(int feet, float inches);

    abstract public int getFeet();

    abstract public float getInches();

    abstract String getDistanceComparison(Distance dist2);

}

class DistanceImplementation extends Distance {


    @Override
    public void setFeetAndInches(int feet, float inches) {
        this.feet = feet;
        this.inches = inches;
    }

    @Override
    public int getFeet() {
        return this.feet;
    }

    @Override
    public float getInches() {
        return this.inches;
    }

    @Override
    String getDistanceComparison(Distance dist2) {

        // if (dist2) { ????????????

        return null;
    }
}

Well, after reading the assessment, I think that you can safely assume that 1 foot = 12 inches . 那么,在阅读评估后,我认为你可以放心地假设1 foot = 12 inches So, in order to correctly implement the getDistanceComparison method, you could calculate the total distance in inches for both the current object and the parameter, compare them and then return the corresponding string value. 因此,为了正确实现getDistanceComparison方法,您可以计算当前对象和参数的总距离( 以英寸为单位),比较它们然后返回相应的字符串值。

Suppose you have the following method: 假设您有以下方法:

private float getTotalInches() {
    return (float) feet * 12.0 + inches;
}

This method returns the total inches of this DistanceImplementation instance, taking into account the feet and the inches attributes. 此方法返回此DistanceImplementation实例的总英寸,同时考虑了feetinches属性。

Please note that for the total result to be of type float , we need to first cast the feet attribute to float , so that it actually becomes of type float . 请注意,总的结果是类型的float ,我们需要先 feet属性float ,因此,它实际上变成类型的float Then, we multiply by 12.0 (note the .0 , it's important because it indicates that the 12.0 literal value is also a float ). 然后,我们乘以12.0 (注意.0 ,这很重要,因为它表明12.0字面值也是一个float )。 Then, we are summing two float values, which yields a result of type float . 然后,我们将两个float值相加,得到float类型的结果。 While all this casting and convertions are not always necessary (sometimes the compiler is smart enough as to guess the correct types and preserve decimal precision), it's considred good practice to make your intentions crystal-clear, so that future developers that will maintain your code know what you have tried to accomplish. 虽然所有这些转换和转换并不总是必要的(有时编译器足够聪明,可以猜测正确的类型并保持小数精度),但是为了让您的意图清晰明确,我们认为这是一种很好的做法,以便未来的开发人员能够维护您的代码知道你试图完成的事情。

Then, once you have this method, it would be easy to compare the total inches of both DistanceImplementation instances and return the corresponding string: 然后,一旦你有了这个方法,就可以很容易地比较两个DistanceImplementation实例的总英寸并返回相应的字符串:

@Override
String getDistanceComparison(Distance dist2) {

    float myTotalInches = getTotalInches();
    float otherTotalInches = dist2.getTotalInches();

    if (myTotalInches > otherTotalInches) {
        // return ...
    } else if (myTotalInches < otherTotalInches) {
        // return ...
    } else {
        // return ...
    }
}

Here is the solution on which I was working and it might be useful as well 这是我工作的解决方案,它也可能有用

package com.prog;

import java.util.Scanner;

abstract class Distance {
    protected int feet;
    protected float inches;

    abstract public void setFeetAndInches(int feet, float inches);
    abstract public int getFeet();
    abstract public float getInches();
    abstract String getDistanceComparison(Distance dist2);

}

public class DistanceCalculator {
private static final Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
        Distance dist1 = new DistanceImplementation();
        Distance dist2 = new DistanceImplementation();

        int feet1 = 1;
        float inches1 = (float) 2.0;

        int feet2 = 3;
        float inches2 = (float) 4.1;

        dist1.setFeetAndInches(feet1, inches1);
        dist2.setFeetAndInches(feet2, inches2);

        System.out.println(dist1.getDistanceComparison(dist2));
    }
}

package com.prog;

public class DistanceImplementation extends Distance {

    @Override
    public void setFeetAndInches(int feet, float inches) {
        this.feet=(int) (feet+ (inches/12));
        this.inches=inches+ (feet*12);

    }

    @Override
    public int getFeet() {

        return feet;
    }

    @Override
    public float getInches() {

        return inches;
    }

    @Override
    String getDistanceComparison(Distance dist2) {
        String ret;
        int dist1a=this.getFeet();
        System.out.println(dist1a);
        int dist2a=dist2.getFeet();
        if(dist1a > dist2a)
            return "First is greater";
        else if(dist1a < dist2a)
            return "Second is greater";
        else


    return "Both are equal";
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM