📄 exam.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.io.*;
import java.util.zip.*;
import java.net.*;
public class Exam extends Applet {
String sQuestionSource;
MarkForm frMark;
boolean isStandalone = false;
public TextArea ta = new TextArea(15,55);
Panel pOptButtons = new Panel();
Panel pCmdButtons = new Panel();
Panel pSouth = new Panel();
Button cmdForward = new Button("Forward");
Button cmdBack = new Button("Back");
Button cmdMark = new Button("Mark");
TimerLabel tl = new TimerLabel();
FlowLayout flowLayout1 = new FlowLayout();
BorderLayout borderLayout1 = new BorderLayout();
GridLayout layGridSouth = new GridLayout(2,1);
GridLayout gridLayout1 = new GridLayout(6,30);
Vector v = new Vector();
boolean bMarkDisplayed=false;
public QuestManager qm ;
//Construct the applet
public Exam() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace() ;
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(borderLayout1);
String sWelcome = "\nThis applet simulates taking the Sun Certified Java Programmers Exam. \n";
sWelcome += "When you click Forward for the first time the first question will be displayed";
sWelcome += " and the counter will start.";
ta.setText(sWelcome);
add("North",ta);
pCmdButtons.add(cmdForward);
pCmdButtons.add(cmdBack);
pCmdButtons.add(cmdMark);
pCmdButtons.add(tl);
pCmdButtons.setLayout(flowLayout1);
pCmdButtons.setBackground(Color.pink);
add("South",pCmdButtons);
pOptButtons.setBackground(Color.green);
pOptButtons.setLayout(gridLayout1);
add("Center",pOptButtons);
go();
}
public void go(){
FileOpener fo = new FileOpener();
sQuestionSource= getParameter("QuestionSource");
v =fo.openFile(sQuestionSource);
//v =fo.openFile("http://www.software.u-net.com/applets/Exam/JXam.txt");
//For an applet this should be put in a parameter
// v= fo.openFile("file:///JBuilder2/myprojects/Xam/JXam.txt");
// v= fo.openFile("file:///jdk1.1/myprogs/Exam/JXam.txt");
qm = new QuestManager(v,this);
//q.getForward();
}
public boolean handleEvent(Event event){
//String sArg="";
if((event.id) == Event.WINDOW_DESTROY) {
String s= this.getName();
}
if((event.id) ==Event.ACTION_EVENT){
if(event.target instanceof Checkbox){
//System.out.println("Target is" + event.target);
} else
{
String sArg = (String) event.arg;
if(sArg.equals("Forward")){
qm.getForward();
tl.StartCount();
}
if(sArg.equals("Back")){
qm.getBack();
}
if(sArg.equals("Mark")){
//if(bMarkDisplayed==false){
if(frMark==null){
frMark = new MarkForm(this);
tl.StopCount();
}
else{
frMark=null;
frMark = new MarkForm(this);
tl.StopCount();
}
// bMarkDisplayed=true;
// }
}
}
}
return true;
}
}
class MarkForm extends Frame{
int iHeight=700;
int iWidth=1000;
Exam m_Xam;
Panel pAnswer= new Panel();
//GridLayout gl = new GridLayout();
MarkForm(){
go();
}
MarkForm(Exam arg){
m_Xam=arg;
go();
}
public String bArrayToString(boolean[] bAns){
String sAns="";
for(int j=0;j< bAns.length; j++){
if(bAns[j]==true){
//increment to account for 0th start
sAns=sAns+" "+(j+1);
}
}
return sAns;
}
public void go(){
getMarks();
setVisible(true);
setBackground(Color.green);
this.setTitle("Marks");
}
public void getMarks(){
Panel pnlMarks = new Panel();
this.setLayout(new BorderLayout());
Panel pnlNorth= new Panel();
pnlNorth.setLayout(new GridLayout(3,1));
GridLayout gl = new GridLayout();
m_Xam.qm.getForward();
m_Xam.qm.getBack();
int iRightAnsCount= m_Xam.qm.getRightAnsCount();
int iAttemptedAnsCount = m_Xam.qm.getAttemptedCount();
float iAnsRows = iAttemptedAnsCount - iRightAnsCount;
float fPercentMark =0;
//Let's not try to divide by zero
if(iAttemptedAnsCount >0){
fPercentMark= ((float)iRightAnsCount) /((float) iAttemptedAnsCount) ;
}
int iPercentMark = (int) (fPercentMark *100);
//iAnsRows=40;
if(iAnsRows > 25){
//pnlMarks.setLayout(new GridLayout(25,2));
gl.setRows((int) iAnsRows/2);
gl.setColumns(2);
pnlMarks.setLayout(gl);
setSize(iWidth, iHeight);
}else{
this.setSize(iWidth/2,iHeight);
pnlMarks.setLayout(new GridLayout(25,1));
}
//setSize(iWidth, iHeight);
Label lblAttemptedAnsCount = new Label("You attempted " + iAttemptedAnsCount + " Questions");
String sRightAns;
if(iRightAnsCount ==0){
sRightAns = " " + iRightAnsCount + " answers were correct, a mark of "+ iPercentMark +" %";
} else if(iRightAnsCount==1){
sRightAns = " " + iRightAnsCount + " answer was correct, a mark of "+ iPercentMark +" %";
}else{
sRightAns = " " + iRightAnsCount + " answers were correct, a mark of "+ iPercentMark +" %";
}
Label lblRightAnsCount = new Label(sRightAns);
pnlNorth.add(lblAttemptedAnsCount);
pnlNorth.add(lblRightAnsCount);
System.out.println("iPercentMark "+iPercentMark);
if(iPercentMark < 100)
pnlNorth.add(new Label(" Following are my answers that didn't match yours, See HTML version for explanations"));
pnlNorth.setBackground(Color.pink);
add(pnlNorth,"North");
pnlMarks.setBackground(Color.green);
this.remove(pnlMarks);
String s = "";
int iQuestCount = m_Xam.qm.getQuestCount();
for (int i=0;i < iQuestCount; i++) {
boolean bAnsRight= m_Xam.qm.getAnsweredRight(i);
boolean bAttempted=m_Xam.qm.getAtttempted(i);
//If an answer was given but was incorrect
if((bAnsRight ==false) && (bAttempted==true)) {
// if(true){
boolean bAns[]= m_Xam.qm.getRightAns(i);
s= (i+1)+") Option(s)"+bArrayToString(bAns);
TextField t = new TextField(s);
//t.setText(s);
pnlMarks.add(t);
}//End if
}//End for
add(pnlMarks);
}
public boolean handleEvent(Event event){
if((event.id) == Event.WINDOW_DESTROY) {
this.dispose();
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -