The code given below demonstrates the stop watch. Code is executed for the certain duration which is given as input argument in seconds and stops execution when the mentioned time limit is reached and the time over message is displayed to user.
import javax.swing.*;
class StopWatch {
public static void main(String args[]) {
try {
String scount = JOptionPane.showInputDialog(null,"Enter duration in seconds");
int count=Integer.parseInt(scount);
for (int n=1;n<=count;n++) {
System.out.println(n);
Thread.sleep(1000);
}
JOptionPane.showMessageDialog(null,"Time Over");
}
catch (Exception e) {
}
}
}