My application is a Mutliple choice quiz. Right now there are only two questions so i can test the logic, but after I figure it out ill add up to 100. Really I have a new Frame with buttons added to a panel and then the panel added to the JFrame. I then use nextQuestion() method to allow me to go through multiple questions as long as there are questions left.and there is also a timer that i through in their that gives a time for each question, 10 seconds.
protected void nextQuestion() {
timer.stop();
currentQuestion++;
if (currentQuestion >= quiz.size()) {
cardLayout.show(QuestionsPanel, "last");
next.setEnabled(false);
//Show however many correct after last question.
//iterate on the collection to count the selected radio buttons
//What im confused about is really how do i tell the program which (options) are right or wrong.
//and if i can do that how do i only count the correct ones.
//or for example do i have to take the total questions and subtract by the ones they didnt choose?
//a code example on how to do this would be great.
} else {
cardLayout.show(QuestionsPanel, Integer.toString(currentQuestion));
startTime = null;
next.setText("Next");
next.setEnabled(true);
timer.start();
}
}
public interface Question {
public String getPrompt();
public String getCorrectAnswer();
public String[] getOptions();
public String getUserResponse();
public void setUserResponse(String response);
public boolean isCorrect();
}
public class ChoiceQuestion implements Question {
private final String prompt;
private final String correctAnswer;
private final String[] options;
private String userResponse;
public ChoiceQuestion(String prompt, String correctAnswer, String... options) {
this.prompt = prompt;
this.correctAnswer = correctAnswer;
this.options = options;
}
@Override
public String getPrompt() {
return prompt;
}
@Override
public String getCorrectAnswer() {
return correctAnswer;
}
@Override
public String[] getOptions() {
return options;
}
@Override
public String getUserResponse() {
return userResponse;
}
@Override
public void setUserResponse(String response) {
userResponse = response;
}
@Override
public boolean isCorrect() {
return getCorrectAnswer().equals(getUserResponse());
}
}
public class QuestionPane extends JPanel {
private Question question;
public QuestionPane(Question question) {
this.question = question;
setLayout(new BorderLayout());
JLabel prompt = new JLabel("<html><b>" + question.getPrompt() + "</b></html>");
prompt.setHorizontalAlignment(JLabel.LEFT);
add(prompt, BorderLayout.NORTH);
JPanel guesses = new JPanel(new GridBagLayout());
guesses.setBorder(new EmptyBorder(10,10,10,10));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
List<String> options = new ArrayList<>(Arrays.asList(question.getOptions()));
options.add(question.getCorrectAnswer());
Collections.sort(options);
ButtonGroup bg = new ButtonGroup();
for (String option : options) {
JRadioButton btn = new JRadioButton(option);
bg.add(btn);
guesses.add(btn, gbc);
}
add(guesses);
}
public Question getQuestion() {
return question;
}
public class ActionHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
getQuestion().setUserResponse(e.getActionCommand());
}
}
}
}
SO really i just have an issue with doing that small if else code block. If you guys could help me out. I got some ideas about iterating a collection although i have no idea how to do that.These are just the classes im using and the method i am having a problem with.