import java.util.Timer ; import java.util.TimerTask ; import java.util.Scanner;
ID: 3550020 • Letter: I
Question
import java.util.Timer;
import java.util.TimerTask;
import java.util.Scanner;
class StopWatch
{
private long startTime = 0;
private long stopTime = 0;
private boolean running = false;
public void start ()
{
this.stopTime = System.currentTimeMillis();
this.running= false;
}
public long getElaspedTimeSecs()
{
long elapsed;
if (running)
{
elapsed = ((System.currentTimeMillis() - startTime) / 1000);
}
else
{
elapsed = ((stopTime - startTime)/ 1000);
}
return elapsed;
} // Comment out below main code we dont nedd this.
/*public static void main (String[] args)
{
StopWatch s = new StopWatch();
s.start();
//s.stop();
System.out.println("time in seconds:"+s.getElaspedTimeSecs());
}
*/
}
public class Walk
{
public static void main(String[]args)
{
Scanner myScanner = new Scanner(System.in);
StopWatch s = new StopWatch();
s.start();
//s.stop();
Boolean stillRunning = true;
//while(stillRunning)
{
System.out.println("Welcome to the 10 meter walk test");
System.out.println("Would you like to do a test? Enter [Y/N]");
String userChoice = myScanner.next();
if(userChoice.equalsIgnoreCase("Y") ||
userChoice.equalsIgnoreCase("YES"))
{
//continue;
}
else
{
stillRunning= false;
}
}
System.out.println("time in seconds:"+s.getElaspedTimeSecs());
}
}
--------------------------------------------------------------------------------------------------
I want to be able to have the user press any key to begin and stop the timer. How do I do this?
I want something like:
System.out.println("Welcome to the 10 meter walk test");
System.out.println("Press any key to begin and stop the test");
String userChoice = myScanner.next();
[ insert code for any key]
}
else
{
stillRunning= false;
}
}
System.out.println("time in seconds:"+s.getElaspedTimeSecs());
}
}
Explanation / Answer
import java.util.Timer;
import java.util.TimerTask;
import java.util.Scanner;
class StopWatch
{
private long startTime = 0;
private long stopTime = 0;
private boolean running = false;
public void start ()
{
this.stopTime = System.currentTimeMillis();
this.running= false;
}
public long getElaspedTimeSecs()
{
long elapsed;
if (running)
{
elapsed = ((System.currentTimeMillis() - startTime) / 1000);
}
else
{
elapsed = ((stopTime - startTime)/ 1000);
}
return elapsed;
} // Comment out below main code we dont nedd this.
/*public static void main (String[] args)
{
StopWatch s = new StopWatch();
s.start();
//s.stop();
System.out.println("time in seconds:"+s.getElaspedTimeSecs());
}
*/
}
public class Walk
{
public static void main(String[]args)
{
Scanner myScanner = new Scanner(System.in);
StopWatch s = new StopWatch();
s.start();
//s.stop();
Boolean stillRunning = true;
//while(stillRunning)
{
System.out.println("Welcome to the 10 meter walk test");
System.out.println("Press any key to begin and stop the test");
// comment below line we dont need this
//String userChoice = myScanner.next();
// check if scanner has anything left to scan.
if(myScanner.hasNext())
{
System.out.println("You Typed " + myScanner.next());
}
else
{
stillRunning= false;
}
}
System.out.println("time in seconds:"+s.getElaspedTimeSecs());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.