Basic Requirements Write a program that tests the user’s ability to memorize a s
ID: 3627401 • Letter: B
Question
Basic Requirements
Write a program that tests the user’s ability to memorize a sequence of colors. As shown in the sample session, the program starts off by displaying a dialog box with a list of colors that are to be memorized – red, white, yellow, green, and blue. The user then enters the colors one at a time in a text box. If the user makes a mistake, the program prints a “Sorry” message. If the user correctly enters all the colors, the program prints a “Congratulations” message. Note that when the sorry or congratulations message is printed, the window’s original components get cleared away.
As always, you are required to write elegant code. In particular, you should avoid hard coding the color values in the interior of your program. You should declare those values one time in an array at the top of the program.
Note:
· Your program should contain a class named YourNameProg7.
· Use a simple FlowLayout layout manager scheme.
· Use an inner class for the listener.
As always:
· Limit your use of class variables and instance variables – use them only if appropriate.
· Use appropriate modifiers for your methods. The modifiers we’ve discussed are private, public, static, and final.
· Use helping methods if appropriate.
· Mimic the sample session precisely. In particular, note the dialog box’s text, the window’s title, and the window’s text.
First sample session:
The opening dialog box:
How good is your memory?
Try to memorize this color sequence:
red white yellow green blue
After closing the dialog box, here’s the main window:
Enter color number 1:
After typing the first color:
Enter color number 1: red
After pressing enter and then typing the second color:
Enter color number 2: white
After entering all five colors correctly:
Congratulations - your memory is perfect!
Second sample session:
After closing the dialog box and typing the first color:
Enter color number 1: red
After pressing enter and then typing the second color:
Enter color number 2: black
After pressing enter:
Sorry - wrong color. Eat more antioxidants.
You can get full credit for completing this section only.
72
Extra Credit
Provide a hint button that causes the current color’s first letter to appear in the text box. For example, since the first color is red, the first hint should be r. The hint button must cause focus to be put on the text box (i.e., the cursor should appear within the text box without the user having to click there with the mouse). To cause focus to be put on a component, use the requestFocusInWindow method. See Sun’s API documentation for requestFocusInWindow details.
Sample session:
After closing the dialog box, here’s the main window:
Enter color number 1:
After clicking the hint button:
Enter color number 1: r
After typing the first color:
Enter color number 1: red
After pressing enter and clicking the hint button:
Enter color number 2: w
After entering all five colors correctly:
Congratulations - your memory is perfect!
Explanation / Answer
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Prog7 extends JFrame { String currentcol; JPanel jpncontrols; // panel for holding controls String colors[] = {"red", "white", "yellow", "green", "blue"}; //colors array //control definition JLabel jlcol1; JLabel jlcol2; JLabel jlcol3; JLabel jlcol4; JLabel jlcol5; JTextField jtxcolor1; JTextField jtxcolor2; JTextField jtxcolor3; JTextField jtxcolor4; JTextField jtxcolor5; FlowLayout flt; //using flow layout class eventhandle implements ActionListener { //this is the inner class to handle events private boolean CheckOthers() { if (!(jtxcolor1.getText().trim().equalsIgnoreCase(colors[0]))) { //if the content of jtxcolor1 not equals to red return false; // return false } if (!jtxcolor2.getText().trim().equalsIgnoreCase(colors[1])) { //if the content of jtxcolor2 not equals to white return false; // return false } if (!jtxcolor3.getText().trim().equalsIgnoreCase(colors[2])) { //if the content of jtxcolor3 not equals to yellow return false; // return false } if (!jtxcolor4.getText().trim().equalsIgnoreCase(colors[3])) { //if the content of jtxcolor4 not equals to green return false; // return false } return true; //if all of abone are ok return true } public void actionPerformed(ActionEvent e) { String errMessage = "Sorry -wrong color. Eat more antioxidants"; String congratsMessage= "Congratulations- your memory is perfect! "; String title ="Memory Game "; if (e.getSource() == jtxcolor1) { // if the action happend on (pressing Enter Button ) on jtxcolor1 textbox if (jtxcolor1.getText().trim().equalsIgnoreCase(colors[0])) { //is the content of jtxcolor1 equals to red jtxcolor2.requestFocus(); // if true set the focus to jtxcolor2 } else { JOptionPane.showMessageDialog(null,errMessage,title,0 ); //if not display error message System.exit(-1); //exit with an error(Says to the System Programm exited with an error } } else if (e.getSource() == jtxcolor2) { // if the action happend on (pressing Enter Button ) on jtxcolor2 textbox if (jtxcolor2.getText().trim().equalsIgnoreCase(colors[1])) { //is the content of jtxcolor2 equals to white jtxcolor3.requestFocus();//set the focus to jtxcolor3 } else { JOptionPane.showMessageDialog(null,errMessage,title,0 ); //display error message System.exit(-1); //exit with an error(Says to the System Programm exited with an error) } } else if (e.getSource() == jtxcolor3) {// if the action happend on (pressing Enter Button ) on jtxcolor3 textbox if (jtxcolor3.getText().trim().equalsIgnoreCase(colors[2])) { //is the content of jtxcolor3 equals to yellow jtxcolor4.requestFocus(); } else { JOptionPane.showMessageDialog(null,errMessage,title,0 ); System.exit(-1); //exit with an error(Says to the System Programm exited with an error) } } else if (e.getSource() == jtxcolor4) {// if the action happend on (pressing Enter Button ) on jtxcolor4 textbox if (jtxcolor4.getText().trim().equalsIgnoreCase(colors[3])) { //is the content of jtxcolor4 equals to green jtxcolor5.requestFocus(); } else { JOptionPane.showMessageDialog(null,errMessage,title,0 ); System.exit(-1); //exit with an error(Says to the System Programm exited with an error) } } else if (e.getSource() == jtxcolor5) {// if the action happend on (pressing Enter Button ) on jtxcolor5 textbox if (jtxcolor5.getText().trim().equalsIgnoreCase(colors[4]) && CheckOthers()) { //is the content of jtxcolor5 equals to blue and other txtboxes also Ok JOptionPane.showMessageDialog(null,congratsMessage,title,1); //displays the greeting message System.exit(0); // exits normally } else { JOptionPane.showMessageDialog(null,errMessage,title,0 ); System.exit(-1); //exit with an error(Says to the System Programm exited with an error) } } } } public Prog7() { super("Memory Game "); String Firstmessage = " How good is your memory ? Try To Memorize the Sequence : " + colors[0] + " " + colors[1] + " " + colors[2] + " " + colors[3] + " " + colors[4]; JOptionPane.showMessageDialog(null, Firstmessage,"Memory Game",1 ); //shows the initial message //shows the initial message flt = new FlowLayout(0, 2, 3); //create a new FlowLayout //initialize the controls jpncontrols = new JPanel(flt); //initialize jpncontrols with the Flow Layout jlcol1 = new JLabel("Enter Color1"); jlcol2 = new JLabel("Enter Color2"); jlcol3 = new JLabel("Enter Color3"); jlcol4 = new JLabel("Enter Color4"); jlcol5 = new JLabel("Enter Color5"); jtxcolor1 = new JTextField(" "); jtxcolor2 = new JTextField(" "); jtxcolor3 = new JTextField(" "); jtxcolor4 = new JTextField(" "); jtxcolor5 = new JTextField(" "); //add controls to jpncontrols Panel jpncontrols.add(jlcol1); jpncontrols.add(jtxcolor1); jpncontrols.add(jlcol2); jpncontrols.add(jtxcolor2); jpncontrols.add(jlcol3); jpncontrols.add(jtxcolor3); jpncontrols.add(jlcol4); jpncontrols.add(jtxcolor4); jpncontrols.add(jlcol5); jpncontrols.add(jtxcolor5); // add action listner to each text box jtxcolor1.addActionListener(new eventhandle()); jtxcolor2.addActionListener(new eventhandle()); jtxcolor3.addActionListener(new eventhandle()); jtxcolor4.addActionListener(new eventhandle()); jtxcolor5.addActionListener(new eventhandle()); //Add jpncontrols to Main Pane getContentPane().add(jpncontrols); setDefaultCloseOperation(EXIT_ON_CLOSE);//Activate the Close button setBounds(10, 20, 300, 200); //Sets the Window area setVisible(true);//Shows the Window } public static void main(String[] args) { Prog7 prg = new Prog7(); //create A New Prog7 Window } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.