Monday, January 16, 2012

Java Programming Trip Time Program

Question

Write an application that prompts for and reads integer values for speed and distance traveled, then prints the time required for the trip as a floating point result.



Solution

//********************************************************************
// TripTime.java // CS151A, Assignment 2 PP 2.7
// This class prints the time required for a trip based on speed and distance inputs
//********************************************************************

import java.util.Scanner;

public class TripTime
{
    public static void main (String[]args)
    {
        Scanner scan = new Scanner (System.in);
        int Kilometers;
        double Speed;
        double Travel_Time;
        System.out.println ("Enter the distance travelled.");
        Kilometers = scan.nextInt();
        System.out.println ("Enter the travel speed.");
        Speed = scan.nextDouble();
        Travel_Time = Kilometers / Speed;
        System.out.println ("Time required for trip: " + Travel_Time + " hours");
    }
}

 

No comments:

Post a Comment