The problem Assume a runner runs 24 miles in 1 hour, 40 minutes, and 35 seconds. In this instance , write a program that displays the average speed in kilometers per hour. (Note that 1 mile is 1.6 kilometers.)
Breaking it down private static double HOURS = 1 ;
private static double MINUTES = 40 ;
private static double SECONDS = 35 ;
private static double DISTANCE_IN_MILES = 24 ;
public static void main ( String [] strings ) {
double distanceInKilometers = DISTANCE_IN_MILES * 1.6 ;
double timeInMinutes = convertToTimeInMinutes ( HOURS , MINUTES , SECONDS );
double kilometersPerHour = kiloMetersPerHour ( distanceInKilometers ,
timeInMinutes );
System . out . println ( kilometersPerHour );
}
private static double kiloMetersPerHour ( double distanceInKilometers ,
double timeInMinutes ) {
double kilometersPerHour = 60.0 * distanceInKilometers / timeInMinutes ;
return kilometersPerHour ;
}
private static double convertToTimeInMinutes ( double hours , double minutes ,
double seconds ) {
double timeInMinutes = ( hours * 60.0 ) + minutes + ( seconds / 60.0 );
return timeInMinutes ;
}
Output 22.906379453189732
Average speed in kilometers posted by Justin Musgrove on 31 March 2016
Tagged: java, java-exercises-beginner, intro-to-java-10th-edition, and ch1
Share on: Facebook Google+