Results 1 to 20 of 20
- 07-20-2013, 06:19 PM #1
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
calling variables from a method....
Still struggling with this numbers guessing game assignment. I got the first class written, and it compiles, but I am having trouble writing the tester. I have gone completely brain dead on calling variables in the methods.
Here is the method:
PHP Code:public void newGame(int maxGuesses) { maxGuesses = maxGuessesAllowed; Random generator = new Random(); int answer = (int) (Math.random() * 100 + 1); gameOver = false; differential = max; numGuessesTaken = 0; }
PHP Code:import java.util.Scanner; import java.util.Random; public class GuessingGameTester2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); Random generator = new Random(); GuessingGame startGame = new GuessingGame(); startGame.newGame(7); int answer = (int) (Math.random() * 100 + 1); System.out.println("The answer is: " + answer); String choice = ""; System.out.println("Guess a number between 1 and 100. You are only allowed" + startGame.maxGuesses + "guesses."); System.out.println("Are you ready to play? Enter Y for yes, N for no."); choice = keyboard.nextString(); } }
GuessingGameTester2.java:17: error: cannot find symbol
System.out.println("Guess a number between 1 and 100. You are only allowed" + startGame.maxGuesses + "guesses.");
^
symbol: variable maxGuesses
location: variable startGame of type GuessingGame
- 07-20-2013, 06:25 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: calling variables from a method....
In your newGame() method, none of your variables (other than answer) seem to be declared. If they are instance variables they need to be defined somewhere. Regarding, answer, it is a local variable but does not appear to be used anywhere.
Also, you are passing maxGuesses as an argument but then immediately assigning something to it. Definitely not what you want to do.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-20-2013, 06:44 PM #3
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
Re: calling variables from a method....
Here are my instance variables:
private int answer;
private Random generator;
private boolean gameOver;
private int differential;
private int max;
private int maxGuessesAllowed;
private int numGuessesTaken;
private String response;
And the constructor I called in the tester:
PHP Code:public GuessingGame() { max = 0; Random generator = new Random(); int answer = (int) (Math.random() * 100 + 1); }
- 07-20-2013, 06:58 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: calling variables from a method....
maxGuesses is a local variable (actually a parameter to a method). You may not access it using a class reference. And once again, why are you clobbering your parameter in your newGame() method? Perhaps you meant this:
Java Code:maxGuessesAllowed = maxGuesses. //Then in your print statement: startGame.maxGuessesAllowed
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-20-2013, 11:49 PM #5
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
Re: calling variables from a method....
Sorry, I had to step out for some family stuff.
I changed the parameter around as you suggested, as well as the print statement, and I still get the error cannot find symbol.
- 07-21-2013, 12:38 AM #6
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
Re: calling variables from a method....
OK, basics 101 I am sure, but I am at a loss....how do you call a mutator in a method when it is not the method parameter? (I do not even know if I asked that right)....in the above, I should be calling the int answer from the newGame method, instead of declaring it in the tester class...but everything I try results in "cannot find symbol" so evidently I simply do not know how to do it, or my original program is completely wrong even though it compiles.
This calls the newGame method, and sets the parameter for 7 max guesses
startGame.newGame(7);
that I get...(at least I think I get) but calling the other variables in the newGame method has me stumped.....
PHP Code:public void newGame(int maxGuesses) { maxGuessesAllowed = maxGuesses; Random generator = new Random(); int answer = (int) (Math.random() * 100 + 1); gameOver = false; differential = max; numGuessesTaken = 0; }
- 07-21-2013, 02:05 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: calling variables from a method....
Your instance variables need to be declared in the GuessingGame class (just below the class declaration). Otherwise, they won't be accessible via a class reference.
Regarding the mutator. You would need a method like setAnswer();
So you prompt for the answer and they say.
startGame.setAnswer(answer);
Assuming that is where you want to pass the value. I am still unclear as to what your goals are.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-21-2013, 03:07 AM #8
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
Re: calling variables from a method....
Yes, that worked. I had not set those up....the directions on this assignment are rather vague.
Now I am having other issues. I have the guess method in the GuessingGame class:
public String guess(int newGuess)
newGuess is the user input in the tester class.
I tried this in the tester:
System.out.println("Enter a guess (1-100): ");
startGame.guess(newGuess = input.nextInt());
but it cant find symbol. So I guess I need to figure out how to assign newGuess in the guess method to user input in the tester. I am not sure how to do that. There is no instance variable for the user guess, so once again I feel at a loss.
- 07-21-2013, 03:26 AM #9
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: calling variables from a method....
It can't find the symbol because "guess" is not an instance variable in the class GuessingGame.
Edit: I mis-read the post (guess is a method and is defined).
Regards,
JimLast edited by jim829; 07-21-2013 at 04:32 AM. Reason: I'm a dummy
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-21-2013, 03:44 AM #10
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
Re: calling variables from a method....
so how do I call newGuess in the guess method?
- 07-21-2013, 04:31 AM #11
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: calling variables from a method....
Sorry, my mistake. I misread the post. When you say it can't find the symbol, please be specific. Can you show the error message. And I would recommend that you validate the input before sending it to the guess method (unless the guess method validates it for you).
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-21-2013, 05:06 AM #12
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
Re: calling variables from a method....
OK I think I have it working except for one particular.(BTW you have been a great help, I appreciate it)
here are my newGame and guess methods
PHP Code:public void newGame(int maxGuesses) { maxGuessesAllowed = maxGuesses; Random generator = new Random(); answer = (int) (Math.random() * 100 + 1); gameOver = false; differential = max; numGuessesTaken = 0; } /** Sets new guess @param newGuess sets new guess to user input. */ public String guess(int newGuess) { differential = Math.abs(newGuess-getAnswer()); int newDifferential = Math.abs(getAnswer() - getDifferential()); if (getNumGuessesTaken() <= getMaxGuessesAllowed()&& getNumGuessesTaken() != getMaxGuessesAllowed()) { if(getAnswer() == newGuess || getNumGuessesTaken() > getMaxGuessesAllowed()) { response = "Correct. Would you like to play again, enter Y for yes, N for no."; gameOver = true; } else if(getAnswer() > newGuess && numGuessesTaken == 0) { response = "Too low"; numGuessesTaken++; } else if(getAnswer() < newGuess && numGuessesTaken == 0) { response = "Too high"; numGuessesTaken++; } else if(getAnswer() > newGuess && newDifferential > getDifferential()) { response = "To low and getting colder."; numGuessesTaken++; } else if(getAnswer() > newGuess && newDifferential < getDifferential()) { response = "To low, but getting warmer."; numGuessesTaken++; } else if(getAnswer() < newGuess && newDifferential > getDifferential()) { response = "To high and getting colder."; numGuessesTaken++; } else if(getAnswer() < newGuess && newDifferential < getDifferential()) { response = "To high, but getting warmer."; numGuessesTaken++; } else if(newGuess > 100 || newGuess < 1) { response ="Guess out of range, the guess must be between 1 and 100."; numGuessesTaken++; } } else { response = "Game over. Would you like to play again, enter Y for yes, N for no."; gameOver=true; } return response; }
PHP Code:if (!choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("y")) { System.out.println("I do not understand, enter Y for yes, N for no:"); choice = keyboard.next(); } else if(choice.equalsIgnoreCase("n")) { System.out.println("Goodbye, thanks for playing."); } else if(choice.equalsIgnoreCase("y")) { System.out.println("Good, I am glad you want to play."); } while(choice.equalsIgnoreCase("y")) { if (startGame.isGameOver() == false) { int i = startGame.getNumGuessesTaken(); for (i = 0; i < startGame.getMaxGuessesAllowed(); i++) { System.out.println("Enter a guess (1-100): "); startGame.guess(keyboard.nextInt()); System.out.println(startGame.getResponse()); } } else { System.out.println("Would you like to play again? Enter Y for yes, N for no:"); choice = keyboard.next(); if (!choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("y")) { System.out.println("I do not understand, enter Y for yes, N for no:"); choice = keyboard.next(); } else if(choice.equalsIgnoreCase("n")) { System.out.println("Goodbye, thanks for playing."); } } }
The problem I am having is when I run the tester, and I get to the end of my guesses I get this:
Game over. Would you like to play again, enter Y for yes, N for no.
Enter a guess (1-100):
instead of the input of y or n to start a new game.
- 07-21-2013, 05:43 AM #13
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: calling variables from a method....
It's hard to tell what is happening.
Are you certain the isGameOver code is returning the same boolean you set in the game?
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-21-2013, 11:19 PM #14
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
Re: calling variables from a method....
at this point I am not certain of anything.....this program is so messy, and I am so frustrated with it. I wish I could just email you the code so you could see its entirety instead of this piece mill stuff....I am at a point of giving up.....this class has made sure I do not want to take anymore programming courses...it does not feel like an introductory course at all.
- 07-22-2013, 12:58 AM #15
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: calling variables from a method....
First, don't get frustrated. Just hang in there. Programming can be very rewarding (and actually quite fun).
There are some ways you can make it simpler. First, you can validate your answers of yes or no as you use them and also package them in a method to reuse.
Java Code:// disclaimer. Untested code if (playAgain()) { // play again } else { //quit the game } public boolean playAgain() { Scanner input = new Scanner(System.in) ; while (true) { System.out.print("Do you want to play another game? "); char choice = input.next().toLowerCase().charAt(0); if (choice == 'y') { return true; } else if (choice == 'n') { return false; } System.out.println("Unknown response."); } }
Note: Think how you might customize the playAgain method to a more general one which prompts for a y or n answer to any questions and returns true or false depending the answer.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-22-2013, 01:44 AM #16
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
Re: calling variables from a method....
I think half my issue is that I am not allowed to have any user input in my class. All user input is suppose to be in the tester only.
Here is my tester...I tied to work in some of what you suggested, but it did not change my results...
PHP Code:public class GuessingGameTester2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); GuessingGame startGame = new GuessingGame(100); startGame.newGame(7); System.out.println("The answer is: " + startGame.getAnswer());//only for testing purposes String choice;//to hold the users yes or no answer System.out.println("Guess a number between 1 and 100. You are only allowed " + startGame.getMaxGuessesAllowed() + " guesses."); System.out.println("Are you ready to play? Enter Y for yes, N for no:"); choice = keyboard.next(); boolean gameOver = false; while (!gameOver) { if (!choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("y")) { System.out.println("I do not understand, enter Y for yes, N for no:"); choice = keyboard.next(); } else if(choice.equalsIgnoreCase("n")) { gameOver = false; System.out.println("Goodbye, thanks for playing."); } else if(choice.equalsIgnoreCase("y")) { gameOver = true; System.out.println("Good, I am glad you want to play."); } while(choice.equalsIgnoreCase("y")) { if (startGame.isGameOver() == false) { System.out.println("Enter a guess (1-100): "); startGame.guess(keyboard.nextInt()); System.out.println(startGame.getResponse()); } else { if(startGame.getNumGuessesTaken()> startGame.getMaxGuessesAllowed()) { System.out.println("You ran out of guesses, would you like to play again? Enter Y for yes, N for no:"); choice = keyboard.next(); } if (!choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("y")) { System.out.println("I do not understand, enter Y for yes, N for no:"); choice = keyboard.next(); } else if(choice.equalsIgnoreCase("n")) { System.out.println("Goodbye, thanks for playing."); gameOver = true; } } } } }
The answer is: 55
Guess a number between 1 and 100. You are only allowed 7 guesses.
Are you ready to play? Enter Y for yes, N for no:
y
Good, I am glad you want to play.
Enter a guess (1-100):
1
Too low
Enter a guess (1-100):
2
To low and getting colder.
Enter a guess (1-100):
4
To low and getting colder.
Enter a guess (1-100):
46
To low and getting colder.
Enter a guess (1-100):
7
To low, but getting warmer.
Enter a guess (1-100):
8
To low and getting colder.
Enter a guess (1-100):
9
You are out of guesses. Would you like to play again, enter Y for yes, N for no:
Enter a guess (1-100):
y
Exception in thread "main" java.util.InputMismatchException
or this:
The answer is: 35
Guess a number between 1 and 100. You are only allowed 7 guesses.
Are you ready to play? Enter Y for yes, N for no:
y
Good, I am glad you want to play.
Enter a guess (1-100):
35
Correct. Would you like to play again, enter Y for yes, N for no.
y
y
- 07-22-2013, 04:04 AM #17
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
Re: calling variables from a method....
So it was suggested I use a do{} while{} statement in my tester......uh.....I have no clue how to get all of that if {} else if{} stuff into a do{}while{}....especially in 3 hours....here is what I have come up with so far
Java Code:do { System.out.println("Enter a guess (1-100): "); startGame.guess(keyboard.nextInt()); [COLOR="#FF0000"]system.out.println(startGame.getResponse());[/COLOR] if(startGame.getNumGuessesTaken()> startGame.getMaxGuessesAllowed()) { System.out.println("You ran out of guesses, would you like to play again? Enter Y for yes, N for no:"); choice = keyboard.next(); if (!choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("y")) { System.out.println("I do not understand, enter Y for yes, N for no:"); choice = keyboard.next(); } else if(choice.equalsIgnoreCase("n")) { System.out.println("Goodbye, thanks for playing."); gameOver = true; } } } while(choice.equalsIgnoreCase("y")); } }
GuessingGameTester3.java:31: error: package system does not exist
system.out.println(startGame.getResponse());
1 error
- 07-22-2013, 04:37 AM #18
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
Re: calling variables from a method....
Remember that Java is a case sensitive language. The package name should be in the correct form, what you need is System.out.println() where the "S" is a capital letter.
Website: Learn Java by Examples
- 07-22-2013, 04:51 AM #19
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: calling variables from a method....
As I said before, you need to break it down into simpler steps. Here is a skeleton of how the interaction would be. You need to provide the ask and play methods. Here are a couple notes:
1. The ask method only returns when the input is valid. Which means it either returns a true for continuing or false for not continuing.
2. The first game is played in response to the first question. When it returns, the next loop continues to prompt for another game and if true, the game is played. This will happen repeatedly.
3. In all cases, if the first question or while loop conditional is false, then the ending message is printed.
4. To automate responses, just have then canned in the ask routine.
This can actually be made shorter with a single while loop and dynamic questions. But for now you need to apply the rest of the code. When writing out code it is important to go thru it on paper and see how the conditionals cause the flow of the program.
Note: This code has not been tested.
Java Code:if (ask("Do you want to play a game? ")) { // then play the game // then proceed to ask for another while (ask("Do you want to play another game?")) { // then play game // then ask for another } } System.out.println("Ok! Have a nice day!");
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-22-2013, 05:03 AM #20
Senior Member
- Join Date
- Jun 2013
- Posts
- 136
- Rep Power
- 0
Re: calling variables from a method....
SMH...I have been staring at this stuff to long! I should have seen the capitalization....
Here is what I have so far:
Java Code:do { GuessingGame startGame = new GuessingGame(100); startGame.newGame(7); System.out.println("The answer is: " + startGame.getAnswer()); System.out.println("Guess a number between 1 and 100. You are only allowed " + startGame.getMaxGuessesAllowed() + " guesses."); System.out.println("Are you ready to play? Enter Y for yes, N for no:"); choice = keyboard.next(); if (!choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("y")) { System.out.println("I do not understand, enter Y for yes, N for no:"); choice = keyboard.next(); } else if(choice.equalsIgnoreCase("n")) { System.out.println("Goodbye, thanks for playing."); gameOver = true; startGame.isGameOver(); } else if(choice.equalsIgnoreCase("y")) { System.out.println("Good, I am glad you want to play."); int i = startGame.getNumGuessesTaken(); for (i = 0; i < startGame.getMaxGuessesAllowed(); i++) { System.out.println("Enter a guess (1-100): "); startGame.guess(keyboard.nextInt()); System.out.println(startGame.getResponse()); } System.out.println("Would you like to play again, enter Y for yes, N for no:"); choice = keyboard.next(); } } while(choice.equalsIgnoreCase("y")); } }
The only issue I seem to be having is when I guess the number correctly, and my differential comparison still is not right...the getting warmer/colder bit....
Result:
The answer is: 28
Guess a number between 1 and 100. You are only allowed 7 guesses.
Are you ready to play? Enter Y for yes, N for no:
y
Good, I am glad you want to play.
Enter a guess (1-100):
28
Correct. Would you like to play again, enter Y for yes, N for no.
Enter a guess (1-100):
Similar Threads
-
Calling variables from another method
By jwl in forum New To JavaReplies: 2Last Post: 10-17-2012, 09:37 PM -
Calling variables from another method
By jwl in forum Advanced JavaReplies: 1Last Post: 10-16-2012, 05:45 AM -
calling variables in a loop?
By SnakeDoc in forum New To JavaReplies: 3Last Post: 06-14-2012, 11:54 PM -
Calling Variables in Multiple Methods
By PrimalScientist in forum New To JavaReplies: 10Last Post: 02-07-2012, 11:26 AM -
Calling Variables
By Soulpole in forum New To JavaReplies: 7Last Post: 01-28-2012, 09:26 PM
Bookmarks