Create a class called Distance that includes two private data members feet (type int) and inches (type float). Use a method setData to set values to instance variables. Provide a method display that displays the feet and inches, a member function addDistance for adding two distances, and a member function compareDistance for comparing two distances. Implement your program in Java.
Updated: Oct. 3, 2023 — Training Time: 2 minutes
Overseen by: Archangel Macsika
All Training Resources
Scroll for more menu list
Topic:Generic - Java Programming
Difficulty: Advance.
Companies who previously asked this: -.
Objective: Create a class called Distance
that includes two private data members feet
(type int
) and inches
(type float
). Use a method setData
to set values to instance variables. Provide a method display that displays the feet and inches, a member function addDistance
for adding two distances, and a member function compareDistance
for comparing two distances. Implement your program in Java.
Input: Numbers to serve as distance values.
Expected Output: Result will vary based on the user input.
Sikademy Solution
package sikademy;
/**
*
* @author Archangel Macsika
*/
// This class has two variables(distances) and functions that adds, compares two objects and displays the result.
public class Distance {
private float feet;
private float inches;
// This method sets the value of two private variables feet and inches
private void setData(float feet, float inch) {
this.feet = feet;
this.inches = inch;
}
// This method displays the distances
private void display() {
System.out.println("The distance in feet is: \t" + this.feet);
System.out.println("The distance in inches is: \t" + this.inches);
}
// This function adds both the distances.
private void addDistances(Distance distance) {
float tempFeet = this.feet + distance.feet;
float tempInch = this.inches + distance.inches;
if (tempInch >= 12) {
tempFeet += tempInch / 12;
tempInch = tempInch % 12;
}
System.out.println("The Total length is: \t" +(int)tempFeet + "feet \t" + tempInch + "inches");
}
// This function compares the distances
private void compareDistances(Distance distance) {
float totalInchesOne=this.feet*12+this.inches;
float totalInchesTwo=distance.feet*12+distance.inches;
float difference=Math.abs(totalInchesOne-totalInchesTwo);
float tempFeet = difference/12;
float tempInch = difference%12;
System.out.println("The Total length is: \t" + (int)tempFeet + "feet \t" + tempInch + "inches");
}
public static void main(String[] args) {
Distance d1 = new Distance();
Distance d2 = new Distance();
d1.setData(5,3);
d1.display();
d2.setData(2,11);
d2.display();
d1.addDistances(d2);
d1.compareDistances(d2);
}
}