Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Hangman Game

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my assignment: Write Hangman as a subclass of BasicGame. Each word to be guessed should have five letters. Have a 250-letter String class variable that stores 50 possible words to choose at random. Make sure to take some time thinking about how you plan to do this program BEFORE you start coding.

My question is mainly about the class variable. How would I make a class variable that can store 50 words that I can pull out randomly?
Thanks.
 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class variables are "static final" variables. Then you will also need a static block to load this variable with the words to guess.
 
Marshal
Posts: 69856
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is an interesting question. It would appearThatYouAreSupposedToWriteALongStringOf250Letters which divides into 50 five‑letter words. That seems a strange requirement. If I were doing that assignment, I would use a 50‑element String[] array.
It is also ambiguous about class variable. Does that mean a variable made from the String class, or a String which is a class variable of your Hangman class? The latter seems a reasonable suggestion, since you can use the same bank of words for every game. Class variable is another name for static field. I would mark the field final however, so it isn’t really a variable.

IfYouDoHaveSuchALongString have a look through the methods of the String class; there is one method designed for pulling out a bit of the String of a certain length. I presume you are aware of the Random class, too.
 
Michael Roslansky
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is an interesting question. It would appearThatYouAreSupposedToWriteALongStringOf250Letters which divides into 50 five‑letter words. That seems a strange requirement. If I were doing that assignment, I would use a 50‑element String[] array.
It is also ambiguous about class variable. Does that mean a variable made from the String class, or a String which is a class variable of your Hangman class? The latter seems a reasonable suggestion, since you can use the same bank of words for every game. Class variable is another name for static field. I would mark the field final however, so it isn’t really a variable.

IfYouDoHaveSuchALongString have a look through the methods of the String class; there is one method designed for pulling out a bit of the String of a certain length. I presume you are aware of the Random class, too.



We haven't done arrays yet, so I don't think I can use them. We learned about all the different types of variables, so I'm pretty sure that it's suppose to be a string that's a class variable. I just don't understand how I can fit 50 strings into one variable.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Roslansky wrote:We haven't done arrays yet, so I don't think I can use them. We learned about all the different types of variables, so I'm pretty sure that it's suppose to be a string that's a class variable. I just don't understand how I can fit 50 strings into one variable.


Simple:FYI, String[] is a class, and 'fiftyStrings' is an object (or at least a reference to one).

Winston

 
Michael Roslansky
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Michael Roslansky wrote:We haven't done arrays yet, so I don't think I can use them. We learned about all the different types of variables, so I'm pretty sure that it's suppose to be a string that's a class variable. I just don't understand how I can fit 50 strings into one variable.


Simple:
Winston


So then how would I pull a random string out of that?
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Roslansky wrote:So then how would I pull a random string out of that?


fiftyStrings[someRandomNumber]

Winston
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:FYI, String[] is a class, and 'fiftyStrings' is an object (or at least a reference to one).


But that doesn't fulfill the requirement of "Have a 250-letter String class variable that stores 50 possible words to choose at random"

Michael Roslansky wrote: I just don't understand how I can fit 50 strings into one variable.


You are not fitting 50 String objects into one variable you are storing 50 words in one variable. For example here's 5 three letter words in one variable.

You know there are fifty words so generate a random number between 0 and 49. You know each word is 5 letters long so you need to do something to the generated random number to get the start index of the word and then you need to use one of the String classes methods to extract the word starting at that index.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tony Docherty wrote:But that doesn't fulfill the requirement of "Have a 250-letter String class variable that stores 50 possible words to choose at random"


I know; which is why I didn't mention it until Michael asked.

Winston
 
Michael Roslansky
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tony Docherty wrote:
You know each word is 5 letters long so you need to do something to the generated random number to get the start index of the word and then you need to use one of the String classes methods to extract the word starting at that index.



Could you expand on this? The textbook I use has never given me an example of this. I appreciate all the help, guys!

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

K. Tsang wrote:class variables are "static final" variables.



They're static variables, regardless of whether or not they're final.
 
lowercase baba
Posts: 12871
62
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Roslansky wrote:

Tony Docherty wrote:
You know each word is 5 letters long so you need to do something to the generated random number to get the start index of the word and then you need to use one of the String classes methods to extract the word starting at that index.


Could you expand on this? The textbook I use has never given me an example of this. I appreciate all the help, guys!


Assuming you have a single string that looks like this:

cheaphelloadmitorderexams...

and so on up to the full 250 characters, you should be able to detect a pattern for the position where each new word starts. The first word starts at position 0. The second word starts at position 5, etc.

So you need a function that will randomly determine which starting position to use.

Once you have that, you need to use methods of the string class to get part of the big, long string that is just your word. you will use the starting position you determined randomly, and maybe something else.
 
Campbell Ritchie
Marshal
Posts: 69856
278
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find out about String methods here. I suggest you scroll to the top of the page and read the introductory text, because that will give you a heavy hint. For random numbers, try here. (It is not really random, but you will never notice any difference.)
 
Michael Roslansky
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what I'm getting out of this is that I should be using an int modifier to seperate between every 5 characters. Then, I use a modifer to pull 5 consecutive numbers that are a multiple of 5?
Thanks again.
 
Campbell Ritchie
Marshal
Posts: 69856
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. I think you need a number which is a multiple of 5, and another number 5 more.
 
I'm so happy! And I wish to make this tiny ad happy too:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic