Question: The following program implements and tests a length class that deals with feet / inches measurements. It uses two private data members named feet and
The following program implements and tests a length class that deals with feetinches
measurements. It uses two private data members named feet and inches.
Feetlnchesjava
darr
darr
In JAVA, Reimplement this class to use only one private data member, allinches.
The behavior of the class must be identical to the original implementation. You should change
only the Feetlnches class and not alter the public main class FeetInches at all. The results
using your modified should be identical to the results obtained from the original program.
ProgrPage
of
Main Class
public class FeetInches
public static void main String args
FeetInches f new FeetInches;
FeetInches g new FeetInches;
System.out.printlnf g faddg
;
System.out.printlnf f
;
fsetFeet;
System.out.printlncalled fsetFeet;
System.out.printlnf f
;
System.out.printlng g
;
gsetInches;
System.out.printlncalled gsetInches;
System.out.printlnf f;
System.out.printlng g
;
System.out.printlnf g faddg
;
System.out.printlnfequalsg returns fequalsg;
g f;
System.out.printlnexecuting g f;
System.out.printlnfequalsg returns fequalsg;
The FeetInches class holds distances measured in
feet and inches.
class FeetInches
private int feet; The number of feet
private int inches; The number of inches
This constructor assigns to the feet
and inches fields.
public FeetInches
feet ;
inches ;
This constructor accepts two arguments which
are assigned to the feet and inches fields.
The simplify method is then called.
@param f The value to assign to feet.
@param i The value to assign to inches.
public FeetInchesint f int i
feet f;
inches i;
simplify;
The following is a copy constructor. It accepts a
reference to another FeetInches object. The feet
and inches fields are set to the same values as
those in the argument object.
@param object The object to copy.
public FeetInchesFeetInches object
feet objectfeet;
inches objectinches;
The simplify method adjusts the values
in feet and inches to conform to a
standard measurement.
private void simplify
if inches
feet feet inches ;
inches inches ;
The setFeet method assigns a value to
the feet field.
@param f The value to assign to feet.
public void setFeetint f
feet f;
The setInches method assigns a value to
the inches field.
@param i The value to assign to inches.
public void setInchesint i
inches i;
simplify;
getFeet method
@return The value in the feet field.
public int getFeet
return feet;
getInches method
@return The value in the inches field.
public int getInches
return inches;
toString method
@return a reference to a String stating
the feet and inches.
public String toString
return feet ft inches in;
The add method returns a FeetInches object
that holds the sum of this object and another
FeetInches object.
@param object The other FeetInches object.
@return A reference to a FeetInches object.
public FeetInches addFeetInches object
int totalFeet, To hold the sum of feet
totalInches; To hold the sum of inches
totalFeet feet objectfeet;
totalInches inches objectinches;
return new FeetInchestotalFeet totalInches;
The equals method compares this object to the
argument object. If both have the same values,
the method returns true.
@return true if the objects are equal, false
otherwise.
public boolean equalsFeetInches object
boolean status;
if object null
status false;
else if feet objectfeet &&
inches objectinches
status true;
else
status false;
return status;
