Results 1 to 12 of 12
Thread: Questionare
- 05-26-2010, 10:27 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
Questionare
hey guyz , i tried to write a program functioning like a questionare where user can choose one answer among multiple choices . however i got below errors:
package java.swing doesnt exist
cant find symbol class ButtonGroup
cant find symbol class JRadioButton
cant find ItemEvent
could u guide me through this ? tnx
Java Code:import java.awt.*; import java.applet.*; import java.swing.*; import java.io.*; public class Questionare extends Applet implements ItemListener { private Label question,a; private ButtonGroup radio; private String ss; private JRadioButton b1,b2,b3,b4; int n=0; public void init() { question=new Label("Whats the sum of 2+2 ="); b1 = new JRadioButton("1", false); b2 = new JRadioButton("5", true); b3 = new JRadioButton("4", true); b4 = new JRadioButton("0", true); radio=new ButtonGroup(); radio.add(b1); radio.add(b2); radio.add(b3); radio.add(b4); b1.addItemListener(this); b2.addItemListener(this); b3.addItemListener(this); b4.addItemListener(this); add(question); add(radio); } public void itemStateChanged (ItemEvent e){ Object source=e.getItemSelectable(); if (source==b1){ ss="wrong"; a.setText(ss); // we call string to show wrong msg// if(ss=="wrong"){ b2.setEnabled(false); b3.setEnabled(false); b4.setEnabled(false); } if(e.getItemChange()==ItemEvent.DESELECTED){ } //correct answer// if(e.getStateChange()==ItemEvent.SELECTED){ ss="Correct"; if(ss=="correct"){ b3.setEnabled(false); n++; // score counter// } } } } }
- 05-26-2010, 11:05 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 9
package java.swing doesnt exist
cant find symbol class ButtonGroup
cant find symbol class ButtonGroup
cant find symbol class JRadioButton
If you want to write everything manualy (like me :D ) do not write javax.swing.*;
instead write:
import javax.swing.JRadioButton;
import javax.swing.JButtonGroup;
- 05-26-2010, 11:20 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
i modified my code based on what u said , still gettin these errors :
javax.swing.AbstractionButton cant be applied to (Questionare)
cant find symbol method add (javax.swing.ButtonGroup)
cant find symbol method get ItemChanged();
code :
Java Code:import java.awt.*; import java.applet.*; import javax.swing.*; import java.awt.event.ItemEvent; import java.io.*; public class Questionare extends Applet implements ItemListener { private Label question,a; private ButtonGroup radio; private String ss; private JRadioButton b1,b2,b3,b4; int n=0; public void init() { question=new Label("Whats the sum of 2+2 ="); b1 = new JRadioButton("1", false); b2 = new JRadioButton("5", true); b3 = new JRadioButton("4", true); b4 = new JRadioButton("0", true); radio=new ButtonGroup(); radio.add(b1); radio.add(b2); radio.add(b3); radio.add(b4); b1.addItemListener(this); b2.addItemListener(this); b3.addItemListener(this); b4.addItemListener(this); add(question); add(radio); } public void itemStateChanged (ItemEvent e){ Object source=e.getItemSelectable(); if (source==b1){ ss="wrong"; a.setText(ss); // we call string to show wrong msg// if(ss=="wrong"){ b2.setEnabled(false); b3.setEnabled(false); b4.setEnabled(false); } if(e.getItemChange()==ItemEvent.DESELECTED){ } //correct answer// if(e.getStateChange()==ItemEvent.SELECTED){ ss="Correct"; if(ss=="correct"){ b3.setEnabled(false); n++; // score counter// } } } } }
- 05-26-2010, 01:14 PM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 9
Java Code:import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JApplet; import javax.swing.JRadioButton; import javax.swing.JPanel; import javax.swing.border.Border; import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class Questionare extends JApplet implements ItemListener { private Label question,a; private ButtonGroup radio; private String ss; private JRadioButton b1,b2,b3,b4; int n=0; class QuestionarePanel extends JPanel { public QuestionarePanel() { Border border = BorderFactory.createTitledBorder("Answers"); setBorder(border); setLayout(new GridLayout(0,1)); radio=new ButtonGroup(); b1 = new JRadioButton("1", false); b2 = new JRadioButton("5", false); b3 = new JRadioButton("4", false); b4 = new JRadioButton("0", true); radio.add(b1); radio.add(b2); radio.add(b3); radio.add(b4); add(b1); add(b2); add(b3); add(b4); } } public void init() { question=new Label("Whats the sum of 2+2?"); add(question,BorderLayout.CENTER); QuestionarePanel panel = new QuestionarePanel(); Container contentPane = getContentPane(); contentPane.add(panel, BorderLayout.SOUTH); } public void itemStateChanged (ItemEvent e){ Object source=e.getItemSelectable(); if(source == b1){ ss="wrong"; a.setText(ss); // we call string to show wrong msg// if(ss == "wrong"){ b2.setEnabled(false); b3.setEnabled(false); b4.setEnabled(false); } if(e.getStateChange() == ItemEvent.DESELECTED){ } //correct answer// if(e.getStateChange()==ItemEvent.SELECTED){ ss="Correct"; if(ss=="correct"){ b3.setEnabled(false); n++; // score counter// } } } } }
Last edited by cselic; 05-26-2010 at 01:23 PM. Reason: adding code tags
- 05-26-2010, 02:05 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
alright , just one question , howcome it doesnt show the msg whether the answer is correct or wrong ? isnt it suppose to the call the string after the answer been chosen ?
- 05-26-2010, 08:26 PM #6
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 9
Java Code:import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JApplet; import javax.swing.JRadioButton; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.border.Border; import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class Questionare extends JApplet { private JLabel question, answer; private ButtonGroup radio; private String correctString; private JRadioButton b1,b2,b3,b4; int n=0; class QuestionarePanel extends JPanel { public QuestionarePanel() { Border border = BorderFactory.createTitledBorder("Answers"); setBorder(border); setLayout(new GridLayout(0,1)); radio=new ButtonGroup(); b1 = new JRadioButton("4", false); b2 = new JRadioButton("2", false); b3 = new JRadioButton("1", false); b4 = new JRadioButton("0", true); radio.add(b1); radio.add(b2); radio.add(b3); radio.add(b4); add(b1); add(b2); add(b3); add(b4); b1.addItemListener(new MyItemListener()); b2.addItemListener(new MyItemListener()); b3.addItemListener(new MyItemListener()); b4.addItemListener(new MyItemListener()); } } public void init() { answer = new JLabel(""); question=new JLabel("Whats the sum of 2+2?"); add(question, BorderLayout.NORTH); add(answer, BorderLayout.CENTER); QuestionarePanel panel = new QuestionarePanel(); Container contentPane = getContentPane(); contentPane.add(panel, BorderLayout.SOUTH); } class MyItemListener implements ItemListener { public void itemStateChanged (ItemEvent e){ correctString = ""; Object source=e.getItemSelectable(); if(source == b1){ correctString = "Correct answer"; } else { correctString = "Answer is not correct"; } answer.setText(correctString); b2.setEnabled(false); b3.setEnabled(false); b4.setEnabled(false); } } }
- 05-27-2010, 05:20 AM #7
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
thanks alot man ! only one thing , lets say im goin to put 9 more questions , is there any way i can shorter the codes ? cause u know it gonna be a very long program if i simply copy n paste the lines.
- 05-27-2010, 05:44 AM #8
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 8
- 05-27-2010, 06:28 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 23
I agreed with you, just giving code away to others in not the exact idea of a forum. Whole idea is sharing the knowledge through an interactive discussion. This is not the way to it.
@cselic, please try to guide if someone looking for a help. Just doing there jobs is not the way to learn new things by OP. Actually not all, but most of the people are not much worried about the code once the solution is found, or someone gives the code.
- 05-27-2010, 04:36 PM #10
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 9
@curmudgeon, @Eranga
The most part you're right. Sorry. :(
I have written it with the intention of trying to make my program different of the required.
In last post I gave him about 70% of solution for program requirements.
30% is up to him.
- 05-27-2010, 04:47 PM #11
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 9
thanks alot man ! only one thing , lets say im goin to put 9 more questions , is there any way i can shorter the codes ? cause u know it gonna be a very long program if i simply copy n paste the lines.
1. write class Question.java
In this class write everything what you are want for your question.
For example I want to have:
a) Text of the question
b) First answer
c) Second answer
d) Third answer
e) something that is telling me which answer is correct.
I'll write just a few lines of this class, one constructor, and rest is up to you:
Java Code:public class Question { String questionText; String answer1; String answer2; String answer3; int correctAnswer; public Question(String Text, String answ1, String answ2, String answ3, int correctAnswer) { this.questionText = Text; this.answer1 = answ1; this.answer2 = answ2; this.answer3 = answ3; this.correctAnswer = correctAnswer; } public String getQuestionText() {} public void addQuestion(Question question) {} public Question getRandomQuestion() { }
Then write class AllQuestions.java
Java Code:import java.util.Vector; import java.util.Random; public class AllQuestions { Random generator; Vector<Question> questions = new Vector<Question>(); public AllQuestions() { }
Use for example vector for all questions:
Vector<Question> questions = new Vector<Question>();
write functions:
public void addQuestion(Question question) {}
public Question getRandomQuestion() { }
and so on.Last edited by cselic; 05-27-2010 at 04:52 PM.
- 05-27-2010, 07:28 PM #12
Member
- Join Date
- Apr 2009
- Posts
- 84
- Rep Power
- 0
First of all , this aint no spoon feeding ! i didnt ask for the whole code ! i had some problems n based on them i made my question up here ~ u can see that i put my own code , i alway do ! this guy is tryin to help in his own way .
You expect pros to come here n say somethin like : " hey go to java.sun.com n search for issue ! " i can do this myself ! i mean some of you guyz just make us beginners more confused rather than helpin to solve our problem . if i was suppose to refer to google or any other reference on the either on the web or books i wouldnt post my question on here ! this forum is meant for people to exchange their knowledge , some like "cselic" try to help while some other just try to increase their no of posts,probably !!
i mean c'mon , this aint no help by just tellin me go search on google or java.sun or other references ! if ure gonna help do it in the right way .:)
i hope u guyz dont take this as offense , just tried to tell what i was suppose to say long time ago .
Bookmarks