السلام عليكم
ممكن شرح وافي
لهذا الكود يتكون من 3 كلاسس
وجزاكم الله خير
quiz1.java:
_________________
public class Quiz1
{
//==== True False Variables ========//
int s1=6;
int[] checkRes=new int[s1];
String[] Qtf=new String[s1]; // Questions
String sp="........................................................................";
//============================================//
//=========== Initial Function ===============//
//============================================//
public Quiz1() {
//=====================
sp=sp+sp;
//========== Add true false Questions ========//
Qtf[0]="1- The system bus carries bits between the microprocessor and memory and input/output devices"+sp; checkRes[0]=1;
Qtf[1]="2- Accumulator is an 8-bit register which is part of the ALU"+sp; checkRes[1]=1;
Qtf[2]="3- The following Mnemonics MOV A,B has 1 byte"+sp; checkRes[2]=1;
Qtf[3]="4- The following Mnemonics STA 5050H has 2 bytes"+sp; checkRes[3]=0;
Qtf[4]="5- The following Mnemonics IN 02H has 3 byte"+sp; checkRes[4]=0;
Qtf[5]="6- The following Mnemonics HLT has 1 byte"+sp; checkRes[5]=1;
}
}
________________
quiz2.java:
_______________
public class Quiz2
{
//==== Choice Variables ========//
int s2=3;
int[] chRes=new int[s2];
String[] Qch=new String[s2]; // Questions
String spch="........................................................................";
//==================================//
//=========== Initial Function ===============//
//============================================//
Quiz2() {
spch=spch+spch;
//========== Add Choice Questions ========//
Qch[0]="1- A microprocessor flag is simply a ..........."+spch; chRes[0]=3;
Qch[1]="2- What is the Hex code of MOV A,C ..........."+spch; chRes[1]=2;
Qch[2]="3- What is the Hex code of HLT .................."+spch; chRes[2]=3;
}
}
_____________
exams.java:
________
import java.awt.*;
import java.applet.*;
public class Exams extends Applet {
// to give variables to the classes
Quiz1 Q1=new Quiz1();
Quiz2 Q2=new Quiz2();
//==== True False Variables ========//
int[] checkRes=new int[Q1.s1];
CheckboxGroup[] ck=new CheckboxGroup[Q1.s1];
String[] Qtf=new String[Q1.s1]; // Questions
Label[] lb=new Label[Q1.s1];
//==== Choice Variables ========//
Choice cx1,cx2,cx3,cx4,cx5,cx6;
Choice[] ch=new Choice[Q2.s2];
Label[] lbch=new Label[Q2.s2];
Label[] lbchAns=new Label[Q2.s2];
//========== variables ==============//
String Res1=" ",Res2=" ",Res3=" ",Res=" ";
int R1=0,R2=0,R3=0;
Button Bt,Bt1,Bt2,Bt3;
Label Lq1,Lq2,Lq3;
Label lbRes11,lbRes22,lbRes33,lbRes;
Label lbRes1,lbRes2,lbRes3;
//A Label object is a component for placing text in a container.
//A label displays a single line of read-only text.
//The text can be changed by the application, but a user cannot edit it directly
Label LBA=new Label();
Label LBA1=new Label();
Label LBA2=new Label();
Label ALI=new Label();
Label SPA1=new Label();
Label SPA2=new Label();
String sa=" ";
//============ Quistion colors ===========
Color col1=new Color(0,0,255); // blue
Color col2=new Color(0,0,255);
Color col3=new Color(0,0,255);
Color col4=new Color(0,0,255);
//================== Quistion Head Color =============//
Color col5=new Color(178,34,34);
Color col6=new Color(238,221,130);
//================== Gray backcolor ans ch and text =============//
Color cl=new Color(190,190,190);
//=========== Initial Function ===============//
public void init() {
//============== Choice 1 ==========//
cx1=new Choice();
cx1.addItem(" -------- Select Answer --------");
cx1.addItem("a. Register");
cx1.addItem("b. Decorder");
cx1.addItem("c. Flip-flop");
cx1.addItem("d.None");
cx1.setBounds(280,240,100,20);
add(cx1);
cx1.setVisible(false);
//============== Choice 2 ==========//
cx2=new Choice();
cx2.addItem(" -------- Select Answer --------");
cx2.addItem("a.80");
cx2.addItem("b.79");
cx2.addItem("c.2F");
cx2.addItem("d.None");
cx2.setBounds(280,240,100,20);
add(cx2);
cx2.setVisible(false);
//============== Choice 3 ==========//
cx3=new Choice();
cx3.addItem(" -------- Select Answer --------");
cx3.addItem("a.47");
cx3.addItem("b.D3");
cx3.addItem("c.76");
cx3.addItem("d.None");
cx3.setBounds(280,240,100,20);
add(cx3);
cx3.setVisible(false);
//=====================
add(LBA);
LBA.setText(" "+sa+sa );
LBA1.setText(" "+sa+sa+sa );
LBA2.setText(" "+sa+sa+sa );
SPA1.setText(" "+sa+sa );
Color col11=new Color(255,248,220);
SPA1.setBackground(col11);
SPA2.setText(" "+sa+sa );
Color col111=new Color(255,248,220);
SPA2.setBackground(col111);
ALI.setText(" ~~ EXAM ~~"+sa+sa );
ALI.setFont(new Font("TimesRoman", Font.BOLD, 12));
Color col22=new Color(173,255,47);
ALI.setBackground(col22);
Color col222=new Color(34,139,34);
ALI.setForeground(col222);
//========== Add true false Questions ========//
Lq1=new Label();
add(Lq1);
Lq1.setText("QUESTION No. (1): Select (T) for correct statment and (F) for incorrect" +Q1.sp);
Lq1.setFont(new Font("Dialog", Font.PLAIN, 20));
Lq1.setForeground(col5);
Lq1.setBackground(col6);
//===== add CheckboxGroups =======//
int cs=0;
setLayout(new FlowLayout(0,0,0));
for(int i=0;i<Q1.s1;i++)
{
lb=new Label();
lb.setBackground(Color.white);
add(lb);
lb.setText(Q1.Qtf);
lb.setFont(new Font("Dialog", Font.PLAIN, 14));
lb.setBounds(100,10*(i+1),700,20);
if(cs==0)
{
cs=1;
lb.setForeground(col1);
}
else if(cs==1)
{
cs=2;
lb.setForeground(col2);
}
else if(cs==2)
{
cs=3;
lb.setForeground(col3);
}
else if(cs==3)
{
cs=0;
lb.setForeground(col4);
}
ck=new CheckboxGroup();
add(new Checkbox("T",ck,false));
add(new Checkbox("F",ck,false));
}
add(LBA);
setLayout(new FlowLayout(0,0,0));
lbRes33=new Label();
add(lbRes33);
lbRes33.setText(" ");
Bt1 = new Button(" Submit ");
add(Bt1);
lbRes1=new Label();
add(lbRes1);
lbRes1.setBackground(Color.orange);
lbRes1.setText(" ");
//========== Add Choice Questions ========//
add(SPA1);
add(SPA2);
Lq2=new Label();
add(Lq2);
Lq2.setText("QUESTION No. (2): Choose the best answer "+Q2.spch);
Lq2.setFont(new Font("Dialog", Font.PLAIN, 20));
Lq2.setForeground(col5);
Lq2.setBackground(col6);
//===== add Choice tools =======//
cs=0;
setLayout(new FlowLayout(0,0,0));
for(int i=0;i<Q2.s2;i++)
{
lbch=new Label();
add(lbch);
lbch.setBackground(Color.white);
lbch.setText(Q2.Qch);
lbch.setFont(new Font("Dialog", Font.PLAIN, 14));
lbch.setBounds(100,10*(i+1),700,20);
if(cs==0)
{
cs=1;
lbch.setForeground(col1);
}
else if(cs==1)
{
cs=2;
lbch.setForeground(col2);
}
else if(cs==2)
{
cs=3;
lbch.setForeground(col3);
}
else if(cs==3)
{
cs=0;
lbch.setForeground(col4);
}
ch=new Choice();
if(i<1)
copyChoice(ch,cx1);
else if(i<2)
copyChoice(ch,cx2);
else
copyChoice(ch,cx3);
add(ch);
setLayout(new FlowLayout(0,0,0));
lbchAns=new Label();
add(lbchAns);
lbchAns.setBackground(cl);
lbchAns.setForeground(Color.red);
lbchAns.setText(" "+sa+sa+sa);
lbchAns.setFont(new Font("Dialog", Font.PLAIN, 14));
}
//=======================
add(LBA);
setLayout(new FlowLayout(0,0,0));
lbRes33=new Label();
add(lbRes33);
lbRes33.setText(" ");
Bt2 = new Button(" Submit ");
add(Bt2);
lbRes2=new Label();
add(lbRes2);
lbRes2.setBackground(Color.orange);
lbRes2.setText(" ");
//=======================
//=======================
setLayout(new FlowLayout(0,0,0));
// add(LBA2);
lbRes22=new Label();
add(lbRes22);
lbRes22.setText(sa+sa+sa+sa+sa+sa+sa+sa+sa+sa);
Bt = new Button(" Total Result");
add(Bt);
Bt.setBounds(180,100,100,30);
lbRes=new Label();
add(lbRes);
Color cc6=new Color(188,143,143);
lbRes.setBackground(cc6);
lbRes.setText(" ");
lbRes.setFont(new Font("Dialog", Font.PLAIN, 18));
lbRes11=new Label();
add(lbRes11);
lbRes11.setText(sa+sa+sa+sa+sa+sa+sa);
lbRes33=new Label();
add(lbRes33);
lbRes33.setText(sa);
add(ALI);
}
//============ Copy Choice Function ==========//
public void copyChoice(Choice cxk,Choice cx)
{
int i=0;
cxk.removeAll();
while(i< cx.getItemCount())
{
cxk.addItem(cx.getItem(i));
i++;
}
}
//============ Action Function ===============//
public boolean action (Event evt, Object arg)
{
if(evt.target instanceof Button)
{
Res1=" ";Res2=" ";Res3=" ";Res=" ";
if (evt.target == Bt1)
{
lbRes1.setText("You must answer all of the questions");
if(CheckTF()==1)
{
SeeAns1();
lbRes1.setText(Res1);
}
}
if (evt.target == Bt2)
{
if(CheckCh()==1)
{
SeeAns2();
lbRes2.setText(Res2);
}else
lbRes2.setText("You must answer all of the questions");
}
if (evt.target == Bt)
{
int xr=R1+R2+R3 , tt=Q1.s1+Q2.s2;
lbRes.setText(" You scored: "+xr+" (out of a possible "+ tt+" )");
}
repaint();
}
return true;
}
//=========== Main Paint Function ============//
public void paint(Graphics g){
g.drawString("", 10, 30);
}
//=========== SeeAns1 Function ============//
public void SeeAns1()
{
int i, Rs1=0;
for(i=0;i<Q1.s1;i++)
{
Checkbox cx= (Checkbox) ck.getSelectedCheckbox();
String x1;
x1=cx.getLabel();
if(x1=="T" && Q1.checkRes==1)
{
Rs1++;
cx.setBackground(Color.green);
}
else if(x1=="F" && Q1.checkRes==0)
{
Rs1++;
cx.setBackground(Color.green);
}
else
cx.setBackground(Color.red);
}
Res1=Res1 + " You got: ( "+Rs1 + " ) out of ( "+Q1.s1+" )";
R1=Rs1;
}
//=========== SeeAns2 Function ============//
public void SeeAns2()
{
int Rs2=0, i;
for(i=0;i<Q2.s2;i++)
{
int xi= ch.getSelectedIndex();
if(xi==Q2.chRes)
Rs2++;
else
lbchAns.setText(ch.getSelectedItem());
}
Res2=Res2 + " You got : ( "+Rs2 + " ) out of ( "+Q2.s2+" )";
R2=Rs2;
for(i=0;i<Q2.s2;i++)
{
ch.select(Q2.chRes);
}
}
//=========== SeeAns3 Function ============//
//======== Check True False Function =========//
public int CheckTF()
{
int i;
for(i=0;i<Q1.s1;i++)
{
Checkbox cx= (Checkbox) ck.getCurrent(); //ck.getSelectedCheckbox();
String x1;
x1=cx.getLabel();
if( cx.isValid() == false)// cx.isValid();
{
cx.setBackground(Color.yellow);
return 0;
}
}
return 1;
}
//=========== CheckCh Function ============//
public int CheckCh()
{
int i;
for(i=0;i<Q2.s2;i++)
{
if( ch.getSelectedIndex()==0 )
return 0;
}
return 1;
}
//=========== CheckText Function ============//
}