This week we will work on the Jeopardy program.
Mr Mulkey will be absent Wed-Fri. If you get stuck and need help, you should talk to another student, or send an e-mail to : Dave_Mulkey@fis.edu . You won't get an immediate answer, but certainly within a couple hours.
Follow the instructions in the assignment. Bring a working program on Monday, 20 Oct to be graded. You may share ideas with other students, but don't copy code directly.
This program demonstrates
the use of EasyApp to create a GUI interface. (download
EasyApp)
The program is a simple version of the game Jeopardy,
where the user answers questions
and wins money for correct answers and loses
money for incorrect answers.
After each question, the button is disabled,
so that question cannot be chosen again.
import java.awt.*;
public class Jeopardy extends EasyApp
{
public static void main(String[] args)
{ new Jeopardy(); }
Label lJeopardy = addLabel("Jeopardy",60,30,200,60,this);
Label lScore = addLabel("Score",300,50,50,30,this);
TextField tScore = addTextField("0",350,50,80,30,this);
Button bScience = addButton("Science",50,100,100,50,this);
Button bSports = addButton("Sports",150,100,100,50,this);
Button bHistory = addButton("History",250,100,100,50,this);
Button bMath = addButton("Math",350,100,100,50,this);
Button bSports2 = addButton("Sports 200",150,150,100,50,this);
Button bPlayAgain = addButton("Play again",50,250,400,50,this);
double score = 0;
public Jeopardy() // Constructor - change window appearance
{
setSize(500,500);
setTitle("Jeopardy - (c) 2005 Dave Mulkey, Germany");
lJeopardy.setFont(new Font("Arial",1,36));
lJeopardy.setBackground(new Color(255,255,180));
lJeopardy.setForeground(Color.blue);
lScore.setBackground(new Color(255,255,180));
setBackground(new Color(255,255,180));
bScience.setFont(new Font("Arial",1,16));
bMath.setFont(new Font("Arial",1,16));
bHistory.setFont(new Font("Arial",1,16));
bSports.setFont(new Font("Arial",1,16));
bSports2.setFont(new Font("Arial",1,16));
bPlayAgain.setFont(new Font("Arial",1,16));
}
public void actions(Object source,String command)
{
if (source == bScience)
{ science(); }
if (source == bSports)
{ sports(); }
if (source == bHistory)
{ history(); }
if (source == bMath)
{ math(); }
if (source == bSports2)
{ sports2(); }
if (source == bPlayAgain)
{
bScience.setEnabled(true);
bHistory.setEnabled(true);
bSports.setEnabled(true);
bMath.setEnabled(true);
score = 0;
}
tScore.setText(score + "");
}
public void science()
{
String guess = inputString("f = ma is a ________ law");
if (guess.equals("physics"))
{
score = score + 100;
output("Right!");
}
else
{
score = score - 100;
output("Wrong..." );
}
bScience.setEnabled(false);
}
public void sports()
{
String guess = inputString("What sport did Pele play?");
if (
guess.equalsIgnoreCase("soccer")
|| guess.equalsIgnoreCase("football")
|| guess.equalsIgnoreCase("fussball")
) // || or
{
score = score + 100;
output("Right!");
}
else
{
score = score - 100;
output("Wrong...");
}
bSports.setEnabled(false);
}
public void history()
{
int guess = inputInt("What year did WW II end?");
if (guess == 1945)
{
score = score + 100;
output("Right!");
}
else
{
score = score - 100;
output("Wrong... ");
}
bHistory.setEnabled(false);
}
public void math()
{
int guess = inputInt("What is 5! ?");
if (guess == 120)
{
score = score + 100;
output("Right!");
}
else
{
score = score - 100;
output("Wrong...");
}
bMath.setEnabled(false);
}
public void sports2()
{
int guess = inputInt("What is a perfect score in bowling?");
if ( guess == 300 )
{
score = score + 200;
output("Right!");
}
else
{
score = score - 200;
output("Wrong... " );
}
bSports2.setEnabled(false);
}
}
/***********************************************************************
(1) Add buttons for at least 3 questions in each category
(100, 200, 300) -
that makes a total of 12 questions. Make sure each button is disabled after use,
and that it
adds or subtracts the correct amount.
Improve the ANSWER algorithm(s):
(2) Change the IF commands so they accept both CAPITAL and small letters.
(3) Add a HINT for each question by telling:
(a)
the FIRST LETTER of the answer and
(b) the number of letters
in the answer
(4) Use some STRING commands to:
(a)
make the scoring NON-case-sensitive
(b) accept an answer
as long as the first 3 letters and the last
letter
are correct - thus, it would ignore some spelling mistakes.
(c)
Allow the person to guess again if their answer is PART OF
(contained
in) the correct answer.
Make a better game:
*********************************************************************/