63.7k views
0 votes
This class has one instance variable, a double called miles. The class has methods that convert the miles into different units.It should have the following methods:public Distance(double startMiles) - the constructor; initializes milespublic double toKilometers() - converts the miles to kilometers. To convert to kilometers, divide miles by 0.62137public double toYards() - converts miles to yards. To convert to yards, multiply miles by 1760.public double toFeet() - converts miles to feet. To convert to feet, multiply miles by 5280.public double getMiles() - returns the value of milesMain MethodTo test your class, create three Distance objects in main. One represents the distance between Karel and school, Karel and the park, and Karel and his best friend.Karel lives 5 miles from school. Karel lives 10 miles from the park. Karel lives 12 miles from his best friend.Your program should use the methods from Distance to print the number of:1. yards Karel lives from school2. kilometers Karel lives from the park3. feet Karel lives from his best friend

User Meeech
by
5.6k points

1 Answer

3 votes

Answer:

The class definition with the instance variable and all the required methods is given below:

public class Distance{

double miles;

public Distance (double startMiles) {

this.miles = startMiles;

}

public double toKilometers ( ){

double kilometerValue = miles/0.62137;

return kilometerValue;

}

public double toYards(){

double yardsValue = miles*1760;

return yardsValue;

}

public double toFeet(){

double feetsValue = miles*5280;

return feetsValue;

}

public double getMiles(){

return miles;

}

}

The main method to test the class is given in the explanation section

Step-by-step explanation:

public class Main {

public static void main(String[] args) {

Distance karelToSchool = new Distance(5.0);

Distance karelToPark = new Distance(10.0);

Distance karelToFriend = new Distance (12.0);

double karel_Yards_From_School = karelToSchool.toYards();

System.out.println("Karel's Yards from School is "+karel_Yards_From_School);

double karel_kilometers_from_park = karelToPark.toKilometers();

System.out.println("Karel's Kilometers from Park is "+karel_kilometers_from_park);

double karel_feets_from_friend = karelToFriend.toFeet();

System.out.println("Karel's Feets from Friend is "+karel_feets_from_friend);

}

}

User Sanal Varghese
by
5.6k points