Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have some problems with my projects I am doing. This is a Java awt, swing java program. The program is below. Basically, I have

I have some problems with my projects I am doing. This is a Java awt, swing java program. The program is below. Basically, I have a group of multiple choice questions - 20 in total. I also have a digital clock in around 10 minutes that tells the time for someone who takes this games. Both of this classes can run separately, but when it combines it cannot run. Please help me to fix it. Furthermore, I need to create a graphical UI for he/she takes the games to log in with the password and a four-digit ID number. The ID number is randomly chosen and it will, of couse, not display for the person who takes the games. This program must also inform to he/she takes the test has 10 minutes to answer twenty questions and they should press the ENTER key to start the game. Finally, the test-taker may end the game at any time by clicking on SUBMIT button on this user interface. Thanks!

/* CLASS QUIZZ */

// create a group of multiple choice question.

import java.awt.Font; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import javax.swing.JOptionPane; import javax.swing.JFrame; import javax.swing.GroupLayout; import java.awt.event.ActionEvent; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.ButtonGroup; import javax.swing.WindowConstants;

class Quizz extends JFrame { // Variables declaration. private ButtonGroup buttonGroup1; private JButton jButton1; private JLabel lblQno; private JLabel lblQuestion; private javax.swing.JRadioButton rbAns1; private javax.swing.JRadioButton rbAns2; private javax.swing.JRadioButton rbAns3; private javax.swing.JRadioButton rbAns4; // End of variables declaration /* ArrayList to hold question numbers from 1 - 100 just initialized in contstructor and shuffled it each time it will be shuffled and random questions will be selected. */ ArrayList ar = new ArrayList<>(); //question number. int qno = 0; //Hold the answer of current question. String ans=""; //set difficulty leve where 1 = Easy; 2 = Intermediate; 3 = Hard, 4 = Impossible. int difficulty = 1; //Hold result for correct answers. int result = 0; /* Arrays declared below holds question from easy,intermediate and hard category where [x][0] index array represent question [x][1-4] represents answer options [x][5] index represent answer of question. */ String[][] easy = new String[][] { {"This is question with index 0 from EASY group","1","2","3","4","2"}, {"This is question with index 1 from EASY group","1","2","3","4","2"}, {"This is question with index 2 from EASY group","1","2","3","4","3"}, {"This is question with index 3 from EASY group","1","2","3","4","1"}, {"This is question with index 4 from EASY group","1","2","3","4","2"}, {"This is question with index 5 from EASY group","1","2","3","4","2"}, {"This is question with index 6 from EASY group","1","2","3","4","1"}, {"This is question with index 7 from EASY group","1","2","3","4","4"}, {"This is question with index 8 from EASY group","1","2","3","4","2"}, {"This is question with index 9 from EASY group","1","2","3","4","2"} }; String[][] intermediate = new String[][] { {"This is question with index 0 from INTERMEDIATE group","1","2","3","4","1"}, {"This is question with index 1 from INTERMEDIATE group","1","2","3","4","2"}, {"This is question with index 2 from INTERMEDIATE group","1","2","3","4","4"}, {"This is question with index 3 from INTERMEDIATE group","1","2","3","4","1"}, {"This is question with index 4 from INTERMEDIATE group","1","2","3","4","2"}, {"This is question with index 5 from INTERMEDIATE group","1","2","3","4","2"}, {"This is question with index 6 from INTERMEDIATE group","1","2","3","4","3"}, {"This is question with index 7 from INTERMEDIATE group","1","2","3","4","4"}, {"This is question with index 8 from INTERMEDIATE group","1","2","3","4","2"}, {"This is question with index 9 from INTERMEDIATE group","1","2","3","4","1"} }; String[][] hard = new String[][] { {"This is question with index 0 from HARD group","1","2","3","4","2"}, {"This is question with index 1 from HARD group","1","2","3","4","2"}, {"This is question with index 2 from HARD group","1","2","3","4","3"}, {"This is question with index 3 from HARD group","1","2","3","4","1"}, {"This is question with index 4 from HARD group","1","2","3","4","2"}, {"This is question with index 5 from HARD group","1","2","3","4","2"}, {"This is question with index 6 from HARD group","1","2","3","4","1"}, {"This is question with index 7 from HARD group","1","2","3","4","4"}, {"This is question with index 8 from HARD group","1","2","3","4","2"}, {"This is question with index 9 from HARD group","1","2","3","4","2"} }; String[][] impossible = new String[][] { {"This is question with index 0 from HARD group","1","2","3","4","5"}, {"This is question with index 1 from HARD group","1","2","3","4","4"}, {"This is question with index 2 from HARD group","1","2","3","4","3"}, {"This is question with index 3 from HARD group","1","2","3","4","1"}, {"This is question with index 4 from HARD group","1","2","3","4","2"}, {"This is question with index 5 from HARD group","1","2","3","4","3"}, {"This is question with index 6 from HARD group","1","2","3","4","3"}, {"This is question with index 7 from HARD group","1","2","3","4","3"}, {"This is question with index 8 from HARD group","1","2","3","4","3"}, {"This is question with index 9 from HARD group","1","2","3","4","3"} }; public Quizz() { initComponents(); //load arraylist with numbers ranging from 0 - 99 for(int i=0;i<20;i++) ar.add(i); //Now shuffle these numbers Collections.shuffle(ar); //Load Question loadQuestion(); } private void loadQuestion() { if(qno<20) { //Set Question serial no lblQno.setText("" + (qno + 1) + "/20");

//load questions by difficulty level // Easy if(difficulty==1) { lblQuestion.setText(easy[ar.get(qno)][0]); rbAns1.setText(easy[ar.get(qno)][1]); rbAns2.setText(easy[ar.get(qno)][2]); rbAns3.setText(easy[ar.get(qno)][3]); rbAns4.setText(easy[ar.get(qno)][4]); ans = easy[ar.get(qno)][5]; } //Intermediate if(difficulty==2) { lblQuestion.setText(intermediate[ar.get(qno)][0]); rbAns1.setText(intermediate[ar.get(qno)][1]); rbAns2.setText(intermediate[ar.get(qno)][2]); rbAns3.setText(intermediate[ar.get(qno)][3]); rbAns4.setText(intermediate[ar.get(qno)][4]); ans = intermediate[ar.get(qno)][5]; } //Hard if(difficulty==3) { lblQuestion.setText(hard[ar.get(qno)][0]); rbAns1.setText(hard[ar.get(qno)][1]); rbAns2.setText(hard[ar.get(qno)][2]); rbAns3.setText(hard[ar.get(qno)][3]); rbAns4.setText(hard[ar.get(qno)][4]); ans = hard[ar.get(qno)][5]; } // Impossible if(difficulty==4) { lblQuestion.setText(hard[ar.get(qno)][0]); rbAns1.setText(hard[ar.get(qno)][1]); rbAns2.setText(hard[ar.get(qno)][2]); rbAns3.setText(hard[ar.get(qno)][3]); rbAns4.setText(hard[ar.get(qno)][4]); ans = hard[ar.get(qno)][5]; } } else { JOptionPane.showMessageDialog(rootPane, "You have attempted all question and your score is " + result + "/20"); jButton1.setEnabled(false); } } private void initComponents() { buttonGroup1 = new ButtonGroup(); lblQuestion = new JLabel(); rbAns1 = new javax.swing.JRadioButton(); rbAns2 = new javax.swing.JRadioButton(); rbAns3 = new javax.swing.JRadioButton(); rbAns4 = new javax.swing.JRadioButton(); jButton1 = new JButton(); lblQno = new JLabel();

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

lblQuestion.setFont(new Font("Dialog", 1, 14)); // NOI18N lblQuestion.setText("jLabel1"); buttonGroup1.add(rbAns1); rbAns1.setText("jRadioButton1"); buttonGroup1.add(rbAns2); rbAns2.setText("jRadioButton2"); buttonGroup1.add(rbAns3); rbAns3.setText("jRadioButton3"); buttonGroup1.add(rbAns4); rbAns4.setText("jRadioButton4"); jButton1.setText("Submit"); jButton1.addActionListener((java.awt.event.ActionEvent evt) -> { jButton1ActionPerformed(evt); }); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(27, 27, 27).addComponent(lblQno).addGap(27, 27, 27) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(lblQuestion, GroupLayout.PREFERRED_SIZE, 473, GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING) .addComponent(rbAns2).addComponent(rbAns1) .addComponent(rbAns3) .addComponent(rbAns4))) .addContainerGap(89, Short.MAX_VALUE)) .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1) .addGap(251, 251, 251)) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(57, 57, 57) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(lblQuestion) .addComponent(lblQno)) .addGap(39, 39, 39) .addComponent(rbAns1) .addGap(18, 18, 18) .addComponent(rbAns2) .addGap(18, 18, 18) .addComponent(rbAns3) .addGap(18, 18, 18) .addComponent(rbAns4) .addGap(18, 18, 18) .addComponent(jButton1) .addContainerGap(38, Short.MAX_VALUE)) ); pack(); }// private void jButton1ActionPerformed(ActionEvent evt) { // TODO add your handling code here: String userAns = ""; if(rbAns1.isSelected()) userAns = "1"; if(rbAns2.isSelected()) userAns = "2"; if(rbAns3.isSelected()) userAns = "3"; if(rbAns4.isSelected()) userAns = "4"; if(userAns.equals("")) JOptionPane.showMessageDialog(rootPane, "Please select an answer first "); else { qno++; } boolean isAnswerCorrect = userAns.equals(ans); if(isAnswerCorrect) result++; //First 4 question will b selected from easy group if(qno < 4) loadQuestion(); else if(isAnswerCorrect) { if(difficulty==1 || difficulty==2) difficulty++; loadQuestion(); } else { if(difficulty==2 || difficulty==3) difficulty--; loadQuestion(); } } }

/* CLASS DIGITAL CLOCK */

import java.awt.Font; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Timer; import java.util.TimerTask; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JFrame;

class DigitalClock extends JFrame { // Variables int counter = 10; Boolean isIt = false; private JButton btnStart; private JButton btnStop; private JLabel timeLeft; private JLabel timerName; public DigitalClock() { initComponents2(); } private void initComponents2() { timerName = new JLabel(); timeLeft = new JLabel(); btnStop = new JButton(); btnStart = new JButton(); timerName.setFont(new Font("Tahoma", 0, 18)); timerName.setText("Timer: "); timeLeft.setFont(new Font("Tahoma", 1, 12)); timeLeft.setText("00:00"); btnStop.setText("Stop"); btnStop.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent evt) { btnStopMouseClicked(evt); } }); btnStart.setText("Start"); btnStart.setActionCommand("Start"); btnStart.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent evt) { btnStartMouseClicked(evt); } }); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(55, 55, 55) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(btnStart) .addGap(47,47,47) .addComponent(btnStop)) .addGroup(layout.createSequentialGroup() .addComponent(btnStop) .addGap(18, 18, 18) .addComponent(timeLeft))) .addContainerGap(75, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(60, 60, 60) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(timerName) .addComponent(timeLeft)) .addGap(32, 32, 32) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(btnStart) .addComponent(btnStop)) .addContainerGap(73, Short.MAX_VALUE)) ); } private void btnStartMouseClicked(MouseEvent evt) { Timer timer = new Timer(); // new Timer counter = 10; // setting the counter to 10 secs Thread thread = new Thread(); TimerTask task = new TimerTask() { @Override public void run() { timeLeft.setText(Integer.toString(counter)); // the timer label to counter. counter--; try { Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(DigitalClock.class.getName()).log(Level.SEVERE, null, ex); } if(counter == -1) { timer.cancel(); } else if(isIt){ timer.cancel(); isIt = false; } } }; timer.scheduleAtFixedRate(task, 1000, 1000); // (task, delay, period) } private void btnStopMouseClicked(MouseEvent evt) { isIt = true; // changing the boolean isIt to true, which will stop the timer. } }

/* CLASS MAIN TO RUN THE PROGRAM */

import javax.swing.UIManager.LookAndFeelInfo; import java.util.logging.Logger; import java.util.logging.Level;

public class Main { public static void main(String[] args) { try { for (LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { Logger.getLogger(Quizz.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(DigitalClock.class.getName()).log(Level.SEVERE, null, ex); }

java.awt.EventQueue.invokeLater(() -> { new Quizz().setVisible(true); new DigitalClock().setVisible(true); }); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions

Question

d) Translate signed decimal numbers of -78 to binary. (2 marks)

Answered: 1 week ago