Home | J2ME Yours page | Favorite Links | Contact Me | objques | Canvas Demo | Fruit selection | Commands | Gauge NI | Gauge Response | Pallet | Pal Ex Mid | Slides Text
Prasad's J2ME programs

Quiz

Enter subhead content here

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import java.util.*;

public class ObjQuesMid extends MIDlet implements CommandListener{
private Display d;
private Form f1,f2;
private ChoiceGroup[] objChoice;
private String[]  quesStr;
private Command cexit,csubmit,cback;
private int userChoi[],actChoi[],correctAns;
private String[] queschoi;
private TextField result,corrected;
private String correctStr;

public  ObjQuesMid(){
d =  Display.getDisplay(this);

f1 = new Form("Objective Question Paper");
f2 = new Form("Result Sheet");

cexit = new Command("EXIT",Command.EXIT,1);
csubmit = new Command("SUBMIT",Command.ITEM,1);
cback = new Command("BACK",Command.ITEM,1);
actChoi = new int[]{0,1,2,3,0,1,2,3,0,1};
queschoi = new String[]{"a. First","b. Second","c. Third","d. Fourth"};
objChoice = new ChoiceGroup[10];
quesStr   = new String[10];

result = new TextField("Correct:","",10,TextField.ANY);
corrected = new TextField("Corrected ","",200,TextField.ANY);
for(int i = 0; i<10; i++){
quesStr[i] = ""+(i+1)+". This is question number"+(i+1);

//for(int i = 0; i<10; i++)
objChoice[i] = new ChoiceGroup("",Choice.EXCLUSIVE,queschoi,null);

//for(int i = 0; i<10; i++){
f1.append(quesStr[i]);
f1.append(objChoice[i]);
}

f1.addCommand(cexit);
f1.addCommand(csubmit);
f1.setCommandListener(this);
f2.append(result);
f2.append(corrected);
f2.addCommand(cback);
f2.setCommandListener(this);

}

public void startApp(){
d.setCurrent(f1);
}

public void pauseApp(){}
public void destroyApp(boolean unconditional){}

public void commandAction(Command c,Displayable s){
correctAns = 0;
correctStr = "Correct answers are";

if( c == cexit)
notifyDestroyed();
else if( c == csubmit){
userChoi = new int[10];
for(int i = 0; i<10; i++){
userChoi[i] = objChoice[i].getSelectedIndex();
if(userChoi[i] == actChoi[i])
correctAns++;
else
correctStr = correctStr+" for "+(i+1)+" is "+ objChoice[i].getString(actChoi[i]);
//System.out.println("correctly Answered are "+correctAns);
//System.out.println(correctStr);
corrected.setString(correctStr);
//System.out.println("Hello");
result.setString(""+correctAns);
d.setCurrent(f2);
}
else if(c == cback)
d.setCurrent(f1);
}//end of commandAction
}//end of class


Enter supporting content here

"Striving for success without hard work is like trying to harvest where you haven't planted."