Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

STAGE 1 | The length of an object can be described by two integers: feet and inc

ID: 3909622 • Letter: S

Question

STAGE 1 | The length of an object can be described by two integers: feet and inches (where 12

      inches equals one foot). Class UML is as follows:

Length

- feet : int                                                                                                      

- inches : int                                                                                                                             

+ Length()                                                                                                    

+ Length(newFeet : int, newInches : int)                                                     

+ getFeet() : int

+ setFeet(newFeet : int) : void

+ getInches() : int

+ setInches(newInches : int) : void

+ add(otherLength : Length) : Length

+ subtract(otherLength : Length) : Length

+ equals(otherLength: Length) : boolean

+ compareTo(otherLength: Length) : int

+ toString() : String

-Default constructor sets the length of an object to 0 feet & 0 inches

-Overloaded constructor sets the length of an object to given feet & inches (inches must be in rage)

-For each data field, define the accessor and mutator methods

-add method, adds two length objects and return the result as a length object (feet & inches must be in range)

-Subtract method, subtracts two length objects and return the result as a length object (feet & inches must be in range. this object must be larger than otherLength)

-toString() method returns a String in format of ##’ ##”

    For example:

Length meLength = new Length(10, 6);

    System.out.println( myLength.toString() );

    Will display:      10’ 6”

STAGE 2 | Implement the Length class using the above UML.

STAGE 3 | Compile “Length.java” and fix syntax errors

STAGE 4 | Driver program 1

Implement a driver program called TestLength.java as follows:

1)Create Length object length1 being 10’ 5”

2)Create Length object length2 being 5’ 10”

3)Print the two object

4)Add length1 and length2 and assign the result to length3

5)Print out length1 + length2 = length3

6)Subtract length2 from length1 and assign the result to length4

7)Print out length1 - length2 = length3

STAGE 5 | Testing

-Compile “TestLength.java” and fix syntax errors

-Run “TestLength” and fix runtime and logical errors

STAGE 6 | Driver program 2

Implement a driver program called “SortLength.java” as follows:

1) Create an array of 10 Length objects with random values ( 10 <= feet <= 20 & 0 <= inches <= 11)

2) Print the array

3) Sort the array by length in ascending order

4) Print the sorted array

STAGE 7 | Testing

-Compile “SortLength.java” and fix syntax errors

-Run “SortLength” and fix runtime and logical errors

Length

- feet : int                                                                                                      

- inches : int                                                                                                                             

+ Length()                                                                                                    

+ Length(newFeet : int, newInches : int)                                                     

+ getFeet() : int

+ setFeet(newFeet : int) : void

+ getInches() : int

+ setInches(newInches : int) : void

+ add(otherLength : Length) : Length

+ subtract(otherLength : Length) : Length

+ equals(otherLength: Length) : boolean

+ compareTo(otherLength: Length) : int

+ toString() : String

Explanation / Answer

import java.util.*;

class Length implements Comparable<Length>
{

private int feet,inches;

public Length()
{
feet = 0;
inches = 0;
}
public Length(int newFeet, int newInches)
{
feet = newFeet;
inches = newInches;
}
public int getFeet()
{
return feet;
}
public void setFeet(int newFeet )
{
feet = newFeet;
}
public int getInches()
{
return inches;
}
public void setInches(int newInches)
{
inches = newInches;
}
public Length add(Length otherLength)
{
Length l1 = new Length();
l1.feet = this.feet + otherLength.feet;
l1.inches = this.inches + otherLength.inches;
if(l1.inches > 12) // if inches greater than 12,increase feet by 1
{
l1.inches = l1.inches - 12;
l1.feet = l1.feet + 1;
}
return l1;
}
public Length subtract(Length otherLength)
{
Length l1 = new Length();
l1.feet = this.feet - otherLength.feet;
l1.inches = this.inches - otherLength.inches;

return l1;
}
public boolean equals(Length otherLength)
{
if(this.feet == otherLength.feet && this.inches == otherLength.inches)
return true;
else
return false;
}
public int compareTo(Length otherLength)
{
if(this.feet < otherLength.feet)
        return -1;
        else if(this.feet == otherLength.feet && this.inches < (otherLength.inches) ) // feets are same,inches are compared
        return -1;
        else
        return 0;
}
public String toString()
{
return feet+"'"+" "+inches+"" ";
}

}

class TestLength
{
public static void main (String[] args)
{
Length myLength = new Length(10, 6);
System.out.println( myLength.toString());


Length length1 = new Length(10,5);
Length length2 = new Length(5,10);
System.out.println(length1.toString());
System.out.println(length2.toString());


Length length3 = new Length();

length3 = length1.add(length2);

System.out.println(length1.toString()+" + "+length2.toString()+" = "+length3.toString());


Length length4 = new Length();
length4 = length1.subtract(length2);
System.out.println(length1.toString()+" - "+length2.toString()+" = "+length4.toString());



}
}

---------------------------------------------------------------------------------------------------------------

class SortLength
{
public static void main (String[] args)
{










Length[] length = new Length[10];

length[0] = new Length(5,6);
length[1] = new Length(1,6);
length[2] = new Length(12,2);
length[3] = new Length(10,4);
length[4] = new Length(25,8);
length[5] = new Length(9,3);
length[6] = new Length(2,6);
length[7] = new Length(21,3);
length[8] = new Length(10,3);
length[9] = new Length(13,4);

Arrays.sort(length);

System.out.println("Sorted lengths : ");

for(int i=0;i<10;i++)
{
  System.out.println(length[i]);
}

}
}

---------------------------------------------------------------------------------------------------

Output:

10' 6"
10' 5"
5' 10"
10' 5" + 5' 10" = 16' 3"
10' 5" - 5' 10" = 5' -5"
Sorted lengths :
1' 6"
2' 6"
5' 6"
9' 3"
10' 3"
10' 4"
12' 2"
13' 4"
21' 3"
25' 8"

Do ask if any doubt. Please upvote.