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 feet/inches
measurements. It uses two private data members named feet and inches.
Feetlnches_02.java
darr
darr
In JAVA, Re-implement this class to use only one private data member, all_inches.
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_02 at all. The results
using your modified should be identical to the results obtained from the original program.
ProgrPage
4
of 4
//====================================================================
// Main Class
//
public class FeetInches_02
{
public static void main (String[] args)
{
FeetInches f = new FeetInches(2,6);
FeetInches g = new FeetInches(3,8);
System.out.println(f +"+"+ g +"="+ f.add(g)+'
');
System.out.println("f ="+ f +'
');
f.setFeet(4);
System.out.println("called f.setFeet(4)");
System.out.println("f ="+ f +'
');
System.out.println("g ="+ g +'
');
g.setInches(7);
System.out.println("called g.setInches(7)");
System.out.println("f ="+ f);
System.out.println("g ="+ g +'
');
System.out.println("f + g ="+ f.add(g)+'
');
System.out.println("f.equals(g) returns "+ f.equals(g));
g = f;
System.out.println("executing g = f");
System.out.println("f.equals(g) returns "+ f.equals(g));
}
}
//===================================================
/**
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 0 to the feet
and inches fields.
*/
public FeetInches()
{
feet =0;
inches =0;
}
//------------------------------------------------
/**
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 FeetInches(int 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 object2 The object to copy.
*/
public FeetInches(FeetInches object2)
{
feet = object2.feet;
inches = object2.inches;
}
//------------------------------------------------
/**
The simplify method adjusts the values
in feet and inches to conform to a
standard measurement.
*/
private void simplify()
{
if (inches >11)
{
feet = feet +(inches /12);
inches = inches %12;
}
}
//------------------------------------------------
/**
The setFeet method assigns a value to
the feet field.
@param f The value to assign to feet.
*/
public void setFeet(int f)
{
feet = f;
}
//------------------------------------------------
/**
The setInches method assigns a value to
the inches field.
@param i The value to assign to inches.
*/
public void setInches(int 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 object2 The other FeetInches object.
@return A reference to a FeetInches object.
*/
public FeetInches add(FeetInches object2)
{
int totalFeet, // To hold the sum of feet
totalInches; // To hold the sum of inches
totalFeet = feet + object2.feet;
totalInches = inches + object2.inches;
return new FeetInches(totalFeet, 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 equals(FeetInches object2)
{
boolean status;
if (object2== null)
status = false;
else if (feet == object2.feet &&
inches == object2.inches)
status = true;
else
status = false;
return status;
}
//--------

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!