Write a program which randomly chooses an integer from 1


Question 1:

Write a program (save in a file called GuessGame.java) which randomly chooses an integer from 1 to 100. The program should then tell the user. "I am thinking of a number from 1 to 100 ... guess what it is?". The user should then enter an integer as a guess. If the guess is above the randomly-chosen number, then the program should tell the user: "Lower!" and then wait for another guess. If the guess was below the randomly chosen number, then the program should tell the user: "Higher!" and then wait for another guess. It should repeat this until the user enters the correct number. Then it should print out "Congratulations. You guessed the number in X tries!", where X is the number of guesses that the user made. The program should then quit. You may assume that the user always enters an integer. Here is an example output:

I am thinking of a number from 1 to 100 ... guess what it is?
34
Higher!
78
Higher!
86
Lower!
82
Congratulations. You guessed the number in 4 tries!

Question 2:

Write a program (saved in a file called SmartGame.java) which displays the following 5x5 table of letters (note the board should start with the initialized state shown here). The program should then ask the user to complete the puzzle such that each row and each column consists of the letters 'S', 'M', 'A', 'R', and 'T' in some order. No letter should be repeated in any row or any column. The program should prompt the user for a row, a column and a letter. it should then insert (or replace) the letter at the given row and column with the entered letter and display the table again.

You may assume that the user always enters a value of the correct type (i.e., an integer for row and column and a string containing one char for the letter). However, if an invalid letter is entered (e.g., 'Q') the program should display "Invalid letter. Use 'S', 'M', 'A', 'R', or 'T'!". If an invalid row number is entered (e.g. -1 or 6), the program should display "Invalid row number!". A similar response should be included for invalid column numbers.

Given successful information, a placed letter can be replaced at any time. The program should repeatedly display the updated table and ask for a new letter to be placed. The program should end when each row and each column contains a unique valid letter. It should then display. "Congratulation! You must be SMART." and then quit. You MUST store all letters in a 2-dimensional array as follows:

char[][] table = new char[5][5];

Also, you must create a separate procedure for displaying the table, which should be called from your main program. The table should be displayed as shown below:

	     1   2   3   4   5
	   +---+---+---+---+---+
	 1 | S | M | A | R | T |
	   +---+---+---+---+---+
	 2 |   | T | S | M |   |
	   +---+---+---+---+---+
	 3 |   |   | R |   | S |
	   +---+---+---+---+---+
	 4 |   | S | M |   |   |
	   +---+---+---+---+---+
	 5 |   |   | T | S |   |
	   +---+---+---+---+---+

To determine if the game has ended, you can simply add up the numeric values for each letter (i.e., use (int)table[r][c] to get the numeric value for the letter at row r column c) for each row and column. The total for each row and each column must be 391 exactly for the table to be completed properly.

Sample output for the game:

    1   2   3   4   5
  +---+---+---+---+---+
1 | S | M | A | R | T |
  +---+---+---+---+---+
2 |   | T | S | M |   |
  +---+---+---+---+---+
3 |   |   | R |   | S |
  +---+---+---+---+---+
4 |   | S | M |   |   |
  +---+---+---+---+---+
5 |   |   | T | S |   |
  +---+---+---+---+---+

Enter a row (1-5): 3
Enter a column (1-5): 2
Enter a letter (S,M,A,R or T): G
Invalid letter. Use 'S', M', 'A', 'R' or 'T'.

    1   2   3   4   5
  +---+---+---+---+---+
1 | S | M | A | R | T |
  +---+---+---+---+---+
2 |   | T | S | M |   |
  +---+---+---+---+---+
3 |   |   | R |   | S |
  +---+---+---+---+---+
4 |   | S | M |   |   |
  +---+---+---+---+---+
5 |   |   | T | S |   |
  +---+---+---+---+---+
Enter a row (1-5): 3
Enter a column (1-5): 1
Enter a letter (S,M,A,R or T): T

    1   2   3   4   5
  +---+---+---+---+---+
1 | S | M | A | R | T |
  +---+---+---+---+---+
2 |   | T | S | M |   |
  +---+---+---+---+---+
3 | T |   | R |   | S |
  +---+---+---+---+---+
4 |   | S | M |   |   |
  +---+---+---+---+---+
5 |   |   | T | S |   |
  +---+---+---+---+---+
Enter a row (1-5): 4
Enter a column (1-5): 4
Enter a letter (S,M,A,R or T): T

    1   2   3   4   5
  +---+---+---+---+---+
1 | S | M | A | R | T |
  +---+---+---+---+---+
2 |   | T | S | M |   |
  +---+---+---+---+---+
3 | T |   | R |   | S |
  +---+---+---+---+---+
4 |   | S | M | T |   |
  +---+---+---+---+---+
5 |   |   | T | S |   |
  +---+---+---+---+---+ 
Enter a row (1-5):

Documentation and Testing

Ensure that your name and student number are in comments at the top of all files. Document the purpose of each method including its expected inputs (parameters) and output (return). Include evidence of testing your code in a separate testing.txt file. Comment your testing as to what you are testing and why, giving expected output as well as observed output and explanations for any differences.

Ensure that your code is well-formatted and easily readable; a happy TA is a generous TA.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Write a program which randomly chooses an integer from 1
Reference No:- TGS01244122

Now Priced at $50 (50% Discount)

Recommended (90%)

Rated (4.3/5)