Here's my entire code:
CODE
//Import packages
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AdvancedJavaHangman extends JFrame
{
private static final int WIDTH = 600;
private static final int HEIGHT = 1000;
private static int numOfWrongAns = 0;
private static int score = 0;
private static int quizQuesNum = 0;
private static boolean isUsed;
private static int searchResult;
ImageIcon icon = new ImageIcon("Hangman.jpg", "Hangman");
//Instance variables
private JLabel questionLabel, hangLabel;
private JButton ansButtonA, ansButtonB, ansButtonC, ansButtonD, exitButton;
private ButtonHandler ansButtonHandler;
public AdvancedJavaHangman()
{
String[] question = new String[10]; //array to hold all questions
String[] ansA = new String[10]; //array to hold all answer A's
String[] ansB = new String[10]; //array to hold all answer B's
String[] ansC = new String[10]; //array to hold all answer C's
String[] ansD = new String[10]; //array to hold all answer D's
String[] correctAns = new String[10]; //array to hold all correct answer letters
Vector<Integer> usedList = new Vector<Integer>();//vector to hold used question numbers
int questionNum;
try
{
BufferedReader in = new BufferedReader(new FileReader("JavaQuestions.txt"));
//read questions and answers into arrays
for(int num = 0; num < 10; num++)
{
question[num] = in.readLine();
ansA[num] = in.readLine();
ansB[num] = in.readLine();
ansC[num] = in.readLine();
ansD[num] = in.readLine();
correctAns[num] = in.readLine();
}//end for
in.close();
}//end try
catch(IOException ioe)
{
System.out.println(ioe.toString());
}//end catch
QuestionInfo[] questionObj = new QuestionInfo[10];
for(int x = 0; x < 10; x++)
{
questionObj[x] = new QuestionInfo(question[x], ansA[x], ansB[x], ansC[x], ansD[x], correctAns[x]);
}
questionNum = (int)(Math.random() * 10);
isUsed = true;
while (isUsed)
{
searchResult = seqSearch(usedList, questionNum);
if (searchResult == -1)
{
isUsed = false;
}//end if
else
questionNum = (int)(Math.random() * 10);
}//end while
usedList.addElement(questionNum);
QuestionInfo curQues = new QuestionInfo(questionObj[questionNum]);
displayQuestion(questionObj, curQues, questionNum);
}//end method AdvancedJavaHangman
private class ButtonHandler implements ActionListener
{
private QuestionInfo currentQues = new QuestionInfo();
public ButtonHandler()
{
currentQues = new QuestionInfo();
}
public ButtonHandler(QuestionInfo currentQuestion)
{
currentQues = new QuestionInfo(currentQuestion);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Exit"))
System.exit(0);
else if (e.getActionCommand().equals(currentQues.getAnsA()))
{
String userAns = "A";
checkAnswer(currentQues, userAns);
}//end if
else if (e.getActionCommand().equals(currentQues.getAnsB()))
{
String userAns = "B";
checkAnswer(currentQues, userAns);
}//end if
else if (e.getActionCommand().equals(currentQues.getAnsC()))
{
String userAns = "C";
checkAnswer(currentQues, userAns);
}//end if
else if (e.getActionCommand().equals(currentQues.getAnsD()))
{
String userAns = "D";
checkAnswer(currentQues, userAns);
}//end if
else
JOptionPane.showMessageDialog(null, "An error has occurred!", "ERROR!", JOptionPane.ERROR_MESSAGE);
}//end method actionPerformed
}//end class ButtonHandler
public void checkAnswer(QuestionInfo currentQ, String uAnswer)
{
String correctAnswer = currentQ.getCorrectAns();
if(correctAnswer.equals(uAnswer))
{
String ansMessage = "Correct!";
score+= 100;
JOptionPane.showMessageDialog(null, ansMessage, "Advanced Java Hangman", JOptionPane.PLAIN_MESSAGE);
}//end if
else
{
String ansMessage = "Incorrect!";
numOfWrongAns++;
JOptionPane.showMessageDialog(null, ansMessage, "Advanced Java Hangman", JOptionPane.PLAIN_MESSAGE);
}//end else
quizQuesNum++;
AdvancedJavaHangman play = new AdvancedJavaHangman();
}//end method checkAnswer
public void displayQuestion(QuestionInfo[] questions, QuestionInfo cur_Ques, int quesNumber)
{
if((quizQuesNum < 10) && (numOfWrongAns < 3))
{
ImageIcon hangIcon = new ImageIcon();
if(numOfWrongAns == 0)
hangIcon = new ImageIcon("Hangman0.jpg", "Hangman");
else if(numOfWrongAns == 1)
hangIcon = new ImageIcon("Hangman2.jpg", "Hangman");
else if(numOfWrongAns == 2)
hangIcon = new ImageIcon("Hangman3.jpg", "Hangman");
setTitle("Advanced Java Hangman: Question " + (quizQuesNum + 1));
setSize(WIDTH, HEIGHT);
Container pane = getContentPane();
pane.setLayout(null);
ansButtonHandler = new ButtonHandler(cur_Ques);
questionLabel = new JLabel(cur_Ques.getQuestion(), SwingConstants.CENTER);
questionLabel.setSize(580, 50);
questionLabel.setLocation(20, 520);
hangLabel = new JLabel(hangIcon);
hangLabel.setSize(480, 480);
hangLabel.setLocation(100, 20);
ansButtonA = new JButton(cur_Ques.getAnsA());
ansButtonA.setSize(200, 50);
ansButtonA.setLocation(200, 590);
ansButtonA.addActionListener(ansButtonHandler);
ansButtonB = new JButton(cur_Ques.getAnsB());
ansButtonB.setSize(200, 50);
ansButtonB.setLocation(200, 660);
ansButtonB.addActionListener(ansButtonHandler);
ansButtonC = new JButton(cur_Ques.getAnsC());
ansButtonC.setSize(200, 50);
ansButtonC.setLocation(200, 730);
ansButtonC.addActionListener(ansButtonHandler);
ansButtonD = new JButton(cur_Ques.getAnsD());
ansButtonD.setSize(200, 50);
ansButtonD.setLocation(200, 800);
ansButtonD.addActionListener(ansButtonHandler);
exitButton = new JButton("Exit");
exitButton.setSize(75, 50);
exitButton.setLocation(263, 890);
exitButton.addActionListener(ansButtonHandler);
pane.add(questionLabel);
pane.add(ansButtonA);
pane.add(ansButtonB);
pane.add(ansButtonC);
pane.add(ansButtonD);
pane.add(hangLabel);
pane.add(exitButton);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}//end if
else if((quizQuesNum == 10) && (numOfWrongAns < 3))
{
String gameOverStr = "You Win!\nYour score is " + score + ".";
JOptionPane.showMessageDialog(null, gameOverStr, "Advanced Java Hangman: Game Over", JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
else
{
String gameOverStr = "Sorry, You have three wrong answers!\nGame Over!\nYour total score is " + score + ".";
JOptionPane.showMessageDialog(null, gameOverStr, "Advanced Java Hangman: Game Over",
JOptionPane.PLAIN_MESSAGE);
setTitle("Advanced Java Hangman: Game Over ");
setSize(WIDTH, HEIGHT);
ImageIcon hangIcon = new ImageIcon("Hangman.jpg");
Container pane = getContentPane();
pane.setLayout(null);
ansButtonHandler = new ButtonHandler(cur_Ques);
hangLabel = new JLabel(hangIcon);
hangLabel.setSize(480, 480);
hangLabel.setLocation(100, 20);
exitButton = new JButton("Exit");
exitButton.setSize(75, 50);
exitButton.setLocation(263, 890);
exitButton.addActionListener(ansButtonHandler);
pane.add(hangLabel);
pane.add(exitButton);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}//end else
}//end method displayQuestion
//class QuestionInfo defintion
public class QuestionInfo
{
//declare instance variables
private String question;
private String answerA;
private String answerB;
private String answerC;
private String answerD;
private String correctAnswer;
private boolean isUnused;
//default constructor
public QuestionInfo()
{
setQuestionInfo("", "", "", "", "", "");
}
//constructor with parameters
public QuestionInfo(QuestionInfo otherQues)
{
question = otherQues.question;
answerA = otherQues.answerA;
answerB = otherQues.answerB;
answerC = otherQues.answerC;
answerD = otherQues.answerD;
correctAnswer = otherQues.correctAnswer;
isUnused = otherQues.isUnused;
}
//constructor with parameters
public QuestionInfo(String ques, String qAnsA, String qAnsB, String qAnsC, String qAnsD, String correctAns)
{
setQuestionInfo(ques, qAnsA, qAnsB, qAnsC, qAnsD, correctAns);
}
//method to set QuestioonInfo object
public void setQuestionInfo(String ques, String qAnsA, String qAnsB, String qAnsC, String qAnsD, String correctAns)
{
question = ques;
answerA = qAnsA;
answerB = qAnsB;
answerC = qAnsC;
answerD = qAnsD;
correctAnswer = correctAns;
isUnused = true;
}
//method to get the question
public String getQuestion()
{
return question;
}
//method to get answer A
public String getAnsA()
{
return answerA;
}
//method to get answer B
public String getAnsB()
{
return answerB;
}
//method to get answer C
public String getAnsC()
{
return answerC;
}
//method to get answer D
public String getAnsD()
{
return answerD;
}
//method to get the quesion's correct answer
public String getCorrectAns()
{
return correctAnswer;
}
//method to find out if the question has been previously used
public boolean getIsUnused()
{
return isUnused;
}
//method to change the value of isUnused to false
public void changeToUsed()
{
isUnused = false;
}
//method to get a copy of the object
public QuestionInfo getCopy()
{
QuestionInfo temp = new QuestionInfo();
temp.question = question;
temp.answerA = answerA;
temp.answerB = answerB;
temp.answerC = answerC;
temp.answerD = answerD;
temp.correctAnswer = correctAnswer;
temp.isUnused = isUnused;
return temp;
}
}//end class QuestionInfo
public static void displayRules()
{
String rulesStr = "Advanced Java Hangman!\n"
+ "You will be asked a series of multiple choice questions from material\n"
+ "covered in the Advanced Java course at Baker College.You win by answering\n"
+ "at least 8 out of 10 questions correctly. Each time you answer a question \n"
+ "incorrectly part of the hangman image appears. After three wrong answers,\n"
+ "the entire image appears and you lose the game.\n"
+ "Hope you studied!";
JOptionPane.showMessageDialog(null, rulesStr, "Advanced Java Hangman: Rules", JOptionPane.PLAIN_MESSAGE);
}
public int seqSearch(Vector<Integer> usedList, int questionNum)
{
boolean found = false;
for (Integer num : usedList)
if (num == questionNum)
{
found = true;
break;
}
if (found)
return 1;
else
return -1;
}
public static void main(String[] args)
{
displayRules();
AdvancedJavaHangman playGame = new AdvancedJavaHangman();
}//end method main
My file looks like so:
what is the logical primitive data type?
double
int
Boolean
String
C
What is the 16-bit Java character set?
char
Unicode
float
binary
B
Which method would you use to compare Strings?
getLength
compareString
countChar
compareTo
D
What class is used to develop a drop-down list?
JScrollPane
Component
abstract
JComboBox
D
What data type is actually tested in a While and Do-While loop?
double
Boolean
int
char
B
What is a dynamic way to implement a list in Java?
use the Vector class
use an array
use the List class
use an Object class
A
What sort technique always uses n-1 passes where n is the length of the list?
Sequential
list
bubble sort
binary
C
What is the superclass for applets?
Object
JApplet
JFrame
String
B
What is a unique instance of a class called?
wrapper class
superclass
Object
inheritance
C
What keyword is needed to derive a subclass from a superclass?
implements
instanceOf
extends
super
C