Thursday, 27 September 2012

FeetInches Code


Question:


FeetInches Class – Sample Program for understanding
The class has two int fields: feet and inches. Together these fields hold a distance measured in feet and inches, such as 12 feet 7 inches. The feet field holds the feet part and the inches field holds the inches part.
Method Description
Constructor #1 This constructor assigns 0 to both the feet and inches fields. (no-arg constructor)
Constructor #2 This constructor accepts two int arguments that are assigned to the feet and inches fields. The simplify method is also called.
Constructor #3 This constructor accepts a FeetInches object as its argument. The object that is being created will become a copy of the object passed as an argument. This type of constructor is sometimes referred to as a “copy constructor.”
simplify This method adjusts any set of values where the inches field is greater than 11. For example, 3 feet 14 inches would be adjusted to read 4 feet 2 inches.
setFeet This method accepts an int argument that is assigned to the feet field.
setInches This method accepts an int argument that is assigned to the inches field. The simplify method is then called.
getFeet This method returns the value in the feet field.
getInches This method returns the value in the inches field.
toString This method returns a string representing the distance held by the object. For example, if an object’s feet field holds 3 and its inches field holds 7, the toString method would return the string “3 feet 7 inches”.
add This method accepts a FeetInches object as its argument. It returns a reference to a FeetInches object that is the sum of the calling object and the object that was passed as the argument.
equals This method accepts a FeetInches object as its argument. It returns the boolean value true if the calling object and the argument object hold the same data. Otherwise it returns false.
copy This method returns a reference to a new FeetInches object that is a copy of the calling object.


 Code:


public class FeetInches
{
private int feet; // The number of feet
private int inches; // The number of inches
public FeetInches()
{
feet = 0;
inches = 0;
}
public FeetInches(int f, int i)
{
feet = f;
inches = i;
simplify();
}
public FeetInches(FeetInches object2)
{
feet = object2.feet;
inches = object2.inches;
}
private void simplify()
{
if (inches > 11)
{
feet = feet + (inches / 12);
inches = inches % 12;
}
}
public void setFeet(int f)
{
feet = f;
}
public void setInches(int i)
{
inches = i;
simplify();
}
public int getFeet()
{
return feet;
}
public int getInches()
{
return inches;
}
public String toString()
{
String str = feet + " feet " +  inches + " inches";
return str; }
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);
}
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;
}
public FeetInches copy()
{
FeetInches newObject = new FeetInches(feet, inches);
return newObject; }

public static void main(String[] args) {

FeetInches newObject = new FeetInches(10,20);
System.out.println(newObject);

}

}


No comments:

Post a Comment