/* This class is designed to manipulate distances like: 3ft 2in or 7ft 11in or ... Several of the methods are not implemented. Your job is to implement them an run the driver program to test if they work properly. */ public class Distance implements DistanceInterface, Comparable{ private int feet; private int inches; public Distance(int f, int i){ feet = f; inches = i; normalize(); //make a change so that a new Distance(3,15) 3ft 15in will create an equivalent Distance // of the form new Distance(4,3) 4ft 3in (hint: implement a normalize() method to do the work). } public Distance(Distance d){ //create a copy of Distance d feet = d.feet; inches = d.inches; } public String toString(){ return feet + "ft " + inches + "in"; } public int feet(){ return feet; } public int inches(){ return inches; } public void setFeet(int f){ feet = f; } public void setInches(int i){ inches = i; } public double convertToInches(){ //convert this Distance to inches return 12.0*feet + inches; } public double convertToFeet(){ //Convert this Distance to feet return feet + inches/12.0; } public boolean equals(DistanceInterface other){ //Return true when this Distance is identical to the other Distance return this.convertToInches() == other.convertToInches(); } public DistanceInterface add(DistanceInterface other){ //return the sum of this Distance and other distance double thisInches = convertToInches(); double otherInches = other.convertToInches(); System.out.println("Other object: "+other+"\n"+"Other inches: "+otherInches); int newInch =(int)thisInches + (int)otherInches; System.out.println(newInch); Distance result = new Distance(0,newInch); result.normalize(); return result; } public int compareTo(Distance other){ //Return 1 when this Distance is greater than the other Distance //Return 0 when this Distance and other are the same double thisInches = this.convertToInches(); double otherInches = other.convertToInches(); if(thisInches < otherInches) return -1; if(thisInches > otherInches) return +1; return 0; // return -1; //return -1 otherwise } private void normalize(){ //used by the creator to present distances to the client //in normal form int tempFeet = this.inches / 12; this.inches = this.inches % 12; this.feet = this.feet + tempFeet; } }