chore: Lab04
This commit is contained in:
parent
70f0cdf757
commit
d780953136
6
Lab04/.classpath
Normal file
6
Lab04/.classpath
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
17
Lab04/.project
Normal file
17
Lab04/.project
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Lab04</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
13
Lab04/src/lab04/Lab04.java
Normal file
13
Lab04/src/lab04/Lab04.java
Normal file
@ -0,0 +1,13 @@
|
||||
package lab04;
|
||||
|
||||
public class Lab04 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Quiz quiz = new Quiz(new String[] { "5*5 =", "12-7 =", "9/3 =", "12+10 =", "55/5 =" },
|
||||
new String[][] { { "25", "62", "11", "5" }, { "75", "5", "11", "3" }, { "1", "2", "3", "4" },
|
||||
{ "55", "66", "77", "22" }, { "111", "1111", "1", "11" } },
|
||||
new int[] { 0, 1, 2, 3, 3 });
|
||||
quiz.start();
|
||||
}
|
||||
|
||||
}
|
82
Lab04/src/lab04/Question.java
Normal file
82
Lab04/src/lab04/Question.java
Normal file
@ -0,0 +1,82 @@
|
||||
package lab04;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class Question {
|
||||
private int questionNumber;
|
||||
private String question;
|
||||
private String[] answers;
|
||||
private int correct;
|
||||
private QuestionResponseEventHandler responseHandler;
|
||||
|
||||
private JFrame frame;
|
||||
|
||||
public Question(int questionNumber, String question, String[] answers, int correct,
|
||||
QuestionResponseEventHandler responseHandler) throws Exception {
|
||||
this.questionNumber = questionNumber;
|
||||
this.question = question;
|
||||
this.answers = answers;
|
||||
this.correct = correct;
|
||||
this.responseHandler = responseHandler;
|
||||
|
||||
if (answers.length != 4) {
|
||||
throw new Exception("Should be exactly 4 answers");
|
||||
}
|
||||
if (correct < 0 || correct > 3) {
|
||||
throw new Exception("Correct answer should be valid");
|
||||
}
|
||||
|
||||
this.renderForm();
|
||||
}
|
||||
|
||||
private void renderForm() {
|
||||
frame = new JFrame("Question #" + String.valueOf(this.questionNumber));
|
||||
|
||||
JPanel panel = new JPanel();
|
||||
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
|
||||
|
||||
panel.add(new JLabel(question));
|
||||
|
||||
JPanel answers = new JPanel();
|
||||
|
||||
Question that = this;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
JButton button = new JButton(this.answers[i]);
|
||||
boolean isCorrect = i == correct;
|
||||
button.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
that.handleAnswer(isCorrect);
|
||||
}
|
||||
});
|
||||
answers.add(button);
|
||||
}
|
||||
|
||||
panel.add(answers);
|
||||
|
||||
frame.add(panel);
|
||||
|
||||
frame.setSize(500, 150);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
private void handleAnswer(boolean isCorrect) {
|
||||
if (isCorrect) {
|
||||
JOptionPane.showMessageDialog(frame, "Your answer is correct!", "Hurray!", JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(frame, "Your answers is wrong. The correct is: " + this.answers[this.correct],
|
||||
"Oof", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
|
||||
this.frame.setVisible(false);
|
||||
this.responseHandler.onResponse(isCorrect);
|
||||
}
|
||||
}
|
5
Lab04/src/lab04/QuestionResponseEventHandler.java
Normal file
5
Lab04/src/lab04/QuestionResponseEventHandler.java
Normal file
@ -0,0 +1,5 @@
|
||||
package lab04;
|
||||
|
||||
public interface QuestionResponseEventHandler {
|
||||
public void onResponse(boolean isCorrect);
|
||||
}
|
63
Lab04/src/lab04/Quiz.java
Normal file
63
Lab04/src/lab04/Quiz.java
Normal file
@ -0,0 +1,63 @@
|
||||
package lab04;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
public class Quiz implements QuestionResponseEventHandler {
|
||||
private String[] questions;
|
||||
private String[][] answers;
|
||||
private int[] correct;
|
||||
|
||||
private int answerCorrect;
|
||||
private int currentQuestion;
|
||||
|
||||
public Quiz(String[] questions, String[][] answers, int[] correct) {
|
||||
this.questions = questions;
|
||||
this.answers = answers;
|
||||
this.correct = correct;
|
||||
|
||||
this.answerCorrect = 0;
|
||||
this.currentQuestion = -1;
|
||||
}
|
||||
|
||||
public boolean start() {
|
||||
if (this.currentQuestion == -1) {
|
||||
this.nextQuestion();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void nextQuestion() {
|
||||
this.currentQuestion++;
|
||||
|
||||
if (this.currentQuestion >= this.questions.length) {
|
||||
this.finishQuiz();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
new Question(this.currentQuestion + 1, this.questions[this.currentQuestion],
|
||||
this.answers[this.currentQuestion], this.correct[this.currentQuestion], this);
|
||||
} catch (Exception e) {
|
||||
JOptionPane.showMessageDialog(null, "ERROR: Invalid question: " + e.getMessage());
|
||||
this.nextQuestion();
|
||||
}
|
||||
}
|
||||
|
||||
private void finishQuiz() {
|
||||
JOptionPane.showMessageDialog(null,
|
||||
"Correct answers: " + String.valueOf(this.answerCorrect) + "/" + String.valueOf(this.questions.length)
|
||||
+ " | " + String.valueOf(this.answerCorrect * 100 / this.questions.length) + "%",
|
||||
"Quiz completed!", JOptionPane.QUESTION_MESSAGE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(boolean isCorrect) {
|
||||
if (isCorrect) {
|
||||
this.answerCorrect++;
|
||||
}
|
||||
|
||||
this.nextQuestion();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user