JAVA Design a class named Clock. You should use your IDE for this exercise. The
ID: 3678863 • Letter: J
Question
JAVA Design a class named Clock. You should use your IDE for this exercise. The class contains private data fields for startTime and stopTime, a no argument constructor that initializes the startTime to the current time, a method named start() that resets the startTime to the given time, a stop() method that sets the endTime to the given time and a getElapsedTime() method that returns the elapsed time in seconds. Create a TestClock class to construct a Clock instance and return the elapsed time. Command line arguments should be used to send the start and end times. You should use the java.time classes. Here is sample run: java TestClock 11:45:12 11:48:13 Elapsed time in seconds is: 181 THIS MUST BE COMPLETED AS ONE JAVA FILE!!!!!!!!!!!Explanation / Answer
Hey heres your code :
import java.util.*;
import java.text.*;
public class TestClock {
static long startTime,endTime;
public TestClock() {
Date date = new Date();
long startTime=date.getTime();
}
public static void main(String args[]) {
TestClock test=new TestClock();
String s=args[0];
start(s);
String s1=args[1];
stop(s1);
long elapsed=getElapsedTime();
System.out.println(elapsed);
}
public static void start(String s)
{
SimpleDateFormat ft = new SimpleDateFormat ("HH:mm:ss");
String input = s;
Date t;
try {
Date parseDate = ft.parse(input);
startTime=parseDate.getTime();
}
catch (ParseException e) {
;
}
}
public static void stop(String s)
{
SimpleDateFormat ft = new SimpleDateFormat ("HH:mm:ss");
String input = s;
Date t;
try {
Date parseDate = ft.parse(input);
endTime=parseDate.getTime();
}
catch (ParseException e) {
;
}
}
public static long getElapsedTime()
{
long diff=(endTime-startTime)/1000;
return diff;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.