UW CSE logo uw

CSE 142: Computer Programming I, Summer 2008

arrow CSE Home arrow About Us arrow Search arrow Contact Info

Homework 5 (Guessing Game) FAQ

Q: Where do I start?

A: Read the write-up in its entirety. Try to develop the program in stages. Review example programs from section and lecture, such as Sentinel, MultTutor, and Dice.

Q: What is an InputMismatchException? Why does my program give me that error when reading from the Scanner?

A: This error occurs when you try to read the wrong kind of value from the Scanner. You are probably calling the wrong method on it. If you want a word back, use .next(). If you want an int back, use .nextInt().

Q: How can I return two values from one method?

A: You can't. Maybe you have chosen the wrong method structure; for example, maybe the method should pass those two values as parameters to another method, instead of returning them. Or maybe your method is too large and you should break it into smaller pieces.

Q: How do I compute the best game (fewest guesses)?

A: You mostly have to figure this out for yourself, but here are some hints. In each game you should keep track of how many guesses were needed. You should probably also keep track of the best game you have seen so far, and update that value accordingly after every game, if the game is better than the best one seen previously. You will have to carefully manage your returns and parameters to make sure that the right information reaches the right parts of your code, but it can be done.

Q: Is it okay to have a method that calls itself? Or to have a Method A that calls Method B, which calls A, which calls B, ...?

A: No, you should not do this on Homework 5. Having a method call itself is actually an advanced computing technique called recursion. Recursion is not an appropriate algorithmic technique for solving this problem. You can solve the problem correctly using while loops for repetition, instead of recursion.

Q: Since the program is so random, how can I possibly match the expected output? How can I use the Output Comparison Tool?

A: You don't have to match the randomly generated numbers shown in our output logs. But you should have the same format as our logs.

If you *do* want to exactly match our output so that you can see the coveted "No differences found" in the Output Comparison Tool, it can be done. To do it, you can force your Random object to return the same sequence of random numbers on every run of the program. This is called seeding the Random object. Random numbers are generated from a mathematical function, and a seed is an integer you pass to the Random object as it's being constructed. For example, to seed your Random object with the value 42, you'd write:

Random rand = new Random(42);

The following are the seeds we used to generate our logs of expected output:

  • Run #1: seed = 42
  • Run #2: seed = 69
  • Run #3: seed = 28
  • Run #4: seed = 1590
  • Run with max of 5: seed = 28
Valid XHTML 1.1 Valid CSS!