Java
Java

 

 

 

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), and running (indicates if the stopwatch is running).
  • Commands: The user can enter commands: start, stop, reset, or exit.
  • 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:

  1. Ensure you have the Java Development Kit (JDK) installed on your computer.
  2. Save the code in a file named Stopwatch.java.
  3. Open the terminal or command prompt, navigate to the directory where the file is saved, and compile the program by typing: javac Stopwatch.java.
  4. Run the program by typing: java Stopwatch.

 

© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)