Purchase Solution

Question about Java program to memorize a sequence of colors

Not what you're looking for?

Ask Custom Question

The files must be called <YourNameProg7.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.
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:

After closing the dialog box, here's the main window:

After typing the first color:

After pressing enter and then typing the second color:

After entering all five colors correctly:

Second sample session:
After closing the dialog box and typing the first color:

After pressing enter and then typing the second color:

After pressing enter:

You can get full credit for completing this section only.
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:

After clicking the hint button:

After typing the first color:

After pressing enter and clicking the hint button:

After entering all five colors correctly:

Purchase this Solution

Solution Summary

A java program in a single java file<YourNameProg7.java>. When launched you will see a question "How good is your memory? try to memorize following color sequence". Then the form will appear where the user can enter his guess. If the user correctly enters all the colors, the program prints a "Congratulations" message.

Solution Preview

Please find below the java code. I have compiled and tested it in NetBeans. I have added the Hint button so you would be able to get extra credits. The logic is simple. attemptNumber variable keeps track of the attempt number. There is an event listener on the text field that checks the color entered. Good luck!

HERE IS THE SAME CODE BUT WHICH EXTENDS JFRAME. I HAVE MOVED JFRAME SETTINGS INTO ITS CONSTRUCTOR (YourNameProg7) SO THE MAIN() IS NOW VERY SIMPLE.

You can also test the program using javac.exe compiler as follows:

To compile:

javac.exe -g YourNameProg7.java

Then run:

java YourNameProg7

Source Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class YourNameProg7 extends JFrame {
JLabel colLabel=new JLabel("Enter Color Number 1: ");
JTextField colText=new JTextField(10);
JButton butHint=new JButton("Hint");

int attemptNumber = 1;

...

Purchase this Solution


Free BrainMass Quizzes
C++ Operators

This quiz tests a student's knowledge about C++ operators.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Basic Networking Questions

This quiz consists of some basic networking questions.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.