I have to decide on a problem to solove and write a program. to solve. I am to i
ID: 3767392 • Letter: I
Question
I have to decide on a problem to solove and write a program. to solve. I am to include a written description of what the program in paragraph form of what it does and instuction about how it works. The program must meet the following criteria.
Comments: Enough so another programmer will know what the program is doing without even reading your description Also, include your name, date, and program name at the top of the program
Selection structure: Include either If/else logic or a case structure
Repetition structure: Include either While, Do While or For loop
Array: Any type of an array
Inheritance: Use Inheritance and/or Interfaces
Exception: Use try…catch block Components Button, TextField, Label, or ComboBox Theme
There must be some point to the program, not just a collection of components
Receives user input
Performs output
To receive full credit you will design a JFrame Application that contains at least 4 of the 6, (selection, loop, array, inheritance, components or exception) plus all of the others mentioned above:-
Please keep the code as simple as possible this is a beginners class. This does not need to be elaborate ( I would prefer it not be) as long as it includes what is listed. I don't care what subject is used. Please do not just copy the code already written by others. Thanks!
Please try to explain the code
Explanation / Answer
PROGRAM DESCRITION: The computer generates a number and the user is to guess the number.
Program code:
//Header files
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
//GuessANumber class that implements ActionsListener for button and
//KeyListener for JTextField
public class GuessANumber extends JFrame implements ActionListener, KeyListener
{
// declare and initialize the required variables
JButton startBtn;
JTextField jtf1, jtf2;
JLabel lab1, lab2, lab3, lab0;
JPanel jpan, jpan1;
int guessNumber;
int MAX = 1000, MIN = 1;
int tries = 0;
String guessString = "";
// constructor
public GuessANumber()
{
// JPanel object
jpan = new JPanel(new GridLayout(4, 2, 10, 15));
// Start button object
startBtn = new JButton("Start Game");
// JTextField object
jtf1 = new JTextField();
jtf1.setEnabled(false);
// Create the required labels objects
lab0 = new JLabel("Click Start Button to start the game: ");
lab1 = new JLabel("I have a number between 1 and 1000 can you guess my number?");
lab2 = new JLabel("Please enter a number for your first guess and hit the Enter button");
// add the components to the jpan object
jpan.add(lab0);
jpan.add(startBtn);
jpan.add(lab1);
lab3 = new JLabel();
jpan.add(new JLabel());
jpan.add(lab2);
jpan.add(jtf1);
jpan.add(lab3);
// add the jpan to the container
add(jpan);
// add the ActionListener and KeyListener interfaces to
// JButton and JTextField
startBtn.addActionListener(this);
jtf1.addKeyListener(this);
}
@Override
// Performs the action of button click
public void actionPerformed(ActionEvent ae)
{
if (startBtn == ae.getSource())
{
// generates the random number and sets the
// other components values
Random rand = new Random();
guessNumber = (int) (rand.nextInt(MAX + MIN));
System.out.println(guessNumber);
jtf1.setText("");
jtf1.setEnabled(true);
startBtn.setVisible(false);
lab3.setText("");
}
}
@Override
// When enter Key is pressed in the JTextField/ it verifies the number is guessed number or not
// displays the message and changes the back ground color
public void keyPressed(KeyEvent ke)
{
int key = ke.getKeyCode();
if (jtf1 == ke.getSource())
{
if (key == KeyEvent.VK_ENTER)
{
guessString = guessANumber();
lab3.setText(guessString + " ");
lab3.setFont(new Font("Times New Roman", Font.BOLD, 30));
jtf1.setText("");
}
}
}
// guessANumber method will check for the guessed number
// against the random number and returns a string value
public String guessANumber()
{
// declare the required variables
int guess;
String str = "";
guess = Integer.parseInt(jtf1.getText());
// condition to check the value
if (guess < guessNumber)
{
str = guess + " is TOO LOW!";
jpan.setBackground(Color.red);
}
else if (guess > guessNumber)
{
str = guess + " is To HIGH!";
jpan.setBackground(Color.blue);
}
else
{
str = "Congrats! You have won the game! ";
startBtn.setVisible(true);
jpan.setBackground(Color.green);
}
tries++;
return str;
}
// main method
public static void main(String args[])
{
GuessANumber frame = new GuessANumber();
frame.setVisible(true);
frame.setSize(800, 300);
frame.setTitle("Guess a Number Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}