Please Help!!! Why won\'t my code compile for this assignment??? //Clock class t
ID: 3673128 • Letter: P
Question
Please Help!!! Why won't my code compile for this assignment???
//Clock class that sets the start time and stop time
//and elapsed time between two times
//Clock.java
import java.sql.Time;
public class Clock
{
//Create two instance of Time class with default values
//for hour,minute and seconds
private Time startTime=new Time(0, 0, 0);
private Time stopTime=new Time(0, 0, 0);
//set start time
public void start(Time startTime)
{
this.startTime=startTime;
}
//set end time
public void stop(Time stopTime)
{
this.stopTime=stopTime;
}
/**The method getElapsedTime calculates the time difference
* between two times and returns seconds as return value.
* */
public int getElapsedTime()
{
int totalSeconds=0;
int hours=stopTime.getHours()-startTime.getHours();
if(hours>0)
{
int minues=stopTime.getMinutes()-startTime.getMinutes();
if(minues>0)
{
minues=stopTime.getMinutes()-startTime.getMinutes();
}
else
minues=startTime.getMinutes()-stopTime.getHours();
int seconds=stopTime.getSeconds()-startTime.getSeconds();
if(minues>0)
{
seconds=stopTime.getSeconds()-startTime.getSeconds();
}
else
seconds=startTime.getSeconds()-stopTime.getSeconds();
totalSeconds=hours*60*60+minues*60+seconds;
}
else
{
int minutes=stopTime.getMinutes()-startTime.getMinutes();
if(minutes>0)
{
minutes=stopTime.getMinutes()-startTime.getMinutes();
}
else
minutes=startTime.getMinutes()-stopTime.getMinutes();
int seconds=stopTime.getSeconds()-startTime.getSeconds();
if(seconds>0)
{
seconds=stopTime.getSeconds()-startTime.getSeconds();
}
else
seconds=startTime.getSeconds()-stopTime.getSeconds();
totalSeconds=hours*60*60+minutes*60+seconds;
}
//return seconds
return totalSeconds;
}//end of the getElapsedTime method
}//end of the class Clock
------------------------------------------------------------------------
/**
* Java class to test the Clock class
* to get elapsed time by passsing two arguments at run time
* and print the elapsed time in seconds
* */
import java.sql.Time;
public class TestClock
{
public static void main(String[] args)
{
//Create an instance of Clock class object
Clock clock=new Clock();
//Get first argument
String startTime=args[0];
//split the argument and get hour,minute and second
String [] time=startTime.split(":");
//split the argument and get hour,minute and second
int hour=Integer.parseInt(time[0]);
int minutes=Integer.parseInt(time[1]);
int seconds=Integer.parseInt(time[2]);
//call start method with an instance Time class
clock.start(new Time(hour, minutes, seconds));
//Get first argument
String endTime=args[1];
String [] stopTime=endTime.split(":");
//split the argument and get hour,minute and second
hour=Integer.parseInt(stopTime[0]);
minutes=Integer.parseInt(stopTime[1]);
seconds=Integer.parseInt(stopTime[2]);
//call start method with an instance Time class
clock.stop(new Time(hour, minutes, seconds));
System.out.println("Elapsed time in seconds :"+clock.getElapsedTime());
}//end of main method.
}//end of the class TestClock
Explanation / Answer
I am assuming you defined both public classes Clock and TestClock in seperate files.
I tried to compile your code in eclipse IDE and commandline. In both cases its compiled. In commandline compiling i got a warning saying that Time(hour,min,sec) method is depricated. So if you are getting error because of that then change your Time constructor to use milliseconds. or you can use timestamp class instead of time class. Or you can get time in milliseconds using timestamp class and use the method to get time like below.
The best way is define custom Time class with Time(hour,min,sec) and use it, so that you need not change the entire code.