Directions Points The files must be called <YourNameProg7.java>. Example: JimRob
ID: 3627465 • Letter: D
Question
DirectionsPoints
The files must be called <YourNameProg7.java>.
Example: JimRobbinsProg7.java
Ensure you include ALL files required to make your program compile and run.
I would like to see your .java files only.
Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter.
4
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.
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.
Total Points
80
Explanation / Answer
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Prog7 {
String colors[] = {"red", "white", "yellow", "green", "blue"}; //colors array
public Prog7() {
String Firstmessage = " How good is your memory ? Try To Memorize the Sequence : " + colors[0] + " " + colors[1] + " " + colors[2] + " " + colors[3] + " " + colors[4];
String title="Memory Game";
JOptionPane.showMessageDialog(null, Firstmessage ); //shows the initial message
String errorMessage = "Sorry -wrong color . Eat more antioxidants ";
if (!JOptionPane.showInputDialog(null,"Enter color number 1:", title, 3).equalsIgnoreCase(colors[0])) { //if input is not red
JOptionPane.showMessageDialog(null, errorMessage,title,0);//show error message
System.exit(-1);//exit from the system
}
if (!JOptionPane.showInputDialog(null,"Enter color number 2:", title, 3) .equalsIgnoreCase(colors[1])) {//if input is not white
JOptionPane.showMessageDialog(null, errorMessage,title,0);//show error message
System.exit(-1);//exit from the system
}
if (!JOptionPane.showInputDialog(null,"Enter color number 3:", title, 3).equalsIgnoreCase(colors[2])) {//if input is not yellow
JOptionPane.showMessageDialog(null, errorMessage,title,0);//show error message
System.exit(-1);//exit from the system
}
if (!JOptionPane.showInputDialog(null,"Enter color number 4:", title, 3).equalsIgnoreCase(colors[3])) {//if input is not green
JOptionPane.showMessageDialog(null, errorMessage,title,0);//show error message
System.exit(-1);//exit from the system
}
if (!JOptionPane.showInputDialog(null,"Enter color number 5:", title, 3).equalsIgnoreCase(colors[4])) {//if input is not blue
JOptionPane.showMessageDialog(null, errorMessage,title,0);//show error message
System.exit(-1);//exit from the system
}
String congratsmessage = "Congratulations- your memory is perfect! ";
JOptionPane.showMessageDialog(null,congratsmessage,title,1); // displays Congratulations message
}
public static void main(String[] args) {
Prog7 prg = new Prog7();
}
}
Download here: http://www.mediafire.com/myfiles.php
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.