This program allows you to test your typing speed by comparing the time it takes to type a given passage accurately. The program calculates the number of words per minute (WPM) based on the time and accuracy.
Objective:
The goal of this Typing Speed Test Application is to evaluate a user’s typing speed in terms of words per minute (WPM). The program provides a random text to type, records the time taken by the user, and then calculates the typing speed by comparing the typed text to the original text.
Java Code for Typing Speed Test:
public class TypingSpeedTest { import java.util.Scanner; public class TypingSpeedTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Define the passage to type String passage = "The quick brown fox jumps over the lazy dog."; System.out.println("Typing Speed Test"); System.out.println("Type the following passage:"); System.out.println(passage); // Start the timer long startTime = System.currentTimeMillis(); // Take user input for the passage System.out.println("\nStart Typing: "); String userInput = scanner.nextLine(); // End the timer long endTime = System.currentTimeMillis(); // Calculate time taken to type long timeTaken = endTime - startTime; // in milliseconds // Calculate the words per minute (WPM) int wordCount = userInput.split("\\s+").length; double timeTakenInMinutes = timeTaken / 60000.0; // convert time to minutes double wpm = wordCount / timeTakenInMinutes; // Calculate accuracy int correctChars = 0; for (int i = 0; i < Math.min(passage.length(), userInput.length()); i++) { if (passage.charAt(i) == userInput.charAt(i)) { correctChars++; } } double accuracy = (double) correctChars / passage.length() * 100; // Display results System.out.println("\nTime taken: " + timeTaken / 1000 + " seconds"); System.out.println("Words per minute: " + wpm); System.out.println("Accuracy: " + accuracy + "%"); scanner.close(); } }
Explanation of the Program:
This program measures typing speed by following these steps:
- Passage Selection: The program provides a predefined text (“The quick brown fox jumps over the lazy dog”) that the user must type.
- Timing: The time is recorded using
System.currentTimeMillis()
both at the beginning and the end of the typing session to calculate the typing duration. - Words Per Minute (WPM): WPM is calculated by dividing the number of words typed by the time taken in minutes.
- Accuracy Calculation: The program compares each character typed by the user with the corresponding character in the original passage to determine accuracy.
- Result Display: The program displays the time taken, words per minute, and accuracy as output.
How to Run the Program:
- Ensure you have the Java Development Kit (JDK) installed on your machine.
- Copy the code into a file named TypingSpeedTest.java.
- Open a terminal (or command prompt) and navigate to the directory where the file is saved.
- Compile the code using the following command:
javac TypingSpeedTest.java
- Run the program using the following command:
java TypingSpeedTest
- Follow the on-screen instructions to start typing the provided passage and get your typing results.