Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Run this code for the game Hurkel to see how it executes. It works well in the c

ID: 3853889 • Letter: R

Question

Run this code for the game Hurkel to see how it executes. It works well in the console but your job is to convert it for running in SWING with more graphical feedback for the user.

import java.util.Random;
import java.util.Scanner;
public class Hurkel
{
public static void main(String[] args) {
// TODO Auto-generated method stub

// gets user input for the keyboard
Scanner input = new Scanner(System.in);

//Gets a random number between 0 and 1
Random ran = new Random();

//variable to hold the users' guess
int usersAnswer;
final int LIMIT = 10;
//variable to hold the system random number

int rGuess;
rGuess = ran.nextInt(LIMIT);

// Here we are creating an array of one character
strings into a String variable named 'display'
String[] display ={"0","1","2","3","4","5",
"6","7","8","9","10"};

System.out.println("You get 3 tries to win!");
// Tell the user how many tries they will get
for(int tries = 0; tries < 3; tries++) {

//ask the user for a guess
System.out.println("What is your number guess?" +
' ');

//using the scanner class, get the user guess
usersAnswer = input.nextInt();

if(usersAnswer == rGuess){
System.out.print("Bingo, you win!" + ' ');
tries = 9999;       // 9999 is used because it
is so far out of bounds it has to get out
of the game
} else if(usersAnswer < rGuess){
display[usersAnswer] = ">";
// replace the user guessed number with feedback:
greater than, this is saving the feedback
} else {      // so it doesn't get lost for the user
display[usersAnswer ] = "<";
// replace the user guessed number with feedback:
less than
}

// Display the number line with feedback
System.out.print("Feedback : ");
for(int i=0;i<display.length;i++){
System.out.print( display[i] + " " );
}

} // end of for loop for the game
System.out.println(' ');
System.out.println( "Game Over, 3 tries you lose!
The number was " + rGuess); // 3 tries you lose
}
}

Submission Details:

Embed the two programs in a Microsoft Word document with a description of your programming strategy.

Explanation / Answer

MODIFIED JAVA CODE WITH SWING GUI

import java.util.Random;

import java.util.Scanner;

import javax.swing.JOptionPane;

public class Hurkel

{

public static void main(String[] args) {

// TODO Auto-generated method stub

//Gets a random number between 0 and 1

Random ran = new Random();

//variable to hold the users' guess

int usersAnswer;

final int LIMIT = 10;

//variable to hold the system random number

int rGuess;

rGuess = ran.nextInt(LIMIT);

// Here we are creating an array of one character

//strings into a String variable named 'display'

String[] display ={"0","1","2","3","4","5",

"6","7","8","9","10"};

System.out.println("!");

// Tell the user how many tries they will get

for(int tries = 0; tries < 3; tries++) {

//ask the user for a guess

String num = JOptionPane.showInputDialog(null, "You get "+ (3-tries) +" trie(s) to win "+

"What is you number guess?", "Enter Number", JOptionPane.QUESTION_MESSAGE);

if(num == null)

{

System.exit(0);

}

usersAnswer = Integer.parseInt(num);

if(usersAnswer == rGuess){

JOptionPane.showMessageDialog(null, "Bingo you Win!", "Information", JOptionPane.INFORMATION_MESSAGE);

tries = 9999; // 9999 is used because it

//is so far out of bounds it has to get out

//of the game

System.exit(0);

} else if(usersAnswer < rGuess){

display[usersAnswer] = ">";

// replace the user guessed number with feedback:

//greater than, this is saving the feedback

} else { // so it doesn't get lost for the user

display[usersAnswer ] = "<";

// replace the user guessed number with feedback:

//less than

}

// Display the number line with feedback

String displayNum = "";

for(int i=0;i<display.length;i++){

displayNum += ( display[i] + " " );

}

JOptionPane.showMessageDialog(null, "Feedback: "+displayNum, "Feedback", JOptionPane.INFORMATION_MESSAGE);

} // end of for loop for the game

JOptionPane.showMessageDialog(null, "Game Over!, 3 tries you lose! The number was "+rGuess,"Game Over", JOptionPane.INFORMATION_MESSAGE);

}

}

PROGRAMMING STRATEGY

1) A JOption Input Dialog has been used instead of scanner class to get the input number from user.

2) Next that number is parsed to int to compare to the random number generated.

3) If input number was less then the random number generated then a ">" was put into the array and same was repeated for each greater number, now at the same time a String value displayNum has been declared which will concatenate/store the feedback String e.g., 1 2 > > 3 4 5 6 7 8 9 10. And finally this string will displayed as feedback message on the wrong input.

4) Same is the case when number input by user is greater then the guessed number.

5) And if input matches to the random number generated then program pops up a message stating so and exits by the statement System.exit(0);