-2

I am having some problem in my quiz game. i am using netbeans. in my game i have two classes 1st is my driver class ie Quiz and second is non driver class ie RadioQuestion. i am posting my code for both the classes. i am having two errors in my RadioQuetion class.

my error message in Quiz.java is "cannot find symbol class: RadioQuestion location: class Quiz.quiz" and in RadioQuestion is "total is not public in Quiz.quiz; cannot be accessed from outside package" and "wrong is not public in Quiz.quiz; cannot be accessed from outside package" and one warning is "implements: java.awt.event.ActionListeneractionPerformed(ActionEvent e)".

Quiz.quiz class

package Quiz;

import java.awt.CardLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Quiz extends JFrame{
    JPanel p=new JPanel();
    CardLayout cards=new CardLayout();
    int numQs;
    int wrongs=0;
    int total=0;
    //RadioQuestion[8] questions=new RadioQuestion();

    String[][] answers={
        {"a","b","c","d"},
        {"e","f","g","h"},
        {"i","j","k","l"},
        {"m","n","o","p"},
        {"True","False"},
        {"True","False"},
        {"4","5","6","7"},
        {"q","r","s","t"},
    };

    RadioQuestion questions[]={

        new RadioQuestion("what",answers[0],1,this),
        new RadioQuestion("what",answers[1],3,this),
        new RadioQuestion("what",answers[2],0,this),
        new RadioQuestion("what",answers[3],1,this),
        new RadioQuestion("what",answers[4],1,this),
        new RadioQuestion("what",answers[5],0,this),
        new RadioQuestion("what",answers[6],0,this),
        new RadioQuestion("what",answers[7],2,this),
        new RadioQuestion("what",answers[8],1,this),
    };
    public static void main(String[] args) {
        //line missing
        //line missing
        //line missing
     new Quiz();


    }
        public Quiz(){
            super("quiz game");
            setResizable(true);
            setSize(500,400);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            p.setLayout(cards);

                numQs=questions.length;
                for(int i=0;i<numQs;i++){
                    p.add(questions[i],"q"+i);
                }
                    Random r=new Random();
                    int i=r.nextInt(numQs);
                    cards.show(p,"q"+i);


            add(p);
            setVisible(true);


        }

        public void next(){
        if((total-wrongs)==numQs){showSummary();}
        else{Random r=new Random();
        boolean found=false;
        int i=0;
        while(!found){
        i=r.nextInt(numQs);
        if(!questions[1].used)
        {
            found=true;
        }}
        cards.show(p,"q"+i);
        }
        }

        public void showSummary(){
        JOptionPane.showMessageDialog(null,"all done your result"+"\nincorrect: \t"+wrongs+"\n correct ans: \t"+(total-wrongs)+"\n avg inc ans per ques: \t"+((float)wrongs/numQs)+"\n percentage correct: \t\t"+(int)(((float)(total-wrongs)/total)*100)+ "%");
        System.exit(0);











        }
}


RadioQuestion class

import Quiz.Quiz;
   import javax.swing.JButton;
    import javax.swing.JRadioButton;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.ButtonGroup;
    //import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.BoxLayout;

    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;

    public class RadioQuestion extends JPanel implements ActionListener {
       // private static int EXIT_ON_CLOSE;

        //RadioQuestion=new RadioQuestion(String q, String[] options, int ans, Quiz quiz);
        int correctAns;

        Quiz quiz;
        int selected;
        boolean used;

        //question
        JPanel qPanel=new JPanel();
        //answers
        JPanel aPanel=new JPanel();
        JRadioButton[] responses;
        ButtonGroup group=new ButtonGroup();
        //bottom
        JPanel botPanel=new JPanel();
        JButton next=new JButton("next");

        JButton finish=new JButton("finish");

        /*public static void main(String args[]){
        JFrame frame=new JFrame("radio button test");
        frame.setResizable(true);
                frame.setSize(500,400);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String[] answers={"wrong1","right","wrong2"};
        frame.add(new RadioQuestion("whats right",answers,1));
        frame.setVisible(true);
    }*/
        public RadioQuestion(String q, String[] options, int ans, Quiz quiz){
            //dought
            this.quiz=quiz;
            //dought
        setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        correctAns=ans;
        //question
        qPanel.add(new JLabel(q));
        //answer
        responses=new JRadioButton[options.length];
        for(int i=0;i<options.length;i++){
        responses[i]=new JRadioButton(options[i]);
        responses[i].addActionListener(this);
        group.add(responses[i]);
        aPanel.add(responses[i]);
        }
        add(aPanel);
        //bottom
        next.addActionListener(this);
        finish.addActionListener(this);
        botPanel.add(next);
        botPanel.add(finish);
        add(botPanel);
        }
////////////////////////////////////////////////////////////////////////////////
        public void actionPerformed(ActionEvent e){
        Object src=e.getSource();
        //next button
        if(src.equals(next)){
        showResult();
        if(selected==correctAns){
        used=true;
        quiz.next();}}
        //finish button
        if(src.equals(finish)){
        quiz.showSummary();}

        //radio buttons
        for(int i=0;i<responses.length;i++){
        if(src==responses[i]){
        selected=i;}
        }}
        public void showResult(){
        String text=responses[selected].getText();
        quiz.total++;
        if(selected==correctAns){
        JOptionPane.showMessageDialog(null,text+"is correct\nwell done");
        }else{
        quiz.wrongs++;
        JOptionPane.showMessageDialog(null,text+" is wrong\nSorry:(");}
        }












        //public stataic void main(String[] arg);

    }
usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • Try to reduce the amount of code. It should be like two classes with two or three methods, each containing two lines. That helps to find the problem, for you and for us. – usr1234567 Jun 08 '14 at 16:39

1 Answers1

0

It seems that Quiz class is defined in package Quiz and RadioQuestion resides in default package. Two member of Quiz class wrongs and total are defined without explicit modifier and that makes them package-private. These members can be accessed only within Quiz package. That is what the errors indicate. See Controlling Access to Members of a Class for more details.

You should define these members as private and create getters and setters. Ie:

private int wrongs=0;
private int total=0;

public int getWrongs() {
    return wrongs;
}
public void setWrongs(int wrongs) {
    this.wrongs = wrongs;
}
public int getTotal() {
    return total;
}
public void setTotal(int total) {
    this.total = total;
}
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • Also see [Encapsulation](http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29) – tenorsax Jun 08 '14 at 17:04