Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS1310: Practical class, week 6
Usual preamble
Download and unzip the project for this week’s lab class from WebCT. It will create a new
folder called guess. The folder contains three classes for you to complete. As usual there is a
test program called tester.bat which will test your code as you go.
Instructions
The application for this week is very similar to the technical support example discussed in the
lectures. We are going to turn it into a “guess the number” game. The Responder class will
be able to choose a number between 1 and 10. It will have a respondToGuess method which
returns a string telling us if the guess is correct or not.
The InputReader class will use a Scanner to read from the keyboard. The user will need to
enter a number for each guess, and needs to answer yes or no when asked if they want to play
again.
The GameLoop class contains a method called start which controls the game.
The InputReader class (2 marks)
The first class to complete is the InputReader class. You need to write the implementation of
the method called getNumber which returns an integer. The class contains the signature but no
implementation. Here is what the method should do.
• Print out a prompt telling the user to enter a number and press return.
• Use the field called scanner to get an integer from the user. Look in the Java API
documentation to find the method in the java.util.Scanner class that returns the next
integer in the input. You need to store the integer somewhere until you are ready to
return it.
• Use the field called scanner to read the newline that the user typed. Use the method
called nextLine to do that.
• Finally return the integer that you got from the user.
The Responder class (4 marks)
There are five places where you need to write code in the Responder class. Look for the text
CODE N: in the file where N is a number from 1 to 5 corresponding to the numbered steps below.
The Responder needs to use the java.util.Random class to generate random numbers. You will
need to look at the documentation and/or the lecture notes to find out how to use it.
1. The first thing to do is to import the Random class so that you can use it. That statement
goes at the very top of the file, before the class definition starts.
2. You need to declare a field of type Random called rnd.
3. In the constructor, you need to initialise rnd to a new Random object.
1
4. The selectNumber method should set the field called number to a random number between
1 and 10 inclusive. Use the field called rnd to return its next random integer. Check the
documentation to see how to do that. Check carefully what the method you decide to
use returns - you may need to adust the number to get it in the correct range.
5. Finally, the method called respondToGuess should return a String giving information
about the guess. If the guess is lower than the current value of number, return the string
“higher”. If the guess is greater than the current value of number, return the string
“lower”. Otherwise the guess is correct to return the string “done”.
The GameLoop class (4 marks)
The only code you need to write in this class is in the start method. Look for the text CODE N
again where N corresponds to the numbered steps below. There is no test for this section. If
you finish it correctly, your game will play correctly.
1. Use the field called input to get an integer from the user.
2. Use the field called responder to pass the user’s guess to the responder and get a response.
Save the response in a local variable.
3. This step needs you to test whether the response you got back from the responder equals
"done"
4. Test whether the response held in the local variable called answer is equal to "yes"
5. If the test in step 4 is true, use the field called responder to make the responder select
another number for the next game
6. If the test in step 4 is false, the user wants to stop playing. Set the boolean variable
playing to a value that will cause the loop to be exited.
7. If the test in step 3 was false, print out whatever the responder replied in step 2. It will
be either “higher” or “lower”.
2