in .java
Write a program that computes the time to arrive to destination.
It asks for the distance and the car speed. It will assume that the
car travels at constant speed at all time.
Sample run 1:
This program will compute the travel time. Enter the distance (miles): 105 Enter the car speed (miles/hour): 75 Travel time: 1.40 or: 1 hour and 23 minutes
Sample run 2:
This program will compute the travel time. Enter the distance (miles): 150 Enter the car speed (miles/hour): 60 Travel time: 2.50 or: 2 hour and 30 minutes
import java.util.Scanner; public class DistanceTravelled { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("This program will compute the travel time."); System.out.print("Enter the distance (miles): "); double distance = in.nextDouble(); System.out.print("Enter the car speed (miles/hour): "); double speed = in.nextDouble(); double time = distance / speed; System.out.printf("Travel time: %.2f\n", time); int hours = (int) time; int minutes = (int) ((time - hours) * 60); System.out.println("or: " + hours + " hour and " + minutes + " minutes"); } }
Get Answers For Free
Most questions answered within 1 hours.