Docsity

2 Questions for Midterm - Computer Programming I | CSC 120, Exams of Computer Science

Toggle sidebar
bg1
1. Write a program that prints out the integer 1 to 1000, ten on a line.
Here are the first three lines as they should be printed:
12345678910
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
and so on, until 1000.
2. Write a program that has the user try to guess a secret number. The
program responds to the user’s guesses with “You Win”, “Try Again”,
“Hotter” or “Colder”.
To get input, just assume that there is a provided method getInteger()
that returns the value of the integer typed by the user.
The secret number is in the program as a static final int HIDDEN_NUMBER.
The rules for the program’s responses are:
(a) On the first guess, if the user is right, print “You Win” and exit the
program; if the user is wrong, print “Try Again” and the program
continues.
(b) On subsequent guesses, if the user is right, print “You Win” and
exit the program: if the user is wrong, print “Hotter” if the current
guess is closer to the hidden number than the previous guess,
“Colder” if the guess if farther, and “Try ”Again” if it is the same
distance.
Here is an example, where the hidden number is 13:
Guess the hidden number.
> 19
Try Again
> 2
Colder
> 10
Warmer
> 15
Colder
> 13
You Win

Partial preview of the text

Download 2 Questions for Midterm - Computer Programming I | CSC 120 and more Exams Computer Science in PDF only on Docsity!

  1. Write a program that prints out the integer 1 to 1000, ten on a line.

Here are the first three lines as they should be printed:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

and so on, until 1000.

  1. Write a program that has the user try to guess a secret number. The program responds to the user’s guesses with “You Win”, “Try Again”, “Hotter” or “Colder”. To get input, just assume that there is a provided method getInteger() that returns the value of the integer typed by the user. The secret number is in the program as a static final int HIDDEN_NUMBER. The rules for the program’s responses are:

(a) On the first guess, if the user is right, print “You Win” and exit the program; if the user is wrong, print “Try Again” and the program continues. (b) On subsequent guesses, if the user is right, print “You Win” and exit the program: if the user is wrong, print “Hotter” if the current guess is closer to the hidden number than the previous guess, “Colder” if the guess if farther, and “Try ”Again” if it is the same distance.

Here is an example, where the hidden number is 13:

Guess the hidden number.

19 Try Again 2 Colder 10 Warmer 15 Colder 13 You Win