• Post Reply Bookmark Topic Watch Topic
  • New Topic

Struggling with homework  RSS feed

 
Sabina Strat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry but I cannot manage to add the lines...


This is the description of what I am supposed to be doing:

The shoutOutRandomMessage() method will return type String.

The shoutOutRandomMessage() will use several Arrays or an ArrayList to store words. You will have one data structure that holds a list of words that are subjects, another data structure that holds a list of words that are objects, another that holds a list of verbs, another that holds a list of adverbs, and another that holds a list of adjectives. You can initialize your data structures with words or have the user enter the words. The choice is yours.
The shoutOutRandomMessage() method will use a random number generator that selects one word from each data structure to form a random message. The shoutOutRandomMessage() method will return the random message as a String data type. Random messages will be of the form: Subject - Verb - Adjective - Object - Adverb. Document your shoutOutRandomMessage() method to explain the code.


I have 2 problems:
1) for some reason, when the verbs print, they print null null null whereas all other variables print fine
2) I cannot make the instance variables print one of each with the same index to form a sentence.

PLEASE HELP!

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't want

You want

Seems like you should also be generating a random number instead of 'i' for each of subject, object, verb, etc.
Suggest using java.util.Random instead of the older Math.random().
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you don't have an array called message, you have a String called message, so message[i] is incorrect.
You message concatenation should use '+' to put them together, not commas.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sabina Strat wrote:The shoutOutRandomMessage() method will return type String.


What do you think what is wrong with that?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Sabina Strat wrote:The shoutOutRandomMessage() method will return type String.


What do you think what is wrong with that?

Two things wrong, you declare the method as returning 'void' not 'String', and then you are not actually returning a String.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additionally, main() is not prepared to handle a returned String. Probably should put the println() call in main().
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sabina Strat wrote:
 
Sabina Strat
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, guys! I made it work and it works like a charm



 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sabina Strat wrote:I made it work and it works like a charm

Sabina Strat wrote:The shoutOutRandomMessage() method will return type String.

This requirement still not satisfied. Also there are lots of unused code in your program.

And please, UseCodeTags when you post code. You can do so by selecting your code and pressing button "Code" in edit panel.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
She did use code tags, but incorrectly. I am correcting them, and also removing the unnecessary blank lines.

Please look here to see how use code tags.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:She did use code tags, but incorrectly. I am correcting them, and also removing the unnecessary blank lines.
Thank you Campbell, now it looks the way better.

Sabina, you still can make up your program. Here is my comments, you might find some useful:
1. Lines 19, 30-34 are not used. Double check it, you might missed something within your requirements.
2. Line 36 - it is not an instance variable. What other type it could be?
3. It is good, that all code not laying down in a "main" method, in fact, it would be better, if you'd create more methods accordingly to a specific tasks. At least it would make your program more readable also better structured. All the time try to create methods for a small specific tasks, so you'd know, which part of the code not doing what they meant to do.
4. Variable names could be more descriptive. For example: "length". If you mean the length of array, it could be "arrayLength" - it is more clear than just length, as it could be a string length. Try to choose something unambiguous. There are more unclear variables as "number". Number of guesses? Number of words? Number of what?

Hope it helps at some point.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
4. Variable names could be more descriptive. For example: "length". If you mean the length of array, it could be "arrayLength" - it is more clear than just length, as it could be a string length. Try to choose something unambiguous. There are more unclear variables as "number". Number of guesses? Number of words? Number of what?

Good advice. One thing about a name like "arrayLength" though: it hints at an implementation detail (that there is an array involved) rather than the intent or purpose of the variable. If you used "wordCount" or "numberOfWords" instead, it's easier to discern what its intended use is and when and where it should be used in the program. Good names also make certain kinds of comments redundant and unnecessry and this results in code that is more self-documenting. I don't really trust any comments that aren't backed by passing automated unit tests.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!