# Typing Tutor

View Source View Source

We are going to make a Typing Tutor.

  1. Make a TypingTutor class with a setup() method and a Runner class with a main method. Create a JFrame, JPanel, and currentLetter as member variables Make sure the JFrame is visible and set the default close operation.

JFrame frame = new JFrame(); JPanel panel = new JPanel(); char currentLetter;

void setup(){ frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } 2. Paste the following method after the main method. char generateRandomLetter() { Random r = new Random(); return (char) (r.nextInt(26) + 'a'); } 3. Initialize currentLetter in the setup method as shown below:

currentLetter = generateRandomLetter();

4.Create a JLabel and set the text of the label to the currentLetter in the setup method. 5. Format the letter so that it is nice and big. Here are some hints….


label.setFont(label.getFont().deriveFont(28.0f));
label.setHorizontalAlignment(JLabel.CENTER);
  1. Add a key listener to the frame in setup method. Add label to the panel and panel to the frame. Then pack the frame.

  2. Create object/instance of typingTutor class in main method. Using the object/instance call the setup method as shown below. public static void main(String[] args){ typingTutor tutor = new typingTutor(); tutor.setup(); }

  3. Make the letter change every time a key is pressed. To do that, in the keyReleased method: i) Reset currentLetter with generateRandomLetter(). ii) Set the text of your JLabel to the new currentLetter.

  4. In the keyPressed method, print out the character that the user typed.

  5. If they typed the currentLetter, print “correct”.

  6. If they typed the correct letter, set the panel background to green. Otherwise set it to red.

panel.setBackground(Color.GREEN);

  1. [optional] After a certain number of characters, show the users’ typing speed.
Date timeAtStart = new Date();
     
private void showTypingSpeed(int numberOfCorrectCharactersTyped) {
    Date timeAtEnd = new Date();
    long gameDuration = timeAtEnd.getTime() - timeAtStart.getTime();
    long gameInSeconds = (gameDuration / 1000) % 60;
    double charactersPerSecond = ((double) numberOfCorrectCharactersTyped / (double) gameInSeconds);
    int charactersPerMinute = (int) (charactersPerSecond * 60);
    JOptionPane.showMessageDialog(null, "Your typing speed is " + charactersPerMinute + " characters per minute.");
}