creating a JOptionPane quiz

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2007
Posts: 57
Reputation: piers is an unknown quantity at this point 
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

creating a JOptionPane quiz

 
0
  #1
Oct 22nd, 2007
This is the hardest thing I have tried yet. I am not adding ti as part of the main programme I am doing at the moment cus otherwise it would be too hard to debug. So I have created this as a separate file. I am trying to get a Joptionpane to show ask a question with 2 radio buttons to select the answer. IF the correct answer is shown then another JOptionpane will show saying correct. I also have with this a way to record correct questions you will understand better from the code. once the correct or incorrect box for selecting the wrong answer shows then when the user presses okay it goes to the next question. I am only starting with two questions to keep it simple then once thats mastered I will create 40. anyway here is my code. so anything I am doing wrong or need to do please show me. my biggest problem is an error saying incompatible types. it is cus I am trying to make an equal between a radiobutton and a string but I dont know how else to do it.


import javax.swing.*;

public class QuizFourBib
{
public static void main (String args[])
{
int res = 0;
JRadioButton dav = new JRadioButton("David");
JRadioButton saul = new JRadioButton("saul");
String ques1 = JOptionPane.showInputDialog(null,"who killed goliath?"+dav+saul,"question1",JOptionPane.QUESTION_MESSAGE);

if (ques1 == dav)
{
res=res+1;

}
else if (ques1 == saul)
{
res=res+0;
JOptionPane.showMessageDialog(null,"hard luck","",JOptionPane.INFORMATION_MESSAGE);
}
JRadioButton egypt = new JRadioButton("egypt");
JRadioButton samaria = new JRadioButton("samaria");
String ques2 = JOptionPane.showInputDialog(null,"where was moses born?"+egypt+samaria,"question1",JOptionPane.QUESTION_MESSAGE);
if (ques2 == egypt)
{
res=res+1;

}
else if (ques2 == samaria)
{
res=res+0;
JOptionPane.showMessageDialog(null,"hard luck","",JOptionPane.INFORMATION_MESSAGE);
}
System.out.println(res);
}


}
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: creating a JOptionPane quiz

 
0
  #2
Oct 22nd, 2007
Well, you can't quite work with a custom input dialog like that. There is a way to do it with an Object array for responses, but if you want radio buttons you need to create your own custom dialog. Just for the heck of it, I wrote up one way you could put this all together with JDialogs. Of course, you could probably do it a bit easier with a simple panel. Perhaps this will give you some ideas to work from (the simple BoxLayout is pretty awful)
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.util.ArrayList;
  4. import java.util.Enumeration;
  5. import java.util.List;
  6. import javax.swing.*;
  7.  
  8. public class QuizFourBib extends JDialog {
  9. List<Question> questions = new ArrayList<Question>();
  10. int score=0;
  11. ButtonGroup group = new ButtonGroup();
  12. JButton btnOk = new JButton("Ok");
  13.  
  14. public QuizFourBib(){
  15. super();
  16. setModal(true);
  17. setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
  18. btnOk.addActionListener(new ActionListener() {
  19. public void actionPerformed(ActionEvent e) {
  20. setVisible(false);
  21. }
  22. });
  23.  
  24. questions.add(new Question("Who killed Goliath?", new String[]{"David", "Saul"}, "David"));
  25. questions.add(new Question("Where was Moses born?", new String[]{"Egypt","Samaria"}, "Egypt"));
  26. }
  27.  
  28. public int startQuiz(){
  29. int score=0;
  30. for (Question q : questions){
  31. displayQuestion(q);
  32. if (group.getSelection().getActionCommand().equals(q.getCorrectAnswer())){
  33. score++;
  34. }
  35. }
  36. dispose();
  37. return score;
  38. }
  39.  
  40. private void displayQuestion(Question q){
  41. getContentPane().removeAll();
  42. for (Enumeration buttonGroup=group.getElements(); buttonGroup.hasMoreElements(); ){
  43. group.remove((AbstractButton)buttonGroup.nextElement());
  44. }
  45.  
  46. JLabel questionText = new JLabel(q.getQuestion());
  47. getContentPane().add(questionText);
  48. for (String answer : q.getAnswers()){
  49. JRadioButton radio = new JRadioButton(answer);
  50. radio.setActionCommand(answer);
  51. group.add(radio);
  52. getContentPane().add(radio);
  53. }
  54. getContentPane().add(btnOk);
  55. pack();
  56. setVisible(true);
  57. }
  58.  
  59. public static void main(String args[]) {
  60. QuizFourBib quiz = new QuizFourBib();
  61. int score = quiz.startQuiz();
  62.  
  63. JOptionPane.showMessageDialog(null,"Your score: "+score,"",JOptionPane.INFORMATION_MESSAGE);
  64. }
  65.  
  66. class Question {
  67. String question="";
  68. String[] answers;
  69. String correctAnswer;
  70.  
  71. public Question(String question, String[] possibleAnswers, String correctAnswer){
  72. this.question = question;
  73. this.answers = possibleAnswers;
  74. this.correctAnswer = correctAnswer;
  75. }
  76.  
  77. public String getQuestion() {
  78. return question;
  79. }
  80.  
  81. public String[] getAnswers() {
  82. return answers;
  83. }
  84.  
  85. public String getCorrectAnswer() {
  86. return correctAnswer;
  87. }
  88. }
  89.  
  90. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 57
Reputation: piers is an unknown quantity at this point 
Solved Threads: 2
piers piers is offline Offline
Junior Poster in Training

Re: creating a JOptionPane quiz

 
0
  #3
Oct 22nd, 2007
wow. you are good at java. I tried the code and what I did was I added lines where you put the questions and was able to add new questions very simply. There are some things in that which I dont quite understand. some of the code you posted I have never seen before. So thnx cus it means I can learn some new stuff.

Also why are you so specific with the stuff u import? I am in the habbit of just ending them with .* cus it covers everything you need then and saves lines of code. is that bad practise?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: creating a JOptionPane quiz

 
0
  #4
Oct 22nd, 2007
Quick addition on that code. Where the answer is compared, a null pointer exception is possible if no answer is chosen when you click ok. To fix it, change to this line
  1. if (group.getSelection()!=null && group.getSelection().getActionCommand().equals(q.getCorrectAnswer())){
Also why are you so specific with the stuff u import? I am in the habbit of just ending them with .* cus it covers everything you need then and saves lines of code. is that bad practise?
If you are using many classes from a package, it can be just fine to use .* in most cases. If however one of your class names appears in multiple packages that you import with .*, like say List which is present in java.awt and java.util then it will be ambiguous and the compiler won't like it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 2182 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC