Instructions
Objective
Write a program to implement word shuffling in java.
Requirements and Specifications
In the first section to complete a java assignment, you will write methods to perform basic text analysis on a document.
Count the number of words in the text. You can assume that words are separated by a space.
Count the number of sentences in the text. You can assume that a new sentence begins following a punctuation mark. Note: you only need to consider the
following punctuation: "." "?" "!"
Find the top 10 words in the text and return the number of times they occur. This information should be outputted to a text file in an easy-to-read format. Note: The top 10 words refers to the 10 most frequently used words in the text file.
A method that accepts a string as a parameter and returns True if the word is found in the text document and False otherwise.
Here is a text file with a portion of President Barack Obama's acceptance speech. You can test your programs using this text file. PoliticalSpeech.txt
Here is the code that you can use to read in a text file from your computer. You will need to add the path in the appropriate place to identify where the text file is saved on your own computer.
You may not use any of the built-in Java methods or libraries that many entirely solve any of the above problems. You can use built-in methods such as "length()" for finding the length of a string.
Part 2: WORD SHUFFLE
The Word Shuffle game is a puzzle that shuffles the letters of a word. For this assignment, your program will select a word and will create a jumble or mixed up version of the word. The player of the game must then guess what the original word was before it had been shuffled.
Write a program that selects a word and produces a “jumbled” version. For example, the letters of the word “TAKEN” might be scrambled as “AKNET”.
The shuffling of the letters should be random. This means, for example, that your program shouldn't follow a predictable shuffling algorithm, such as always moving the first letter of a word to the end of the word. Ten shuffles is more than enough to jumble a 5 or 6 letter word. Hint: To randomize the scrambling, use the Random class.
Here is a link to a dataset of English words. This is the dataset that you should use to select a word to scramble. The word should be selected at random.
Here are the specific rules for the game:
The game is played with two players competing against each other to get the highest score. During each turn, the player is presented with a scrambled word. They need to guess the original word before it was shuffled.
During each turn, the player gets a maximum number of 5 chances to guess the original word. Once the player has entered 5 guesses, the word is revealed, the player loses their turn, and game-play is transferred to the other player.
Your program should keep track of the players' scores. Here is how points are accumulated:
If the player guesses the word correctly on their first try, they get 5 points.
If the player guesses the word correctly on their second try, they get 4 points.
If the player guesses the word correctly on their third try, they get 3 points.
If the player guesses the word correctly on their fourth try, they get 2 points.
If the player guesses the word correctly on their fifth try, they get 1 point.
If the player does not successfully guess the word within five tries, they get 0 points, and game-play is passed over to the opposing player. Before the end of their turn, the correct word should be revealed to the player.
At any point, the player should be able to forfeit their turn to request to see the word. If the player chooses to have the word revealed, they will not gain any points for that round.
As soon as a player reaches a score of 40 or greater, the game is over and that player wins the game.
You may not use StringBuilder to solve this problem.
Part 3: Encryption and decryption of messages
To complete java assignment, you will code a ciphering program. A cipher is an algorithm for performing encryption or decryption of messages. We will use a cipher algorithm that I have devised called step cipher.
Task # 1: Write a method called encode that will encode a message using a specified key that is in the range 0 through 25.
The header of the method should be as follows:
public static String encode(String message, int key)
The message may include spaces and punctuation characters, but only the lower case characters ‘a’ through ‘z’ should be encoded.
Each of the letters of the alphabet will map to the letter key positions further along in the alphabet. For example, if the key value is 4, the letters of the alphabet will be mapped as follows:
A -> E
B -> F
C -> G
and so on...
Task # 2: Write a java assignment that will decode a message using a specified key that is in the range 0 through 25. The header of the method should be as follows:
public static String decode(String codedText, int key)
The coded text may include spaces and punctuation characters, but only the lower case characters ‘a’ through ‘z’ should be decoded.
You may not use StringBuilder to solve this problem.
All code needs to be sufficiently commented.
Source Code
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Random;
import java.util.Scanner;
public class WordShuffle {
private static Random random = new Random();
private static Scanner in = new Scanner(System.in);
// Load a word to be guessed by the player
private static String loadWord() throws Exception {
// Open the file and choose a random word
while (true) {
BufferedReader br = new BufferedReader(new FileReader("words.txt"));
while (true) {
String line = br.readLine();
// Stop at end of file
if (line == null) {
break;
}
if (random.nextBoolean()) {
// OKay use this word
br.close();
return line.toLowerCase();
}
}
// Re-do if no word was selected
br.close();
}
}
// Shuffle the word, scramble the letters
private static String shuffleWord(String word) {
char[] letters = word.toCharArray();
for (int i = 0; i < letters.length; i++) {
int j = random.nextInt(letters.length);
char letter = letters[i];
letters[i] = letters[j];
letters[j] = letter;
}
return new String(letters);
}
// Make a player play
private static int play() throws Exception {
String word = loadWord();
String shuffledWord = shuffleWord(word);
System.out.println("The shuffled word is: " + shuffledWord);
// 5 tries
for (int score = 5; score > 0; score--) {
System.out.print("Guess the word (-1 to reveal word): ");
String guess = in.nextLine().toLowerCase();
if (guess.equals("-1")) {
System.out.println("The word is '" + word + "'");
return 0;
}
if (guess.equalsIgnoreCase(word)) {
System.out.println("Correct, you got " + score + " points.");
return score;
}
}
System.out.println("You ran out of tried, the word is '" + word + "'");
return 0;
}
// Entry point of the program
public static void main(String[] args) throws Exception {
int player1Score = 0;
int player2Score = 0;
int currentPlayer = 1;
while (player1Score < 40 && player2Score < 40) {
// Play a new round
System.out.println("PLAYER " + currentPlayer + " YOUR TURN");
int score = play();
// A player keeps on playing until they score 0
while (score > 0) {
if (currentPlayer == 1) {
player1Score += score;
} else {
player2Score += score;
}
// Game stops when one player scores 40 or more points
if (player1Score >= 40 || player2Score >= 40) {
break;
}
System.out.println("Player 1 Score: " + player1Score);
System.out.println("Player 2 Score: " + player2Score);
// Next round
System.out.println("PLAYER " + currentPlayer + " STILL YOUR TURN");
score = play();
}
// Switch player
if (currentPlayer == 1) {
currentPlayer = 2;
} else {
currentPlayer = 1;
}
}
// Play the game
System.out.println("GAME OVER");
System.out.println("Player 1 Score: " + player1Score);
System.out.println("Player 2 Score: " + player2Score);
if (player1Score > player2Score) {
System.out.println("Player 1 wins.");
} else {
System.out.println("Player 2 wins.");
}
}
}
Similar Samples
At ProgrammingHomeworkHelp.com, delve into our comprehensive array of programming homework samples. These exemplify our prowess in diverse languages and problem-solving techniques, showcasing meticulous coding and clear solutions. Our samples serve as a testament to our commitment to excellence in assisting students with their programming challenges effectively.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java