Quiz Skeleton that reads questiions from a file
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class newquiz extends Applet
{String fileToRead = "quiz.txt"; // this is where we set file to read
StringBuffer strBuff;
Font f= new Font("Monotype Corsiva",Font.BOLD,24); //title
Label title = new Label(" ");
Label question = new Label(" ") ;
Label result1= new Label(" ") ;
Label result2= new Label(" ") ;
Label result3= new Label(" ") ;
Label response = new Label(" ");
String[] questions = new String[25];
String[] ans1 = new String[25];
String[] ans2 = new String[25];
String[] ans3 = new String[25];
String[] ans4 = new String[25];
String[] ca = new String[25];
int ct,answered;
double right,wrong; // otherwise we get integer division
CheckboxGroup trivia = new CheckboxGroup();
Button done = new Button("Push for next question");
Checkbox a1 = new Checkbox(" ",trivia,false); // objects that are
Checkbox a2 = new Checkbox(" ",trivia,false); // constructed
Checkbox a3 = new Checkbox(" ",trivia,false ); // will be added
Checkbox a4 = new Checkbox(" ",trivia,false);
Checkbox a5 = new Checkbox("",trivia,false); // non-showing box used to clear others
int next; // whether next question has been selected yet
int printedtime = 0;
public void init()
{ setBackground ( Color.white );
resize(600,300);
String prHtml = this.getParameter("fileToRead");
if (prHtml != null) fileToRead = new String(prHtml);
int ct3 = 0;
String an1,an2,an3,an4,c,q;
setLayout(new GridLayout(12,1)); // put objects right underneath each other
add(question);
URL url = null;
try{
url = new URL(getCodeBase(), fileToRead);
}
catch(MalformedURLException e){}
try{
InputStream in = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
strBuff = new StringBuffer();
while ((q = br.readLine()) != null) {
an1 = br.readLine();
an2 = br.readLine();
an3 = br.readLine();
an4 = br.readLine();
c = br.readLine();
questions[ct3] = q;
ans1[ct3] = an1;
ans2[ct3] = an2;
ans3[ct3] = an3;
ans4[ct3] = an4;
ca[ct3] = c;
ct3++;
}
in.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
right =0; wrong = 0;
ct =0; // which question to start with
add(a1);
add(a2);
add(a3);
add(a4);
add(response); // will print out right or wrong
add(done); // push a key for question
add(result1); // for responding to how they did
add(result2);
add(result3);
result2.setText("");
result3.setText("");
result1.setText("");
question.setText(ct + 1 +". "+questions[ct]);
a1.setLabel(ans1[ct]); // next set of answers
a2.setLabel(ans2[ct]); // put into already constructed objects
a3.setLabel(ans3[ct]);
a4.setLabel(ans4[ct]);
next =1;
}
public boolean action(Event event, Object obj)
{Object oTarget = event.target;
Checkbox choice ; // had to make choice global to function
if (oTarget instanceof Checkbox && next ==1)
{ next =0; // won't take another selection until new question button
choice = trivia.getCurrent();
if (choice == a1 && (ca[ct].compareTo("a") ==0))
{ right++; response.setText("Correct");
}
else
if (choice == a2 && (ca[ct].compareTo("b") ==0))
{ right++; response.setText("Correct");
}
else
if (choice == a3 && (ca[ct].compareTo("c") ==0))
{ right++; response.setText("Correct");
}
else
if (choice == a4 && (ca[ct].compareTo("d") ==0))
{ right++; response.setText("Correct");
}
else
{ wrong++;
response.setText("Sorry, wrong answer");
}
if (printedtime ==0 && ct == 4) // give score - done with quiz
{ printedtime =1; // don't print again
next = 0; // don't let them select button again
if (wrong ==0)
{
result3.setText("All Correct");
}
else
{
result2.setText("Your final score is "+(right/(right+wrong)*100));
}
} // printed time == 0 results not printed yet
} // if checkbox
else
if (oTarget instanceof Button && next == 0) // don't let them go on until they have answered last question
{choice = trivia.getCurrent(); // need this because of scope
next =1; // sets it so we can answer next question
if (ct < 5)
{
ct = ct+1;
a1.setLabel(ans1[ct]);
a2.setLabel(ans2[ct]);
a3.setLabel(ans3[ct]);
a4.setLabel(ans4[ct]);
a5.setState(true); //set non-showing button to true to clear
// a1.setState(false) didn't clear it
response.setText("");
question.setText(ct + 1 +". "+questions[ct]);
} // if cnt < question.length
} // button
return true;
} // function action
}