Input/Output
Scanner and Random
Goals
Random
Go to the API link for the Random class given above. Make sure to notice the details on the
simple constructor, (the one without a seed input), and the nextInt(int upper) method which returns
a psuedo-random integer within the range [0, upper).
So, to create a Random object without a seed (don't worry about what a seed is), we simply type
> Random r = new Random();
and if we want our Random object to generate a new integer from, say, 0-9, we simply say
> r.nextInt(10)
(Mini-Challenge) Can you think of a way to get the random object to only give you numbers in the
range from [1,10]? Remember, you can only specify the upper bound.
Scanner
Go to the API link for the Scanner class given above. Read through the first few paragraphs which
decsribes some different inputs Scanner works on and the different things you can do with the Scanner.
Don't worry if you do not understand all the terminology used, just try to get a general feel for how it works.
For this lab, we will focus on using System.in as our input. Also notice the different methods for getting
the next of something in your input stream. For example, nextDouble(), nextInt(), nextLine(), etc. The key
things to know are that we will create a Scanner object by typing
> Scanner sc = new Scanner(System.in);
that creates a Scanner object that will always use System.in (the opposite of System.out) for its input.
and then by typing
> sc.nextsomething
where something could be (), Int(), Line(), any of the next methods we see present in the API, we cause
the Scanner object to prompt the user (through System.in) for input of that type. You will see a test box appear
on the screen, that is where you type your input.
Overview
Our goal today is to utilize I/O to write a simple game, Guess the Number.
We want to have this game be user vs. computer, so the computer is going to be
the one picking the number and the user will try to guess it.
The entire game can be modeled in less than 30 lines.
The specifics of the game are up to each of you, but there are some rules:
- You must use a Scanner object to read input(in this case the guesses) from the user through System.in
- You must use a Random object to generate the next number for the user to guess
- You must be able to start the game by simply calling a method. For example, if your class is called NumberGame
there must be some method in it (say startGame()) that begins the game so that if you type
> NumberGame ng = new NumberGame();
> ng.startGame();
into DrJava, the game starts.
Step One: General Specs
The first thing programmers do when starting a project is to make a spec sheet. This is a sheet
designed to be a general outline of the project's goals, much like the beginning of these lab writeups.
The idea here is to figure out how to break up the problem into subproblems that are easier to handle
and test on their own.
This should be done as a lab.
Step Two: Intermediate Specs
The next thing we do is take the general spec sheet and expand each part to figure out what the
subproblems actually entail.
This should be done as a lab.
Step Three: Advanced (Finished) Specs
The next thing we do is take the intermediate spec sheet and now figure out how we want to solve each part.
Break up into your pair groups and going through the spec sheet, jot down pseudocode to address each problem.
Overview: Specs
Hopefully, this drives home how important planning is to successful programming. You might be able
to get away with little planning at the start, with smaller problems, but as the problems grow in
complexity, a good plan is essential to minimizing your errors and your time. Here we first
took a simple look at our problem and figured out where the subproblems were. Then we looked at the
details of each subproblem to determine what we needed to solve. Finally, we actually decided how we
were going to solve those subproblems. Now, the coding part is actually quite simple.
Step Four: Coding/Compiling
The amount of time this step takes in inversely proportional to the amount of effort you put into
the previous step. Simply take your advanced spec sheet and convert that into actual code.
Step Five: Debugging
Again, if you planned well, testing and debugging should be a breeze. The trick to make the subproblems
you devised in step one be roughly independant of each other and therefore simple to test on their own.
If you do that, instead of having to write multiple long scripts to test the funcionality of the entire
program, and having to trace through that if it fails, you can write short simple test scripts to test the
individual components and if those all pass, just one script to test the entire program.
Here are some sample interactions from one solution. NOTE!!! Your output need not exactly match this,
nor do your class/method names need to match. The only that does have to match is the basic functionality
of the game, that is, that you play against the computer to try and guess the random number.
> NumberGame g = new NumberGame()
> g.startGame(10) ***the 10 here refers to the upper bound on the RNG, it is up to you to decide if you want a
fixed upper bound (therefore no parameter), or a user generated one as shown here***
You're quite low. ***I typed 2 into the input box***
You're quite low. ***I typed 4 into the input box***
A little higher. ***I typed 7 into the input box***
Correct. Well done. It took you: 4 guesses. ***I typed 8 into the input box***
The output generated (such as "a little higher") is not needed to complete the game, but makes the game
more enjoyable, once you have the basics of the game coded, try adding your own little touches.