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

in JAVA: Create the following class. Read the entire question first! Write a cla

ID: 3754915 • Letter: I

Question

in JAVA:

Create the following class. Read the entire question first!

Write a class Distance with the following specifications:

Class variables:

a private static integer called INCHES_IN_A_FOOT with a value 12;

a private static float called FEET_IN_A_METER with a value of 0.3048f;

Instance variables:

Two integer instance variables: feet and inches;

Constructors:

a no argument constructor to initialize a Distance with zero feet, zero inches.

a two argument constructor that will accept two positive integers and for feet and inches. An “IllegalArgumentException” is thrown if the number of inches is more than 11.

Get/Set methods:

get methods for the instance variables;

set methods for instance variables.

Public methods:

1. a method add() that adds another Distance object to itself.

That is,

w1 = new Distance(4,9);

w2 = new Distance (3,6);

w1.add(w2); // w1 becomes 8 feet, 3 inches

2. a method metricDistance() that will return (as a float) the number of meters in the distance represented by the object.

That is, for w1 above, w1.metricDistance() returns 1.4478f

Explanation / Answer

class Distance{
private static int INCHES_IN_A_FOOT = 12;
private static float FEET_IN_A_METER = 0.3048f;
int feet, inches;
Distance(){
feet = 0;
inches = 0;
}
Distance(int feet, int inches){
this.feet = feet;
if(inches>11)
throw new IllegalArgumentException("Inches should be lessthan 12");
this.inches = inches;
}
void setFeet(int feet){
this.feet = feet;
}
void setInches(int inches){
this.inches = inches;
}
int getFeet(){
return feet;
}
int getInches(){
return inches;
}
public void add(Distance d){
int totalInches = this.getInches()+d.getInches();
this.setFeet(this.getFeet()+d.getFeet()+totalInches/INCHES_IN_A_FOOT);
this.setInches(totalInches%INCHES_IN_A_FOOT);
}
public float metricDistance(){
float totalFeet = getFeet()+getInches()*1/(float)INCHES_IN_A_FOOT;
return totalFeet*FEET_IN_A_METER;
}
}

//any query, post in the comment section.

//give a thumbs up if you like the answer.