Introduction
A stopwatch is a device used to measure the time elapsed between two events. In this tutorial, we will learn how to create a basic stopwatch program in Java. This simple program will allow users to start, stop, and reset the stopwatch with the help of basic Java concepts like classes, methods, and loops.
Objective
The objective of this program is to help beginners learn how to use Java for creating simple applications. The stopwatch will include the basic features such as starting the timer, stopping the timer, and resetting the timer to zero. By the end of this tutorial, you will have a functional stopwatch program and a better understanding of Java programming.
Java Program Code
import java.util.Scanner; public class Stopwatch { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long startTime = 0; long elapsedTime = 0; boolean running = false; System.out.println("Welcome to the Java Stopwatch!"); System.out.println("Commands: "); System.out.println("'start' - Start the stopwatch"); System.out.println("'stop' - Stop the stopwatch"); System.out.println("'reset' - Reset the stopwatch"); System.out.println("'exit' - Exit the program"); while (true) { System.out.print("\nEnter command: "); String command = scanner.nextLine().toLowerCase(); if (command.equals("start")) { if (!running) { startTime = System.currentTimeMillis() - elapsedTime; running = true; System.out.println("Stopwatch started..."); } else { System.out.println("Stopwatch is already running."); } } else if (command.equals("stop")) { if (running) { elapsedTime = System.currentTimeMillis() - startTime; running = false; System.out.println("Stopwatch stopped."); System.out.println("Elapsed time: " + (elapsedTime / 1000) + " seconds"); } else { System.out.println("Stopwatch is not running."); } } else if (command.equals("reset")) { startTime = 0; elapsedTime = 0; if (running) { startTime = System.currentTimeMillis(); } System.out.println("Stopwatch reset."); } else if (command.equals("exit")) { System.out.println("Exiting the program..."); break; } else { System.out.println("Invalid command. Please try again."); } } scanner.close(); } }
Program Structure & Explanation
The program starts by importing the Scanner class for taking user input. It defines the main method where the core logic of the stopwatch is implemented.
- Variables: The program uses three main variables:
startTime
(holds the start time),elapsedTime
(holds the time that has passed), andrunning
(indicates if the stopwatch is running). - Commands: The user can enter commands:
start
,stop
,reset
, orexit
. - Start Command: When the user enters
start
, the stopwatch starts and records the current time. If it’s already running, the program notifies the user. - Stop Command: When the user enters
stop
, the stopwatch stops and calculates the elapsed time, displaying it in seconds. - Reset Command: When
reset
is entered, the stopwatch is reset, clearing any previous time. - Exit Command: The program exits when the user enters
exit
.
How to Run the Program
To run the program:
- Ensure you have the Java Development Kit (JDK) installed on your computer.
- Save the code in a file named
Stopwatch.java
. - Open the terminal or command prompt, navigate to the directory where the file is saved, and compile the program by typing:
javac Stopwatch.java
. - Run the program by typing:
java Stopwatch
.