简体   繁体   中英

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. 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. 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 . 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.

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.

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 . 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 ). Then, we are summing two float values, which yields a result of type 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:

@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";
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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