السلام عليكم و رحمة الله و بركاته
أنا ، بفضل الله أولا و أخيرا ولا حول و لا قوة لي إلا به ، قمت بعمل framework ( لا أعرف ترجمه لها ) للإختبارات
فقط كل ماعليك هو أن تضع الاختبار اللي عاوز الطلاب يمتحنوه في صورة ملف xml ليه شكل معين - الشكل سهل جدا - و بس ، الطالب هيمتحن نفسه و يعرف إجابته لواحده .
أنا عامله open source يعني الكل يقدر يعدل فيه و يطوره ( :P أنا هعمل معاكم زي ما الشركات الكبيره بيعمل مع مجتمع ال open source ) ،،،
المهم الframework ده شغال تمام التمام و أي حد ممكن يستفيد منه ، بس ناقص حاجه واحده بس ، أحطع عداد تنازلي بالوقت الباقي ( دي مهمتكو بقي يا أبطال )
و أدي المصدر :
* In the name of ALLAH, The gracious the merciful
* This is the examframework version 1.0 by mohammed hewedy
* you can use, modify and redistribute it as you like, no copyright or left owned by me
* I hope you can understand it with its little comments
*/
package client;
public class Answer {
//private boolean correct;
private char id;
private String value;
/*
public void setCorrect(boolean correct) {
this.correct = correct;
}
public boolean isCorrect() {
return correct;
}
*/
public void setId(char id) {
this.id = id;
}
public char getId() {
return id;
}
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
/**
* In the name of ALLAH, The gracious the merciful
* This is the examframework version 1.0 by mohammed hewedy
* you can use, modify and redistribute it as you like, no copyright or left owned by me
* I hope you can understand it with its little comments
*/
package client;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import java.util.*;
public class MyHandler extends DefaultHandler {
private static List<Question> list;
// private int questionNo;
private Question q;
private char rightAns = '';
private boolean start = false;
public MyHandler(List<Question> list) {
this.list = list;
}
public void startElement(String nameSpace, String localName, String QName,
Attributes att) throws SAXException {
if (!localName.equals("questions")) {
start = true;
if (localName.equals("question")) {
q = new Question();
q.setNumber(Integer.parseInt(att.getValue(0)));
rightAns = att.getValue(1).charAt(0);
q.setCorrectAnswer(rightAns);
} else {
Answer answer = new Answer();
answer.setId(att.getValue(0).charAt(0));
/*if (rightAns == answer.getId())
answer.setCorrect(true);*/
q.addAnswer(answer);
}
}
}
public void endElement(String nameSapce, String localName,
String QName) throws SAXException {
try {
if (!localName.equals("questions")) {
if (/*q.getAnswers().get( 0).getValue() != null*/ localName.equals("question")) {
list.add(q);
q = null;
}
}
} catch (Exception e) {
// e.printStackTrace();
}
}
public void characters(char[] ch, int start,
int length) throws SAXException {
StringBuilder sb = new StringBuilder();
for (int i = start; i < start + length; i++) {
sb.append(ch);
}
String s = sb.toString();
try {
if (this.start) {
if (q.getValue() == null)
q.setValue(s);
else {
for (int i = 0; i < q.getAnswers().size(); i++) {
if (q.getAnswers().get(i).getValue() == null) {
q.getAnswers().get(i).setValue(s);
break;
}
}
}
}
} catch (Exception e) {
// e.printStackTrace();
}
}
public List<Question> getList() {
return list;
}
public static Question getQuestion (int index){
return list.get(index);
}
}
/**
* In the name of ALLAH, The gracious the merciful
* This is the examframework version 1.0 by mohammed hewedy
* you can use, modify and redistribute it as you like, no copyright or left owned by me
* I hope you can understand it with its little comments
*/
package client;
import java.util.*;
public class Question {
private int number;
private String value;
private List<Answer> list;
private char correctAnswer;
public char getCorrectAnswer (){
return correctAnswer;
}
public void setCorrectAnswer ( char ca){
correctAnswer = ca;
}
public Question() {
list = new ArrayList<Answer>();
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public void addAnswer ( Answer answer ){
list.add(answer);
}
public List<Answer> getAnswers (){
return list;
}
}
/**
* In the name of ALLAH, The gracious the merciful
* This is the examframework version 1.0 by mohammed hewedy
* you can use, modify and redistribute it as you like, no copyright or left owned by me
* I hope you can understand it with its little comments
*/
package client;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Test extends JApplet implements ActionListener {
private JButton nxtBtn = new JButton("next");
private JButton prvBtn = new JButton("previous");
private JButton endBtn = new JButton("End");
// array of radio buttons to handle the answers
private JRadioButton[] radio = null;
private ButtonGroup group;
private JTextArea area;
private Container c;
private Panel p;
private java.util.List<Question> lst; // is a list of all questions in the xml file,
//of course with thier answers .
Map<Integer, String> map = new HashMap<Integer, String>(); // v.i.p map, it contains the user answer
//for each qustion
private char userAnswer; // hold the user answer
private int count = 0; // count of questions
private int score = 0; // score of the user
private boolean paint;
/*
* the init method of the applet
*/
public void init() {
try {
parse();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
initComponent();
}
/*
* initialize the UI component
*/
public void initComponent() {
Container c = getContentPane();
this.c = c;
c.setLayout(new BorderLayout());
Panel downPnl = new Panel();
c.add(downPnl, BorderLayout.SOUTH);
downPnl.setLayout(new BorderLayout());
Panel radioPnl = new Panel();
radioPnl.setLayout(new FlowLayout());
downPnl.add(radioPnl, BorderLayout.CENTER);
p = radioPnl; // <======================
Panel navgatPnl = new Panel();
downPnl.add(navgatPnl, BorderLayout.SOUTH);
navgatPnl.setLayout(new BorderLayout());
Panel btnPnl = new Panel();
navgatPnl.add(btnPnl, BorderLayout.EAST);
btnPnl.setLayout(new GridLayout());
btnPnl.add(prvBtn);
btnPnl.add(nxtBtn);
btnPnl.add(endBtn);
nxtBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionPerformedNxtBtn(e);
}
});
prvBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionPerformedPrvBtn(e);
}
});
endBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionPerformedEndBtn(e);
}
});
displayAnswers(radioPnl, '$');
displayQuestion(c);
}
/*
* display questions , invoked every time the user presses next or previous
*/
public void displayQuestion(Container c) {
area = new JTextArea();
c.add(area, BorderLayout.CENTER);
StringBuilder question = new StringBuilder();
question.append(lst.get(count).getValue() + "\n\n");
for (int i = 0; i < lst.get(count).getAnswers().size(); i++) {
question.append(lst.get(count).getAnswers().get(i).getId() + "-" +
lst.get(count).getAnswers().get(i).getValue() +
"\n");
}
area.setText(question.toString());
area.setEditable(false);
}
/*
* display answers for the question , invoked every time the user presses next or previous
*/
public void displayAnswers(Panel p, char ch) {
int size = lst.get(count).getAnswers().size();
//System.out.println(size);
p.removeAll();
p.repaint();
radio = new JRadioButton[size];
group = new ButtonGroup();
for (int i = 0; i < size; i++) {
radio =
new JRadioButton(String.valueOf(lst.get(count).getAnswers().get(i).getId()));
radio.setActionCommand(new Character(lst.get(count).getAnswers().get(i).getId()).toString());
radio.addActionListener(this);
if (ch != '$') {
if (radio.getText().charAt(0) == ch)
radio.setSelected(true);
}
group.add(radio);
p.add(radio);
}
}
/*
* This method is the handlr for the buttton next
*/
public void actionPerformedNxtBtn(ActionEvent e) {
if (count < lst.size() - 1) {
count++;
displayQuestion(c);
//map.get(count).setSelected(true);
try {
char ch = map.get(count).charAt(0);
displayAnswers(p, ch);
} catch (NullPointerException ex) {
displayAnswers(p, '$');
}
}else
actionPerformedEndBtn(e);
}
/*
* This method is the handlr for the buttton previous
*/
public void actionPerformedPrvBtn(ActionEvent e) {
if (count > 0) {
count--;
displayQuestion(c);
try {
char ch = map.get(count).charAt(0);
displayAnswers(p, ch);
} catch (NullPointerException ex) {
displayAnswers(p, '$');
}
}
}
/*
* this method is the handler for the button End
*/
public void actionPerformedEndBtn(ActionEvent e) {
for (int i = 0; i < map.size(); i++) {
Question ques = MyHandler.getQuestion(i);
if (ques.getCorrectAnswer() == map.get(i).charAt(0)) {
score++;
}
}
c.removeAll();
c.repaint();
paint = true;
repaint();
}
public void paint(Graphics g){
if (paint){
//g.setFont(new Font ("sans", Font.BOLD, 20));
g.drawString ("You get : " + score + " from "+ lst.size(), 100, 100);
}
repaint();
}
/*
* is the method that do the parse by calling the Handler MyHandler
*/
public void parse() throws IOException, SAXException {
FileInputStream file = new FileInputStream(new File("data.xml"));
InputSource insrc = new InputSource(file);
XMLReader reader =
XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
java.util.List<Question> list = new ArrayList<Question>();
MyHandler hndlr = new MyHandler(list);
reader.setContentHandler(hndlr);
reader.parse(insrc);
lst = list;
}
/*
* radio buttons action method :
*/
public void actionPerformed(ActionEvent e) {
userAnswer = e.getActionCommand().charAt(0);
JRadioButton rb = (JRadioButton)e.getSource();
map.put(count, rb.getActionCommand());
}
}
/**
* In the name of ALLAH, The gracious the merciful
* This is the examframework version 1.0 by mohammed hewedy
* you can use, modify and redistribute it as you like, no copyright or left owned by me
* I hope you can understand it with its little comments
*
*
* This class needs your efforts
*/
package client;
import javax.swing.*;
public class TheTimer extends JLabel {
int time;
public TheTimer() {
}
}
/**
و ده مثال علي ملف الاسئله :
<questions>
<question no="1" correctAnswerId="a">
Can we extend an abstract class ?
<answer id="a"> yes </answer>
<answer id="b"> no, we can not </answer>
</question>
<question no="2" correctAnswerId="b">
can we instantiate a abastract class ?
<answer id="a"> yes </answer>
<answer id="b"> no </answer>
</question>
<question no="3" correctAnswerId="a">
What is your name ?
<answer id="a"> Omar </answer>
<answer id="b"> wael </answer>
<answer id="c"> mostafa </answer>
</question>
<question no="4" correctAnswerId="a">
How old are you ?
<answer id="a"> 20 </answer>
<answer id="c"> 40 </answer>
</question>
<question no="5" correctAnswerId="c">
what is the number of your building ?
<answer id="a"> 4 </answer>
<answer id="b"> 8 </answer>
<answer id="c"> 1 </answer>
<answer id="d"> 3 </answer>
</question>
<question no="6" correctAnswerId="b">
what does steal means in .net ?
<answer id="a"> the class can not be instantiated </answer>
<answer id="b"> the class can not be inhereted </answer>
</question>
</questions>
<?xml version="1.0" encoding="UTF-8" ?>
الشرح :
البرنامج يقوم بقراءق ملف xml و من ثم عرضه للممتحن و تسجيل إجاباته و عرضها في النهايه
الفئه Answer يمثل إجابه من الإجابات
الفئه Question تمثل السؤال
الفئه MyHandler هي خاصه بعمليه ال parse
الفئه Test هي بمثابة ال Main class في المشروع
لحاد كده كله تماتم ، المشروع شغال حلو
الفئه TheTimer تحت التطوير،
والمراد منها إظهار الوقت المتبقي من الامتحان، فكرت أن أعملها JLabel و أضعها علي ال Frame المصنوع في الفئه Test و لكني لم أنفذ، أرجو التطويير
**** ملحوظه هذا المشروع جزء من مشروع تحرجي الذي لم يسلم بعد *******
يوجد ملف zip مهم لجعل هذا المشروع يعمل ، سو أرفقه في المرفقات، كل ماعليك هو أن تفكه ثم تضعه في ال classpath بتاعك
في رعاية الله
