Please help. I need to complete the following code: public class StopWatch { int
ID: 3632074 • Letter: P
Question
Please help. I need to complete the following code:public class StopWatch {
int timeLeft;
// sets default value for timeLeft of 60
public StopWatch () {
}
// sets default value to absolute value of argument
public StopWatch(int value) {
}
// returns value of timeLeft
public int getTime () {
}
// sets timeLeft to absolute value of newTime
public void setTime (int newTime) {
}
// subtracts 1 from timeLeft if timeLeft is positive;
// if timeLeft is 0, does nothing
public void tick () {
}
}
Thank you!
Explanation / Answer
public class StopWatch
{
int timeLeft;
// sets default value for timeLeft of 60
public StopWatch ()
{
timeLeft=60;
}
// sets default value to absolute value of argument
public StopWatch(int value)
{
timeLeft=value;
}
// returns value of timeLeft
public int getTime()
{
return timeLeft;
}
// sets timeLeft to absolute value of newTime
public void setTime (int newTime)
{
timeLeft=newTime;
}
// subtracts 1 from timeLeft if timeLeft is positive;
// if timeLeft is 0, does nothing
public void tick ()
{
if(timeLeft>0)
timeLeft=timeLeft-1;
}
public String toString()
{
return "Time:"+timeLeft;
}
}
public class TestStopWatch
{
public static void main(String[] args)
{
StopWatch st=new StopWatch();
for(int i=0;i<10;i++)
{ st.tick();
System.out.println(st);
}
}//end main
}//end test class