Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. package ia;
  2.  
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import javax.swing.ButtonGroup;
  6. import javax.swing.JRadioButton;
  7.  
  8. import java.awt.*;
  9.  
  10. @SuppressWarnings("serial")
  11. public class Quizzes extends EasyApp implements ActionListener {
  12.     public String username;
  13.  
  14.     public Quizzes(String username) {
  15.         setTitle("Quiz");
  16.         setBounds(300, 70, 500, 400);
  17.         Title.setFont(new Font("Arial", 0, 20));
  18.         Title.setForeground(Color.blue);
  19.         Correct.setForeground(Color.BLUE);
  20.         Wrong.setForeground(Color.RED);
  21.         Correct.setVisible(false);
  22.         Wrong.setVisible(false);
  23.         Option1.setBounds(50, 200, 130, 40);
  24.         Option1.setSelected(true);
  25.         Option2.setBounds(50, 250, 130, 40);
  26.         Option3.setBounds(50, 300, 130, 40);
  27.         add(Option1);
  28.         add(Option2);
  29.         add(Option3);
  30.         ButtonGroup group = new ButtonGroup();
  31.         group.add(Option1);
  32.         group.add(Option2);
  33.         group.add(Option3);
  34.        
  35.     }
  36.  
  37.     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)" }, { "" } };
  38.     String[] quizAnswers = { "(x+4)", "6e^2x", "ln(x)" };
  39.  
  40.     String StringQuestion = quizQuestions[0][0];
  41.     String StringOption1 = quizQuestions[0][1];
  42.     String StringOption2 = quizQuestions[0][2];
  43.     String StringOption3 = quizQuestions[0][3];
  44.  
  45.     int questionNumber = 0;
  46.     int Score = 0;
  47.  
  48.     Label Title = addLabel("Quiz", 50, 50, 400, 50, this);
  49.     Button Next = addButton("Next", 350, 350, 130, 40, this);
  50.     Button CheckAnswer = addButton("Check Answer", 160, 350, 180, 40, this);
  51.     Label Correct = addLabel("Correct", 300, 200, 200, 200, this);
  52.     Label Wrong = addLabel("Wrong", 300, 200, 200, 200, this);
  53.     Label PrintedQuestion = addLabel(StringQuestion, 50, 150, 500, 40, this);
  54.     Button Back = addButton("Back", 20, 350, 130, 40, this);
  55.  
  56.     JRadioButton Option1 = new JRadioButton(StringOption1);
  57.     JRadioButton Option2 = new JRadioButton(StringOption2);
  58.     JRadioButton Option3 = new JRadioButton(StringOption3);
  59.  
  60.     public void QuestionIncrement(int i) {
  61.         String[] f = quizQuestions[i];
  62.         PrintedQuestion.setText(f[0]);
  63.         Option1.setText(f[1]);
  64.         Option2.setText(f[2]);
  65.         Option3.setText(f[3]);
  66.     }
  67.  
  68.     public void usernameForScore() throws IOException {
  69.         String file = "/Users/shreyaschhabra/eclipse-workspace/IA/ScoreKeeper.txt";
  70.         BufferedReader userReader = new BufferedReader(new FileReader(file));
  71.         FileWriter FileWriter1 = new FileWriter(file, true);
  72.         BufferedWriter BufferedWriter = new BufferedWriter(FileWriter1);
  73.         String m = userReader.readLine(); // the problem is with f and m
  74.         PrintWriter writer = new PrintWriter(BufferedWriter, true);
  75.         System.out.print(m);
  76.         if (m == null) {
  77.             writer.println(this.username + "/" + Score);
  78.             return;
  79.         }
  80.  
  81.         do {
  82.             String usernameString = m.substring(0, m.indexOf("/"));
  83.             System.out.println(m);
  84.             if (!usernameString.equals(this.username)) {
  85.                 writer.println(this.username + "/" + Score);
  86.                 m = userReader.readLine();
  87.                 System.out.println("hi");
  88.                 m = userReader.readLine();
  89.             } else {
  90.                 String newScore = m.replaceAll(m, this.username + "/" + Score);
  91.                 writer.println(newScore);
  92.                 writer.flush();
  93.                 System.out.println("bye");
  94.                 m = userReader.readLine();
  95.             }
  96.         } while ((m = userReader.readLine()) != null);
  97.     }
  98.  
  99.     public void DisplayString(int m) {
  100.         outputString("The Quiz Is Over, You got " + (Score) + " right");
  101.     }
  102.  
  103.     public void actions(Object source, String command) {
  104.         String s1 = null;
  105.         if (source == CheckAnswer) {
  106.             if (Option1.isSelected()) {
  107.                 s1 = Option1.getText();
  108.             }
  109.             if (Option2.isSelected()) {
  110.                 s1 = Option2.getText();
  111.             }
  112.             if (Option3.isSelected()) {
  113.                 s1 = Option3.getText();
  114.             }
  115.             if (s1.equals(quizAnswers[questionNumber])) {
  116.                 Correct.setVisible(true);
  117.                 Score++;
  118.             } else {
  119.                 Wrong.setVisible(true);
  120.             }
  121.         }
  122.  
  123.         if (source == Next) {
  124.  
  125.             questionNumber++; // questionNumber is incremented
  126.  
  127.             if (questionNumber != quizAnswers.length) {
  128.                 QuestionIncrement(questionNumber); //questionNumber is passed on to QuestionIncrement
  129.             }
  130.             if (questionNumber == quizAnswers.length) { //checks if this is the last question
  131.                 DisplayString(questionNumber); //if it is, then it will go to the method DisplayString$
  132.                 try {
  133.                     usernameForScore();
  134.                 } catch (IOException e) {
  135.                     e.printStackTrace();
  136.                 }
  137.             }
  138.             Correct.setVisible(false); // erased again after next is pressed
  139.             Wrong.setVisible(false);
  140.  
  141.         }
  142.         if(source == Back) {
  143.             dispose();
  144.         }
  145.  
  146.     }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement