Sei sulla pagina 1di 5

package trial;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import java.io.*;
/*
<applet code="CreateCheckBox" width=200 height=200>
</applet>
*/
public class NewClass extends Applet implements ActionListener{
String questionMessage="";
String ScoreMessage="";
String optionMessage[]= new String[5];
String Questions[]= new String[20];
String correctAnswers[]=new String[20];
static int questionCounter=0;
String choices[][]=new String[20][5];
Checkbox checkBox1,checkBox2,checkBox3,checkBox4,checkBox5;
TextArea t1;
Button button1,button2,button3;
String answer[]=new String[20];
@Override
public void init(){
int rand[]=new int[20];
rand=randomnum();
for(int x=0;x<20;x++)
answer[x]="";
String actualquestions[]=new String[30];
for(int c=0;c<30;c++)
actualquestions[c]="";
String actualkey[]=new String[30];
String fileName = "C:/Users/user/Documents/questions.txt";
String fileName2 = "C:/Users/user/Documents/answers.txt";
String line = null;
int totalcount=0;
int totalcount2=0;
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader =new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
if(line.isEmpty())
{
totalcount++;
}
else
actualquestions[totalcount]+=line+"\n";
}
bufferedReader.close();

}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
try {
FileReader fileReader = new FileReader(fileName2);
BufferedReader bufferedReader =new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
int c=(line.indexOf(".")+1);
actualkey[totalcount2]=line.substring(c);
totalcount2++;
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
int i;
for(i=0;i<20;i++)
{
Questions[i]=(i+1)+"."+actualquestions[rand[i]];
correctAnswers[i]=actualkey[rand[i]];
System.out.println(correctAnswers[i]);
}
t1= new TextArea(10, 50);
checkBox1 = new Checkbox("A");
checkBox2 = new Checkbox("B");
checkBox3 = new Checkbox("C");
checkBox4 = new Checkbox("D");
checkBox5 = new Checkbox("E");
button1 = new Button("Next");
button2 = new Button("Submit");
button3=new Button("Start");
add(button1);
add(button2);
add(button3);
add(checkBox1);
add(checkBox2);
add(checkBox3);
add(checkBox4);
add(checkBox5);

t1.setBounds(50, 50, 700, 300);


checkBox1.setBounds(200,500, 90, 20);
checkBox2.setBounds(400,500, 90, 20);
checkBox3.setBounds(600,500, 90, 20);
checkBox4.setBounds(800,500, 90, 20);
checkBox5.setBounds(1000,500, 90, 20);
button1.setBounds(900, 600, 90, 20);
button2.setBounds(1000,600,90,20);
button3.setBounds(800, 600, 90, 20);
setLayout(null);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
}
@Override
public void paint(Graphics g){
g.drawString(ScoreMessage, 800, 300);
}
@Override
public void actionPerformed(ActionEvent ae){
String action = ae.getActionCommand();
if(action.equals("Start"))
{
questionMessage=Questions[questionCounter];
add(t1);
t1.setText(questionMessage);
//questionCounter++;
button3.setEnabled(false);
repaint();
}
if(action.equals("Next"))
{
questionMessage = Questions[questionCounter+1];
t1.setText(questionMessage);
if(checkBox1.getState())
{
answer[questionCounter]="A";
}
if(checkBox2.getState())
{
answer[questionCounter]+="B";
}
if(checkBox3.getState())
{
answer[questionCounter]+="C";
}
if(checkBox4.getState())
{
answer[questionCounter]+="D";
}
if(checkBox5.getState())
{
answer[questionCounter]+="E";
}

questionCounter++;
checkBox1.setState(false);
checkBox2.setState(false);
checkBox3.setState(false);
checkBox4.setState(false);
checkBox5.setState(false);
repaint();
}
if(action.equals("Submit"))
{
if(checkBox1.getState())
{
answer[questionCounter]="A";
}
if(checkBox2.getState())
{
answer[questionCounter]+="B";
}
if(checkBox3.getState())
{
answer[questionCounter]+="C";
}
if(checkBox4.getState())
{
answer[questionCounter]+="D";
}
if(checkBox5.getState())
{
answer[questionCounter]+="E";
}
int scores=0;
for(int z=0;z<20;z++)
{
if(answer[z].equals(correctAnswers[z]))
{
System.out.println("answer right:"+answer[z]+correct
Answers[z]);
scores++;
}
System.out.println("answer:"+answer[z]+correctAnswers[z]
);
}
ScoreMessage=" Score: "+scores;
repaint();
}
}
int[] randomnum()
{
int ans[]=new int[20];
Random randomGenerator = new Random();
int count=0;
while(count<20)

{
int num=randomGenerator.nextInt(29);
boolean flag=true;
for(int i=0;i<count;i++)
{
if(num==ans[i])
{
flag=false;
break;
}
}
if(flag==true)
{
ans[count]=num;
count++;
}
}
return ans;
}
}

Potrebbero piacerti anche