Write a java program that has the following input and output Input from user: St
ID: 3563363 • Letter: W
Question
Write a java program that has the following input and output
Input from user:
Start Time (in hh:mm:ss 24 hr format) Start date: (in mm/dd/yyyy format)
End Time (in hh:mm:ss 24 hr format) End Date (in mm/dd/yyyy format)
Distance: (in feet)
and have the output calculate the total amount of hours minutes and seconds from start to end.
Also have and input of distance in ft (ex. 50000 ft) and have the the output of the previous (total time duration) divided by the ft to get a speed in ft/sec.
Output:
Duration (hh:mm:ss)
Speed (ft/sec)
Please Help! Thanks
Explanation / Answer
================================================
Program is:
==================================================
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SpeedCalculator {
public static void main(String[] args) {
String startTime;
String endTime;
long distance;
long totalSeconds;
Scanner sc = new Scanner(System.in);
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
Date d1 = null;
Date d2 = null;
try {
System.out.println("Enter Start Time (in hh:mm:ss 24 hr format) Start date: (in dd/mm/yyyy format) followed by space (Ex:12:13:23 11/12/2014)");
startTime = sc.next();
startTime = startTime +" "+ sc.next();
System.out.println("Enter Start Time (in hh:mm:ss 24 hr format) Start date: (in dd/mm/yyyy format) followed by space (Ex:12:24:23 13/12/2014) ");
endTime = sc.next();
endTime = endTime +" "+ sc.next();
System.out.println("Enter total distance in feets ");
distance = sc.nextLong();
d1 = format.parse(startTime);
d2 = format.parse(endTime);
//in milliseconds
long time = d2.getTime() - d1.getTime();
long seconds = time / 1000 % 60;
long minutes = time / (60 * 1000) % 60;
long hours = time / (60 * 60 * 1000) % 24;
long days = time / (24 * 60 * 60 * 1000);
//Calculating total hours
hours = hours + days*24;
//Calculating total seconds
totalSeconds = seconds + minutes*60 + hours*60*60;
System.out.println("Duration (hh:mm:ss) is :"+formatOutout(hours)+":"+formatOutout(minutes)+":"+formatOutout(seconds));
System.out.println("Speed (ft/sec) is:"+(double)distance/totalSeconds);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Method to format output
* @param value
* @return
*/
static String formatOutout(long value) {
if(value >9 ) {
return String.valueOf(value);
} else {
return "0"+String.valueOf(value);
}
}
}
=============================================
Sample Input and output
=========================================
Enter Start Time (in hh:mm:ss 24 hr format) Start date: (in dd/mm/yyyy format) followed by space (Ex:12:13:23 11/12/2014)
10:12:13 28/09/2014
Enter Start Time (in hh:mm:ss 24 hr format) Start date: (in dd/mm/yyyy format) followed by space (Ex:12:24:23 13/12/2014)
12:13:14 29/09/2014
Enter total distance in feets
123456
Duration (hh:mm:ss) is :26:01:01
Speed (ft/sec) is:1.3181153308207259
===============================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.