java - error in quiz game using swings -
i having problem in quiz game. using netbeans. in game have 2 classes 1st driver class ie quiz , second non driver class ie radioquestion. posting code both classes. having 2 errors in radioquetion class.
my error message in quiz.java "cannot find symbol class: radioquestion location: class quiz.quiz" , in radioquestion "total not public in quiz.quiz; cannot accessed outside package" , "wrong not public in quiz.quiz; cannot accessed outside package" , 1 warning "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 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+" wrong\nsorry:(");} } //public stataic void main(string[] arg); }
it seems quiz
class defined in package quiz
, radioquestion
resides in default package. 2 member of quiz
class wrongs
, total
defined without explicit modifier , makes them package-private. these members can accessed within quiz
package. errors indicate. see controlling access members of class more details.
you should define these members private
, create getters , 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; }
Comments
Post a Comment