Question
Your program should read in a random word from a file called words.txt (note the lack of capitalization) that you provide. The file should contain:
Your program should read in a random word from a file called words.txt (note the lack of capitalization) that you provide. The file should contain:
One word per line
At least 10 different words
Each word should have at least 5 letters
Words should have variable lengths
To read a random word you can generate a random number between 1 and the number of lines, and skip n-1 lines before reading the word. Scramble the word by swapping random pairs of letters a random number of times (mandatory use of a loop). Display the scrambled word with character indices on top. Offer the user a menu with the following choices:
Enter 1 to swap a pair of letters
If this option is selected, prompt the user to enter two indices and swap the letters. A space should separate the two indices. Print an error message if the indices are invalid. If the word is unscrambled successfully, print a congratulatory message containing the word and the number of steps it took to unscramble. Otherwise, display the new word and offer the menu again.
Enter 2 to solve
If this option is selected, print the unscrambled word and quit.
Enter 3 to quit
If this option is selected, quit. Example run:
--------- 0123456 rpocjet --------- Enter 1 to swap letters. Enter 2 to solve. Enter 3 to quit. 1 Enter the indices separated by spaces 0 1 --------- 0123456 procjet --------- Enter 1 to swap letters. Enter 2 to solve. Enter 3 to quit. 1 Enter the indices separated by spaces 3 4 --------- 0123456 projcet --------- Enter 1 to swap letters. Enter 2 to solve. Enter 3 to quit. 1 Enter the indices separated by spaces 4 5 Congratulations! You unscrambled the word project in 3 steps.
Requirements
Read in random the word from input file named words.txt.
Scramble the word.
Handle output as listed in the project instructions.
Handle user input as listed in the project instructions.
Swap two letters as requested by the user.
Check if the word was unscrambled correctly and proceed accordingly.
Print solution and quit if requested by the user.
Quit program if requested by the user.
Handle errors appropriately. You have to handle correctly at least errors related to menu selections out of bounds, indices out of bounds, and indices out of order (not an error, but should produce expected result).
Formatting Requirements
Follow the indentation rules as discussed in Java Foundations.
Use descriptive variable names.
Comment your code: your name, name of the class and assignment at the beginning of program, description of program functionality at beginning of program, explanations of blocks of code throughout your program.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started