(2330300)Simple Stopwatch using JAVA
(2330300)Simple Stopwatch using JAVA
Aim:
To develop a simple stopwatch application using Java that allows users to start, stop,
and reset the timer while displaying elapsed time in seconds.
Objective:
1. Implement a user-friendly stopwatch using Java's built-in
System.currentTimeMillis() function.
2. Utilize object-oriented programming concepts such as encapsulation and methods
to manage the stopwatch's functionality.
3. Provide an interactive command-line interface for users to control the stopwatch
(start, stop, reset, and exit).
4. Ensure accurate time tracking and efficient handling of elapsed time even when
paused and resumed.
5. Improve the understanding of Java concepts like loops, conditionals, and user
input handling.
Code:
import java.util.Scanner;
while (true) {
System.out.print("\nCommands: (S)tart | (T)op | (R)eset | (E)xit : ");
String command = scanner.next().toLowerCase();
switch (command) {
case "s" -> stopwatch.start();
case "t" -> stopwatch.stop();
case "r" -> stopwatch.reset();
case "e" -> {
System.out.println("Exiting...");
scanner.close();
return;
}
default -> System.out.println("Invalid command!");
}
}
}
}
Output:
Conclusion:
The Java-based stopwatch successfully fulfills its purpose by allowing users to track
elapsed time through a simple command-line interface. The implementation
effectively demonstrates the use of Java’s time functions and object-oriented
programming principles. This project serves as a foundational step for further
enhancements, such as integrating a graphical user interface (GUI) or adding features
like lap timing.