Text Share Online

Question

Implement a class to compare distances given in feet and inches. You have an abstract class with predefined fields and methods. The class instances will store distances in feet and inches.

 

Implement the required methods to:

  1. Initialize the class instances
  2. Determine which of the distances is greater, if any

Use the abstract class named Distance with its fields and methods as the foundation for your implementation.

 

The following code is provided in the locked portion of the editor:

  • The declaration for an abstract class named Distance with the following fields and methods:
Fields
Name Type Description
feet integer A private instance variable to store the number offeet in the Distance object.
inches floating-point A private instance variable to store the number ofinches in the Distance object.
The total value represented by a Distance object is a combination of its feet and inches. For example, iffeet = 1 and inches = 6, the total length represented by the Distance object is18 inches or 1.5 feet.
Methods
Return Type Method Name Parameter Type Param. Name Description
void setFeetAndInches integer feet Assign a value to the feet instance variable.
floating-point inches Assign a value to the inches instance variable.
integer getFeet no parameters Returns the value of the feet instance variable.
float getInches no parameters Returns the value of the inches instance variable.
string getDistanceComparison Distance dist2

Compares the distance between the object it is called on and theDistance object passed as an argument (i.e., dist2) and returns one of the following strings:

  • If the object’s distance is greater thandist2‘s, return First distance is greater.
  • If dist2‘s distance is greater than the object’s, return Second distance is greater.
  • If both distances are equal, returnBoth distances are equal.
  • A main method that does the following:
    • Creates a DistanceImplementation object named dist1.
    • Calls the setFeetAndInches method on dist1.
    • Creates a DistanceImplementation object named dist2.
    • Calls the setFeetAndInches method on dist2.
    • Calls getDistanceComparison on dist1 and passes dist2 to the function as an argument.

 

Complete the partially implemented code in the editor according to the specifications. The getDistanceComparison method must return a string that denotes the result of the distance comparison as described above.

 

Constraints

  • 1 ≤ feet ≤ 100
  • 1 ≤ inches ≤ 100
Share This: