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

2A. In 1952, A. S. Douglas wrote his University of Cambridge Ph.D. dissertation

ID: 669766 • Letter: 2

Question

2A. In 1952, A. S. Douglas wrote his University of Cambridge Ph.D. dissertation on human-computer interaction and created the first graphical computer game — a version of Tic-Tac-Toe. The game was programmed on an EDSAC vacuum-tube mainframe computer. The first computer game is generally assumed to be “ Space-war! ” , developed in 1962 at MIT; the first commercially available video game was “ Pong, ” introduced by Atari in 1973. In 1980, Atari’s “ Asteroids ” and “ Lunar Lander ” became the first video games to be registered in the U. S. Copyright Office.Throughout the 1980s, players spent hours with games that now seem very simple and unglamorous; do you recall playing “ Adventure, ” “ Oregon Trail, ” “ Where in the World Is Carmen Sandiego?, ” or “ Myst ” ?

Today, commercial computer games are much more complex; they require many programmers, graphic artists, and testers to develop them, and large management and marketing staffs are needed to promote them. A game might cost many millions of dollars to develop and market, but a successful game might earn hundreds of millions of dollars. For games to hold your interest, they almost always include some random, unpredictable behavior. For example, a game in which you shoot asteroids loses some of its fun if the asteroids follow the same, predictable path each time you play the game. Therefore, generating random values is a key component in creating most interesting computer games. Copy the following statement to generate and use a dialog box that displays a random number between 1 and 10:

JOptionPane.showMessageDialog(null,"The number is " +(1 + (int)(Math.random() * 10)));

Write a Java application that displays two dialog boxes in sequence. The first asks you to think of a number between 1 and 10. The second displays a randomly generated number; the user can see whether his or her guess was accurate. Save the file as RandomGuess.java .

2B. Now that you can make decisions, modify the application so it allows a player to enter a guess before the random number is displayed and then displays a message indicating whether the player’s guess was correct, too high, or too low. Save the file as RandomGuess2.java .

2C. Now, add a loop that continuously prompts the user for the number, indicating whether the guess is high or low, until the user enters the correct value. After the user correctly guesses the number, display a count of the number of attempts it took. Savethe file as RandomGuess3.java .

Explanation / Answer

RandomGuess.java

import java.util.Random;

import java.util.Scanner;

public class RandomGuess

{

public static void main(String[] args)

{

Random rand=new Random();

int numberToGuess=rand.nextInt(10);

int number of Tries=0;

Scanner input=new Scanner(System.in);

int guess;

System.out.println("Guess a number between 1 to 10:");

}

}

2B)RandomGuess2.java

import java.util.Random;

import java.util.Scanner;

public class RandomGuess2

{

public static void main(String[] args)

{

Random rand=new Random();

int numberToGuess=rand.nextInt(10);

int numberofTries=0;

Scanner input=new Scanner(System.in);

int guess;

boolean win=false;

while(win==false)

{

System.out.println("Guess a number between 1 to 10:");

guess=input.nextInt();

numberofTries++;

if(guess==numberToGuess)

{

  

}

else if(guess<numberToGuess)

{

System.out.println("your guess is too low");

}

else if(guess>numberToGuess)

{

System.out.println("your guess is too high");

}

}

}

}

2C)RandomGuess3.java:

import java.util.Random;

import java.util.Scanner;

public class RandomGuess3

{

public static void main(String[] args)

{

Random rand=new Random();

int numberToGuess=rand.nextInt(10);

int number of Tries=0;

Scanner input=new Scanner(System.in);

int guess;

boolean win=false;

while(win==false)

{

System.out.println("Guess a number between 1 to 10:");

guess=input.nextInt();

numberofTries++;

if(guess==numberToGuess)

{

win=true;

}

else if(guess<numberToGuess)

{

System.out.println("your guess is too low");

}

else if(guess>numberToGuess)

{

System.out.println("your guess is too high");

}

}

}

System.out.println("you won!");

System.out.println("The number was:" +numberToGuess);

System.out.println("The number of attempts are:" +numberofTries+"tries" );

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote