Why doesn\'t the following code compile for this assignment??? Clock Class: //Cl
ID: 3672856 • Letter: W
Question
Why doesn't the following code compile for this assignment???
Clock Class:
//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
import java.sql.Time;
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 minutes=stopTime.getMinutes()-startTime.getMinutes();
if(minutes>0)
{
minutes=stopTime.getMinutes()-startTime.getMinutes();
}
else
minutes=startTime.getMinutes()-stopTime.getHours();
int seconds=stopTime.getSeconds()-startTime.getSeconds();
if(minutes>0)
{
seconds=stopTime.getSeconds()-startTime.getSeconds();
}
else
seconds=startTime.getSeconds()-stopTime.getSeconds();
totalSeconds=(hours*60*60)+(minutes*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
public class HelloWorld
{
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
I think your java program compile by the above format.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.