• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Confused about Java program

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I am brand new to this forum.  

I am taking my very first computer science class (Java).

Up until this point, the assignments were easy for me to figure out.

However, the latest assignment I have literally spent over 20 hours trying to figure out, to no avail.

I was wondering if there was any way I could get some help in seeing what I am doing wrong?

My college is only having online classes due to COVID, and the professor is never available for help.

I know that I will "get it" if it is just explained to me... I am just having trouble organizing the code, and basically lost when it comes to this assignment...
 
Marshal
Posts: 72612
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain what you can't understand. We need to know that before we can help.
 
Saloon Keeper
Posts: 8077
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you are having difficulty with code, post it here. Do not post it as links or screen shots. Cut and paste the actual code into a post and UseCodeTags (<-- read link).
 
Dwayne Jenkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much for your replies.  I am just so frustrated at this point - that I don't "get" this assignment (though, as soon as I get the hang of it, I am sure it will come easily to me).  My intent here isn't to cheat or have someone write code for me - I would just really appreciate being pointed in the right direction / tips / how far off base I am.... etc.    I really want to learn this material, and disappointed I am having so much trouble figuring this one out.

So, I will copy and paste the assignment below, and under that will copy and paste the code that I have written so far...


ASSIGNMENT:

Professor’s strict requirements for this assignment:
-can NOT use arrays
-can NOT use Joption pane
-can ONLY use the break; statement with switch statements
-Do NOT use System.exit at all
-Just finished chapter on Methods/calling methods/passing arguments to a method… so I have to demonstrate my understanding of that

Write an application named, Guessgame.java.

This application will be a word guessing game reminiscent of the traditional, H*ngman, but no drawing of a hanging stick figure will be produced.

The application will display a welcome message and then be capable of playing as many games as the user wishes in one execution run of the program.

A new random word must be chosen for each game played. The user will indicate that another game is to be played by answering ‘y’ or ‘Y’ in response to the question, “Want to play again?” asked by the program after each game.

The secret word for each game must be randomly chosen from the file, wordlist.txt.. This file contains 33,736words. You may use this numeric value (33736)as a named constant.

use this constant in generating a random integer. Then, read each word from the beginning of the file until you read the nth
word, where nis the random number generated between 1 and 33,736, inclusive.

For example, if the random number is 20,751, your code should read and ignore 20,750 words, then read the word, nonfat, as the secret word (number 20751) for the current game.
[NOTE: After we cover chapter 7, we could use arrays to choose a word, but for now the word must be chosen as indicated in this paragraph.]

Once a secret word is chosen the program displays this to the user in a disguised format. The disguised word contains a dash (‘-‘) in the corresponding position of each consonant in the word and an equal sign (‘=’) for each vowel. All the logs show this clearly. The disguised word therefore shows the user the length of the word, as well as the actual position of each consonant and vowel within the secret word.

The letter, Y, is considered a consonant for this application.
The user begins by guessing one letter at a time. If the guessed letter is in the secret word, a new disguised word is generated showing all occurrences of the guessed letter along with dashes and equal signs for letters not yet guessed. If the guessed letter is not in the word, the incorrect guess is concatenated to the string of incorrect guesses, but only if the guessed letter is not already in this string.

A correct guess and the first time an incorrect letter is guessed cause the guess count to be incremented. If a guessed letter has already been guessed either correctly or incorrectly, the guess count is NOT incremented.

You can observe this in the example output (attached) and thereby get a full understanding of how this must work. Just before the prompt for each guess, which shows the disguised word, the application displays the string of letters that have already been incorrectly guessed. Note that each incorrectly guessed letter appears in the incorrect guesses string
exactly once.  

If the user guesses a character that is not a letter, a validation loop should be entered to print an error message and a “try again” prompt for another guess until the user enters a letter as the guess. You can observe this in the example execution logs.

When the user has guessed the final letter to complete the guessing of the secret word, the game loop should be stopped and the game-ending message displayed. For example: You guessed the word: ready in 7 guesses! Your score was 92.3%!This must be displayed AFTER the end of the game loop, not inside this loop. The guesses include all correct and incorrect letters guessed. Repeated guesses (correct or incorrect) and non-letters are not counted as guesses.

Scoring: Here is how the score is calculated. The score is the sum of two
values: the letters percentage and the word length percentage. The letters percentage is determined by the expression: 100 – guess Count / 26.0 * 100where guess Count is the number of guesses made by the user during the game just played.  The word length percentage is determined by the expression:<Length of the secret word>/ 26.0 * 100This score should be printed to show exactly one decimal place. Following the game-ending display, the program should ask the user whether to play another game. When the user answers with any character other than ‘y’ or ‘Y’, the application termination message is displayed. This includes the “Thanks for playing...” message followed by the best (maximum) score of all the user’s games played during the session, followed by the user’s average score. You should display the application termination message as described here in the exact format found in the execution logs.




And finally, the code I have written so far... would REALLY love some guidance as to where to go from here....

 
Carey Brown
Saloon Keeper
Posts: 8077
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've only just begun to look at your code and right off I see a problem in that you have one excessively long main() method. This should be broken down into many methods so as to be more modular, more manageable, and clearer. Each method should be written to accomplish a single clearly defined task, and the name of the method should clearly indicate what that method does. I don't see anything in your requirements that precludes doing this.

You'll get a lot of other comments about your code about following Java conventions. These are accepted and preferred practices even though the compiler may be accepting what you have now. As an example, it is recommended that each variable be declared independently on a line of its own.
So
Is unacceptable, and should be re-written as:

 
Carey Brown
Saloon Keeper
Posts: 8077
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note: Never go back an edit a post once people have responded to it, it makes the people sound dumb. Re-post the code again after you make changes.

You have some minor compilation errors.

This could be turned into a getNewMysteryWord() method.
To handle error conditions in this method you could throw an exception if you are familiar with that, or you could return 'null' for the word and the caller would have to then check to see if a 'null' had been returned and handle the error as it sees fit.
 
Carey Brown
Saloon Keeper
Posts: 8077
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are using the variable 'stdin' all over the place but haven't defined it. Based on the way you are using it it looks like it's meant to be an instance of the Scanner class constructed from System.in.
 
Sheriff
Posts: 16140
269
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch and welcome to the world of programming!

This is going to sound harsh but what you're written there is equivalent to a haystack made not of straw but of needles. One look at it makes me afraid to touch it, as anyone should be when they see a gigantic stack of needles.

Has your instructor not taught you about methods and breaking down a problem into small parts to make it easier to solve? You can't expect to write programs that are maintainable and bug-free if you jam all your code in one place like that. That's just incomprehensible not to just anyone else reading the code but also to you, the one who wrote the code in the first place. Imagine writing something that you yourself can't even understand.
 
Junilu Lacar
Sheriff
Posts: 16140
269
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dwayne Jenkins wrote:I really want to learn this material, and disappointed I am having so much trouble figuring this one out.


The amount of time you spend solving this will depend largely on your ability to maintain conceptual integrity of what you've done so far. By "conceptual integrity" I mean your ability at any given point in time to understand the big picture of what's happening in the program as well as what each individual section does and how each part relates to all the other parts. When you have all the code in one place like that, it's virtually impossible to maintain conceptual integrity, much less manage it.

Also, your development process is very important here. A disciplined and systematic approach to solving the problem is essential. The code you've shared shows a lack of those qualities in your approach. How much have you tested the program as you've written it? When writing a non-trivial program like this, you should expect to spend more time testing the program than you do writing it out. That is, you should write a little bit of code to do one small task, then run the program to see if the program works as expected with respect to that small task.

An example of a small task would be "Display a welcome message" or "calculate a random number."

I don't expect that your instructor would have taught you about good development practices like automated unit testing so I won't even get into the details of that right now but using a program to test your program as you develop it is essential to speeding up your development process and maintaining the program's  correctness as you build it out.
 
Junilu Lacar
Sheriff
Posts: 16140
269
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dwayne Jenkins wrote:
-Just finished chapter on Methods/calling methods/passing arguments to a method… so I have to demonstrate my understanding of that


I guess this answers my first question. Putting all your code in your main() method demonstrates that you don't understand methods and passing arguments.

As mentioned previously, use methods to break your solution down into many small tasks that you can implement one at a time. This is how you can maintain conceptual integrity of what you've done so far.
 
Junilu Lacar
Sheriff
Posts: 16140
269
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a concrete example of what maintaining conceptual integrity and using methods would look like:

You wrote:

Using methods, this section of code could be written simply as:

These few lines of code are much more readable and comprehensible than the many lines of code you have right now. Additionally, it would be easier to test each bit of functionality in isolation (unit testing).
 
Junilu Lacar
Sheriff
Posts: 16140
269
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Please note: Never go back an edit a post once people have responded to it, it makes the people sound dumb. Re-post the code again after you make changes.


It's not so much about making people sound dumb as it is making the thread difficult to follow by invalidating previously posted comments.

For example, someone makes a comment about lines 141-150 in your code. Then you edit your previous post and delete lines 141-150 or change it in some way that would invalidate the comment made. Anyone reading the thread after your edit will not understand why the comment was made because your edits modified or removed the context. Including the original code as a quote can help but sometimes it's easier to just refer to the code by its line number(s) instead of quoting it.
 
Campbell Ritchie
Marshal
Posts: 72612
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dwayne Jenkins wrote:. . . random number generated between 1 and 33,736, inclusive. . . .

That is slightly unusual. Most indexing on computer sciences is done on a 0‑based basis. Anyway: have you got a way of generating that number? Well, sort of (line 48). I don't like arithmetic with Math.random(), not even if the Java™ Tutorials show you an example. Random has a method which produces such a result more efficiently, but make sure to read its documentation carefully.
 
Dwayne Jenkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for all of your helpful comments!  Tomorrow morning I will resume working on the problem.
What makes this especially difficult is, there are no in-person classes this semester.  Additionally, this particular instructor does not even give virtual lectures.  The only resource we are given is the textbook, and we are expected to learn solely from that resource.  The current chapter on methods is especially confusing, and we are supposed to incorporate methods into this assignment.  I will try again tomorrow - all of your comments will definitely help me grasp this better.
Can anyone recommend an additional good resource I can use to help me understand better?  A specific set of video tutorials that you would recommend, or anything else that would help?
Thanks so much!!
 
Junilu Lacar
Sheriff
Posts: 16140
269
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What textbook are you using for this class? Can you give a list of lessons you went through before this particular assignment? I'm curious as to what the assignments were before this that you found to be easy and whether those would have actually helped you piece together the solution for the current assignment you're having trouble with.

I'm thinking of putting together a series of short tutorial videos that might help someone like you in the future.
 
Dwayne Jenkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
The textbook is "Starting out with Java: From Control Structures Through Objects 7th Edition by Tony Gaddis.
Up until this assignment, we covered everything from Chapters 1 - 4 (omitting the JOptionPane section)
This assignment is supposed to cover Chapters 1 - 5, and just the beginning of Chapter 9 (9.1 and 9.2).

Chapters 5 and 9 just lost me... and other students are having a lot of trouble as well learning in this textbook - only type of learning structure.

The Computer Science Virtual tutors at my college are always unavailable - helping other students.

So, I decided to turn to this forum to see if I could get any guidance...
 
Carey Brown
Saloon Keeper
Posts: 8077
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you've got to start somewhere.  Why don't you try to refactor your code that fetches a random word into a method getRandomWord(). Or if you need something even simpler, a method isVowel() that takes a char and returns true if it's a vowel. Post your modified code back here.
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic