Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Preliminaries Create a Java project. The name of the project must be _lab7 . For example, my project would be named, petruskg_lab7. Add two Java

image text in transcribedimage text in transcribed

Preliminaries

Create a Java project. The name of the project must be _lab7. For example, my project would be named, petruskg_lab7.

Add two Java classes named Applications (the main class) and ESPGame to the package colors in your project (NetBeans users: add Applications first and declare it the main class).

Exercises

In this lab you are going to create a program to solve an upgraded version of Programming Challenge ESP Game (#18, p317(321)) in text book "Starting Out with Java (6th)".

(1 pt)Class ESPGame contains one private data field: declare a String variable named chosenColor. That is, you declare the variable right in the class, not in any of the methods. Apply the private modifier in the declaration before the type.

The class contains three user defined methods named chooseColor( ), showColor( ) and guessColor( ). All the methods are public and none of them is static. See more detailed descriptions of the methods in point 4 below.

The methods make use of the Java library class Color. Import java.awt.Color and javax.swing.* to your project. Note that certain colors in Java are represented as named constants of the Color class, for instance Color.BLUE etc.

Method descriptions:

a. (6pts) chooseColor( )

return type : Color

takes an int type parameter

Accordingly, the method header is this:

public Color chooseColor(int num)

This method returns one of the colors Color.BLUE, Color.YELLOW, Color.RED,

Color.GREEN, Color.ORANGE, Color.CYAN according to the input parameter values

1, 2, 3, 4, 5, 6 in order

In the body of the method you declare the local variables color of type Color, and input of type int.

The method runs a switch selection. The switch is controlled by the variable we named input. A typical case in the switch is like

case 4:

color = Color.GREEN;

chosenColor = "green"; break;

Note that the data field chosenColor stores the name of the chosen color in String format.

The color choice for the default case in the switch is Color.BLACK.

After the switch finished the method returns the color value represented by the parameter number. The return statement is

return color;

b. (4pts) showColor()

As for the return type and parameters you have to create the method header according to the requirements

void

takes a Color type parameter

The purpose of this method is to create a small window for displaying the parameter color. Give the name color to the parameter and copy the following code into the methods body:

JFrame frame = new JFrame(Guess this color); frame.setSize(200,300); frame.setLocation(300,300); JPanel panel = new JPanel(); panel.setBackground(color); frame.add(panel); frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Note that you do not have to study neither to understand the above code fragment. However, you have to re-write the double quote operator into your program by hand, the copied character is wrong for Java.

c. (7pts) guessColor()

Requirements:

void

no parameter

This method runs a while loop to offer the ESP game to the user repeatedly. The offer is displayed on JOptionPane confirm dialog (Figure 1).

Figure 1

The if else statement controlled by the window works as follows:

For No answer the program exits. In particular, the while loop is stopped here with a break, thus you can run the loop with the boolean literal true. For Yes answer the method

randomly selects an integer between 1 and 6, use the Random class for this purpose

calls the chooseColor( ) method with the selected random number as parameter,

calls the showColor( ) method, the parameter is the return value of chooseColor( )

opens a JOptionPane input window as shown on Figure 3

As a result, the two windows of Figures 2 and 3 simultaneously appear on the screen:

having the user entered a choice, the method compares the selection to the chosenColor field value and using an if-else structure the result is displayed according to the templates of Figures 4 and 5.

Figure 4

Figure 5

5. (2pts) Class Applications contains nothing but the main method for which the code is supplied below.

public static void main(String[] args) {

ESPGame game = new ESPGame();

game.guessColor();

}

Document1 Word File Home Insert Design Layout References Mailings Review View Foxit PDF 9 Tell me what you want to do ESP game Enter the ESP game Yes No Figure Input Choose the name of the color you see: brown green Guess this color blue magenta cyan black orange yellow OK Cancel Figure 3 Figure 2 Page 1 of 2 10 words C Ask me anything Derrick White Jr X Share 58 PM A 4x 3/20/2017 R2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Sham Navathe,Ramez Elmasri

5th Edition

B01FGJTE0Q, 978-0805317558

More Books

Students also viewed these Databases questions

Question

Which user group is most interested in solvency ratios?

Answered: 1 week ago

Question

Discuss all branches of science

Answered: 1 week ago