Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this program we are going to create a game where the program will print a random String of lower-case letters, which the user will

In this program we are going to create a game where the program will print a random String of lower-case letters, which the user will then have to type as fast as possible.

The LocalTime class

You will use this class to time the speed with which your player can type. To use this class, remember to add the following code to the very top of your program, even before the class header: import java.time.LocalTime;

This will make this class available to your program. The way the LocalTime class works is that it stores a date and time in nanoseconds. Anywhere in your program, you can get the current time as your program executes by calling the LocalTime.now() method. This method will return a LocalTime object that stores the exact time when that method call was executed. Therefore, to measure the execution time for a piece of code, you will need to make two calls to this method. One before the code you are timing, and one after. Once you have these LocalTime objects, you may get longdata type representations by calling the getNano method. This method returns the time stored by the given LocalTimeobject in nanoseconds. Then, to measure a time difference, simply subtract one long value from the other.

For example, the program below clocks the execution time of a for loop that executes 100 times by getting the time before the loop starts (startTime), and the time after the loop ends (endTime), converts them into millisecond longvalues and then subtracts the two to get the execution time. Try running the program below a few times, changing the number of times the for loop executes and see how it affects the execution time.

 import java.time.Duration; import java.time.Instant; public class TimeTestDriver { public static void main(String[] args) { Instant start = Instant.now(); for (int i = 0; i < 100; i++) System.out.println(i); Instant end = Instant.now(); Duration duration = Duration.between(start, end); long nanosecondsElapsed = duration.toNanos(); double executionTime = nanosecondsElapsed/1000000000.0; System.out.println("The for-loop took " + executionTime + " seconds to execute."); } }

The NumberFormat class

You will use this class to display the real-number typing speeds to only 1 decimal point. To use this class, remember to add the following code to the very top of your program, even before the class header: import java.text.NumberFormat; This will make this class available to your program. The setMaximumFractionDigits method from this class allows us to display a real number to a specified number of decimal points. The OutputDoublesDriver class below gives an example of how one might use this class to print out the Math.PI constant using differing precisions. Try running the program below, changing the precision (max fraction digits) and see what happens.

import java.text.NumberFormat; public class OutputDoublesDriver { public static void main(String[] args) { NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); System.out.println(Math.PI); System.out.println(nf.format(Math.PI)); } }

REQUIREMENTS

Define a typing game program named TypingTestGame that does the following:

  • Welcome Message - Welcomes the user with a message that includes your name.
  • Menu - Your game should provide a menu to the user that allows him/her to either play another round or exit the program. Should a user enter an incorrect selection, he/she should be informed of the mistake and the menu should be reprinted.
  • Random Text - When a user selects the option to play another game, your program should generate aString that is randomly 10 - 30 characters in length, and where each character is a randomly selected lower-case character. This text is to be displayed for the user such that he/she may type it in directly below it such that the characters line-up as one types.
  • User Typing - Once the text to type is displayed, the user should try to type that precise text as quickly as possible. Again, make sure your game works such that the user will be typing directly below the displayed text.
  • Timing - Your program should time how quickly the user types the text, and then convert this into characters per second units. This speed should be displayed for the user to see as a real number with precision to 1 decimal point, for example, "145.6" characters per minute.
  • Fastest Speed - Like a high score, your program should maintain the fastest speed that has been achieved during the session. The fastest speed should be re-printed following each turn. Should someone play again and beat the fastest speed, inform the user with some sort of announcement.
  • Incorrectly Typed String - Should the user incorrectly type the text (even just 1 character off), he/she should receive a speed of 0. Only perfectly matching text will be counted.

EXAMPLE SESSION

*** WELCOME TO RICHARD McKENNA'S TYPING TEST GAME *** *** GAME MENU *** (p) play another round (x) exit the game Selection: p Text to Match: oyzekidautz oyzekidautz Speed: 185.4 characters per minute NEW FASTED TYPING SPEED! Top Speed: 185.4 characters per minute *** GAME MENU *** (p) play another round (x) exit the game Selection: A Invalid Menu Selection, Try Again! *** GAME MENU *** (p) play another round (x) exit the game Selection: p Text to Match: jomtqaaqnvnbyjzolihfifohfnpykr jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj INCORRECTLY TYPED, YOU GET A SPEED OF 0! *** GAME MENU *** (p) play another round (x) exit the game Selection: p Text to Match: bunustvzrqkf bunustvzrqkf Speed: 168.2 characters per minute Top Speed: 185.4 characters per minute *** GAME MENU *** (p) play another round (x) exit the game Selection: x GOODBYE!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

Students also viewed these Databases questions

Question

What is transmission efficiency?

Answered: 1 week ago