944,193 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4965
  • Java RSS
Oct 22nd, 2007
0

creating a JOptionPane quiz

Expand Post »
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);
}


}
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
piers is offline Offline
68 posts
since Jul 2007
Oct 22nd, 2007
0

Re: creating a JOptionPane quiz

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)
java Syntax (Toggle Plain Text)
  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. }
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 22nd, 2007
0

Re: creating a JOptionPane quiz

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?
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
piers is offline Offline
68 posts
since Jul 2007
Oct 22nd, 2007
0

Re: creating a JOptionPane quiz

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
Java Syntax (Toggle Plain Text)
  1. if (group.getSelection()!=null && group.getSelection().getActionCommand().equals(q.getCorrectAnswer())){
Quote ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Feb 21st, 2010
0
Re: creating a JOptionPane quiz
Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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)
java Syntax (Toggle Plain Text)
  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. }
Could you comment on everything how it works? Cause this will be usefull as a rookie java learner, so I can teach how this code work
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Khranashil is offline Offline
2 posts
since Feb 2010
Feb 21st, 2010
0
Re: creating a JOptionPane quiz
Click to Expand / Collapse  Quote originally posted by Khranashil ...
Could you comment on everything how it works? Cause this will be usefull as a rookie java learner, so I can teach how this code work
That example is self-explanatory(well written names of variables and methods, easy to follow). If you have problems to follow it you better ask specific question instead of making general query...
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 875
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Feb 21st, 2010
0
Re: creating a JOptionPane quiz
Well I don't understand the code at all So that's why I asked about comment the code like this :

public class QuizFourBib extends JDialog { //*This is a class, with a extended JDialog
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Khranashil is offline Offline
2 posts
since Feb 2010
Feb 21st, 2010
0
Re: creating a JOptionPane quiz
If you do not understand code at all then perhaps you should start learning from beginning and leave this till the time you will be able to follow action flow of well written code...
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 875
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: table is not displaying.
Next Thread in Java Forum Timeline: sets and gets





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC