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

Written in java, please use comments to explain the code. part 1 To fully unders

ID: 3744850 • Letter: W

Question

Written in java, please use comments to explain the code.

part 1

To fully understand generating a random number you need to learn more about Java classes and methods. For now, you cna use 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 display a randomly generated number; the user can see whether his or her guess was accurate. We will build on this program in future chapters.

part 2

Using the random number program you created from Chapter 1 write a program that selects a random number between two named constant values of 1 and 5 and asks the user to input their guess. Display a message that displays the difference between the two numbers. Display another message that displays the random number and the Boolean true or false depending on whether the user's guess was equal to the random number. An example of using constants to create a random number is:

random = MIN + (int)(Math.random) * MAX;

Explanation / Answer

1) import javax.swing.*; public class GuessRandomNumber { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Think of a number between 1 and 10."); JOptionPane.showMessageDialog(null, "The number is " + (1+(int)(Math.random() * 10))); } } 2) import javax.swing.*; import java.util.Random; public class GuessRandomNumber { public static void main(String[] args) { final int MIN = 1; final int MAX = 5; int guess = Integer.parseInt(JOptionPane.showInputDialog("Enter your guess[1-5]: ")); int randomNumber = new Random().nextInt(MAX - MIN + 1) + MIN; JOptionPane.showMessageDialog(null, "Difference between your guess and Random number is " + Math.abs(guess - randomNumber)); JOptionPane.showMessageDialog(null, "Random Number: " + randomNumber + " and Guess: " + (randomNumber==guess)); } }