Count the Monkeys

For this assignment:

1. Make the code runnable.

2. Code your answers on the sheet.

3. This link to JTextFields may prove useful. It explains Integer.parseInt (2b) and it shows how to clear a textField (2a).

4. You must submit your sheet by the end of the period. However, it shouldn't take you that long.

The 8 Pictures you need:

(name as: blank.gif, m1.gif, m2.gif, m3.gif, m4.gif, m5.gif, m6.gif, m7.gif)

The Starter Code: (Cut and paste and hit indent).

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

public class __________________ extends Applet implements ActionListener
{
JButton ______________, done;

JLabel action, __________, pic;
int monkey = 0, ques = 0, right = 0;

JTextField _____________;

public void init ()
{
JLabel title = new JLabel ("Count the Monkeys!");

title.setFont (new ________ ("Arial", Font.BOLD, 40));
title.___________________ (Color.blue);

JLabel words = new JLabel (" Press next question. ______________________________________ ");
words.setFont (new Font ("Arial", Font.ITALIC, 14));
JLabel morewords = new JLabel (" If you are right,_________________________________ ");
morewords.setFont (new Font ("Arial", Font.ITALIC, 14));

JLabel monk = new JLabel (" The Monkeys: ");
monk.setFont (new Font ("Arial", Font.BOLD, 18));
pic = new JLabel (createImageIcon ("blank.gif"));

JLabel inst = new JLabel ("How many monkeys?");
count = new JTextField (3);

done = new JButton ("I'm done.");
done.setActionCommand ("done");
done.addActionListener (__________________);

done.setEnabled (___________);
action = new JLabel ("Press new question to begin. ");
action.setFont (new Font ("Arial", Font.BOLD, 18));

score = new JLabel ("Score: ________________________________________________ ");
score.setFont (new Font ("Arial", Font.PLAIN, 14));

next = new JButton ("_____________________.");

next._________________ ("again");
next.addActionListener (______________);

add (__________________);
Panel p = new Panel (new GridLayout (2, 1));
p.add (words);
p.add (morewords);
p.setBackground (Color.lightGray);
add (p);
add (monk);
add (pic);
Panel p2 = new Panel ();

p2.add (______________);

p2.add (______________);

p2.add (______________);
p2.setBackground (Color.lightGray);
add (p2);
add (action);
add (score);

add (_________________);
resize (500, 350);
}


public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("again"))
{ //(a) 5 other lines of code are needed to make this work. What are they?

monkey = (int) (Math.random () * 7 + 1);


}


/* (b) On your sheet, For each part of the else if, put a comment beside each line explaining what it does, in plain English. */

else if (e.getActionCommand ().equals ("done"))
{
int u = Integer.parseInt (count.getText ());
ques++;
if (u == monkey)
{
action.setText ("You win! 1 point.");
right++;
}
else
action.setText ("Wrong, " + monkey + " monkies!");
score.setText ("Score: You have " + right + " out of " + ques + " right.");
next.setEnabled (true);
done.setEnabled (false);
}
}


protected static ImageIcon createImageIcon (String path)
{
java.net.URL imgURL = countMonkey.class.getResource (path);
if (imgURL != null)
return new ImageIcon (imgURL);
else
{
System.err.println ("Couldn't find file: " + path);
return null;
}
}
}