Results 1 to 12 of 12

Thread: Questionare

  1. #1
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Question 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//
    			
    			
    		}
    
    		}
    }
    }
    }

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    package java.swing doesnt exist
    import javax.swing.*;

    cant find symbol class ButtonGroup
    import java.awt.event.ItemEvent;

    cant find symbol class ButtonGroup

    cant find symbol class JRadioButton
    They already have been defined with: import javax.swing.*;

    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;

  3. #3
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    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//
    			
    			
    		}
    
    		}
    }
    }
    }

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    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 02:23 PM. Reason: adding code tags

  5. #5
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    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 ?

  6. #6
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    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);
    		}
    	}
    }

  7. #7
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default

    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.

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by pinkdreammsss View Post
    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.
    Rather than ask someone for more code, please feel free to use a little initiative. Otherwise you're just cheating yourself out of a valuable learning experience.

    to cselic: spoon-feeding just teaches folks to beg. Case in point above.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by curmudgeon View Post
    Rather than ask someone for more code, please feel free to use a little initiative. Otherwise you're just cheating yourself out of a valuable learning experience.

    to cselic: spoon-feeding just teaches folks to beg. Case in point above.
    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.

  10. #10
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    @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.

  11. #11
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    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.
    I will give you my opinion:

    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() { }
    in that class you should write getters, and setters, and few other functions if you'll need them.

    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 05:52 PM.

  12. #12
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Arrow

    Quote Originally Posted by curmudgeon View Post
    Rather than ask someone for more code, please feel free to use a little initiative. Otherwise you're just cheating yourself out of a valuable learning experience.

    to cselic: spoon-feeding just teaches folks to beg. Case in point above.

    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 .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •