Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ia;
- import java.awt.event.*;
- import java.io.*;
- import javax.swing.ButtonGroup;
- import javax.swing.JRadioButton;
- import java.awt.*;
- @SuppressWarnings("serial")
- public class Quizzes extends EasyApp implements ActionListener {
- public String username;
- public Quizzes(String username) {
- setTitle("Quiz");
- setBounds(300, 70, 500, 400);
- Title.setFont(new Font("Arial", 0, 20));
- Title.setForeground(Color.blue);
- Correct.setForeground(Color.BLUE);
- Wrong.setForeground(Color.RED);
- Correct.setVisible(false);
- Wrong.setVisible(false);
- Option1.setBounds(50, 200, 130, 40);
- Option1.setSelected(true);
- Option2.setBounds(50, 250, 130, 40);
- Option3.setBounds(50, 300, 130, 40);
- add(Option1);
- add(Option2);
- add(Option3);
- ButtonGroup group = new ButtonGroup();
- group.add(Option1);
- group.add(Option2);
- group.add(Option3);
- }
- String[][] quizQuestions = { { "Which of the following is a factor of x² + 2x - 24?", "(x+8)", "(x+5)", "(x+4)" }, { "Which of the following is the derivative of 3e^2x?", "3e^2x", "3e^x", "6e^2x" }, { "Which of the following is obtained by integrating 1/x", "ln(x)", "1/x", "ln(x^2)" }, { "" } };
- String[] quizAnswers = { "(x+4)", "6e^2x", "ln(x)" };
- String StringQuestion = quizQuestions[0][0];
- String StringOption1 = quizQuestions[0][1];
- String StringOption2 = quizQuestions[0][2];
- String StringOption3 = quizQuestions[0][3];
- int questionNumber = 0;
- int Score = 0;
- Label Title = addLabel("Quiz", 50, 50, 400, 50, this);
- Button Next = addButton("Next", 350, 350, 130, 40, this);
- Button CheckAnswer = addButton("Check Answer", 160, 350, 180, 40, this);
- Label Correct = addLabel("Correct", 300, 200, 200, 200, this);
- Label Wrong = addLabel("Wrong", 300, 200, 200, 200, this);
- Label PrintedQuestion = addLabel(StringQuestion, 50, 150, 500, 40, this);
- Button Back = addButton("Back", 20, 350, 130, 40, this);
- JRadioButton Option1 = new JRadioButton(StringOption1);
- JRadioButton Option2 = new JRadioButton(StringOption2);
- JRadioButton Option3 = new JRadioButton(StringOption3);
- public void QuestionIncrement(int i) {
- String[] f = quizQuestions[i];
- PrintedQuestion.setText(f[0]);
- Option1.setText(f[1]);
- Option2.setText(f[2]);
- Option3.setText(f[3]);
- }
- public void usernameForScore() throws IOException {
- String file = "/Users/shreyaschhabra/eclipse-workspace/IA/ScoreKeeper.txt";
- BufferedReader userReader = new BufferedReader(new FileReader(file));
- FileWriter FileWriter1 = new FileWriter(file, true);
- BufferedWriter BufferedWriter = new BufferedWriter(FileWriter1);
- String m = userReader.readLine(); // the problem is with f and m
- PrintWriter writer = new PrintWriter(BufferedWriter, true);
- System.out.print(m);
- if (m == null) {
- writer.println(this.username + "/" + Score);
- return;
- }
- do {
- String usernameString = m.substring(0, m.indexOf("/"));
- System.out.println(m);
- if (!usernameString.equals(this.username)) {
- writer.println(this.username + "/" + Score);
- m = userReader.readLine();
- System.out.println("hi");
- m = userReader.readLine();
- } else {
- String newScore = m.replaceAll(m, this.username + "/" + Score);
- writer.println(newScore);
- writer.flush();
- System.out.println("bye");
- m = userReader.readLine();
- }
- } while ((m = userReader.readLine()) != null);
- }
- public void DisplayString(int m) {
- outputString("The Quiz Is Over, You got " + (Score) + " right");
- }
- public void actions(Object source, String command) {
- String s1 = null;
- if (source == CheckAnswer) {
- if (Option1.isSelected()) {
- s1 = Option1.getText();
- }
- if (Option2.isSelected()) {
- s1 = Option2.getText();
- }
- if (Option3.isSelected()) {
- s1 = Option3.getText();
- }
- if (s1.equals(quizAnswers[questionNumber])) {
- Correct.setVisible(true);
- Score++;
- } else {
- Wrong.setVisible(true);
- }
- }
- if (source == Next) {
- questionNumber++; // questionNumber is incremented
- if (questionNumber != quizAnswers.length) {
- QuestionIncrement(questionNumber); //questionNumber is passed on to QuestionIncrement
- }
- if (questionNumber == quizAnswers.length) { //checks if this is the last question
- DisplayString(questionNumber); //if it is, then it will go to the method DisplayString$
- try {
- usernameForScore();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- Correct.setVisible(false); // erased again after next is pressed
- Wrong.setVisible(false);
- }
- if(source == Back) {
- dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement