Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Learning Aids/Tools 2 years ago
Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I'm learning Java and plan to take the OCA Java SE8 exam pretty soon. I made a fairly primitive Hangman game to aid with the learning. I would love some feedback on it if possible. Anything that's not done to required standards (naming conventions, how quick some of the methods are etc), I would love to know.
The two things I'm not happy with is that:
package hangman2; import java.util.Arrays; import java.util.Scanner; public class Hangman2 { static int guessesRemaining; static String stringWord; static char[] charWord; public static void playHangman() { System.out.print("Welcome to Hangman! "); System.out.println("Player 1: Enter a word"); Scanner scan = new Scanner(System.in); stringWord = scan.nextLine(); //Loop to get user to enter in letters only while (stringWord.matches(".*[^a-z].*")) { System.out.println("Please enter letters only, try again:"); stringWord = scan.nextLine(); } System.out.println("The length of the word is " + stringWord.length()); //Print out length of word. stringWord = stringWord.toLowerCase(); int lettersRemaining = stringWord.length(); //Letters to be gussed is equal to the word length. //Create a new char array equal to the word length, add * to each index. charWord = new char[stringWord.length()]; for (int i = 0; i < stringWord.length(); i++) { charWord[i] = ('\u002A'); } int livesRemaining = 7; // 7 lives remaining! MainLoop: /* Main loop, gets user input using Scanner, ensures it is only a letter (not a number or special character) Converts string to char, uses the guess method which compares the secret word to the character entered and returns true if it found it, false if it didn't. If found, letters remaining - 1, if the char array contains any asterisks, envoke the gameWon() method. If not found, lives remaining - 1, if no more lives, envoke the gameLost() method. */ while (livesRemaining > 0) { System.out.println("Player 2: Guess a Letter:!"); char guessLetter; String b = scan.next(); while (b.matches(".*[^a-z].*")) { System.out.println("Please enter letters only, try again:"); b = scan.next(); } guessLetter = b.toLowerCase REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
static int guessesRemaining; static String stringWord; static char[] charWord;
You are making the entire class into a sort of fake object.
private String stringWord; private char[] charWord;
That's how real object fields should look.
Unless you have some specific reason (e.g. you want a field to be visible outside for some specific reason), you should declare object fields as private visibility. Avoid the default (package private) unless you have some reason to use it. And not a "I might want to ... in the future" type reason. A reason that is actually implemented in your code. In this case, your entire program is one class. Any visibility will work, but private is preferred in most cases.
private
Again, I'm not saying that there is never a reason to use visibilities other than private for object fields (the data stored in an object). I'm saying that you don't have such a reason here. I'm also saying that it is probably easier to always make fields private and fix the rare exceptions later than to try looking for reasons to make it anything else in the beginning. If that policy starts to chafe, rethink it. But in the early going, it will rarely chafe beyond setters and getters.
The reason why you don't want to use a class as a fake object is that it precludes you from making multiple instances of the class. You can't play two Hangman games at once this way.
You'll need a constructor.
Hangman2(String word) { stringWord = word; charWord = new char[word.length()]; Arrays.fill(charWord, '*'); }
This also eliminates the need to manually fill the character array with asterisks. Instead we use the built-in Arrays.fill.
Arrays.fill
And while some methods do not need to be static, main does. So rewrite it something like
static
main
public static void main(String[] args) { do { Hangman2 game = new Hangman2(inputWordToGuess()); game.play(); } while (isReplayDesired()); }
Note that both getWordToGuess and isReplayDesired would be static methods. Meanwhile, playisn't.
getWordToGuess
isReplayDesired
play
When it does make sense to use static is when you are using the same thing everywhere. For example, it would make sense to have a static Scanner.
Scanner
public static Scanner input = new Scanner(System.in);
This can be public as well, so other classes don't have to make their own scanners.
public
In your original, you would build a scanner object in any method that happened to need to fetch user input. This way, you can build one that lasts for the entire program.
Your gameWon and gameLost methods are almost identical. So I replaced both with
gameWon
gameLost
public static boolean isReplayDesired() { while (true) { System.out.println(" Would you like to play again? Y / N"); switch (input.next().toUpperCase().charAt(0)) { case 'Y': return true; case 'N': return false; default: System.out.println("Please enter Y / N"); } } }
This gets rid of your enter Boolean. So less code.
enter
You can use the intermediate yesno variable, but it's not necessary.
yesno
By returning a Boolean, we can simplify the code both here and in the caller.
It would be possible to overrun the stack with recursive calls with your original code (albeit somewhat difficult). Now it isn't. No recursive call.
I never put a break in the default clause
break
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.