Examen 2023:
package examen_2023;
public abstract class Question {
private int numQ;
private String txtQ;
private double noteQ;
public Question(int numQ,String txtQ,double noteQ) {
this.numQ=numQ;
this.txtQ=txtQ;
this.noteQ=noteQ;
}
public int getNumQ() {
return numQ;
}
public void setNumQ(int numQ) {
this.numQ = numQ;
}
public String getTxtQ() {
return txtQ;
}
public void setTxtQ(String txtQ) {
this.txtQ = txtQ;
}
public double getNoteQ() {
return noteQ;
}
public void setNoteQ(double noteQ) {
this.noteQ = noteQ;
}
public abstract void poser();
public abstract double verifierQuestion(String rep);
}
1
package examen_2023;
public class QuestionRepCourte extends Question {
private String reponseCorrecte;
public QuestionRepCourte(int numQ,String txtQ,double noteQ,String reponseCorrecte) {
super(numQ,txtQ,noteQ);
this.reponseCorrecte=reponseCorrecte;
}
public String getReponseCorrecte() {
return reponseCorrecte;
}
public void setReponseCorrecte(String reponseCorrecte) {
this.reponseCorrecte = reponseCorrecte;
}
@Override
public void poser() {
System.out.println(getTxtQ());
}
@Override
public double verifierQuestion(String rep) {
// TODO Auto-generated method stub
if(rep.equalsIgnoreCase(reponseCorrecte)) {
return getNoteQ();
}
return 0;
}
}
package examen_2023;
public interface Melangeable {
void melanger();
}
package examen_2023;
public class Quiz_Multi_Rep extends Exception {
@Override
public String toString() {
return "Un quiz ne peut contenir qu'une seule bonne réponse." ;
}
}
package examen_2023;
public class Choix {
private String txtC;
private boolean valeurC;
public Choix(String txtC,boolean valeurC) {
this.txtC=txtC;
this.valeurC=valeurC;
}
public String getTxtC() {
2
return txtC;
}
public void setTxtC(String txtC) {
this.txtC = txtC;
}
public boolean isValeurC() {
return valeurC;
}
public void setValeurC(boolean valeurC) {
this.valeurC = valeurC;
}
}
package examen_2023;
import java.util.ArrayList;
public class Quiz extends Question implements Melangeable {
private ArrayList<Choix> listeChoix;
public Quiz(int numQ,String txtQ,double noteQ) {
super(numQ,txtQ,noteQ);
this.listeChoix=new ArrayList<>();
}
public void ajouterChoix(Choix c) throws Quiz_Multi_Rep {
if (c.isValeurC()) {
for (Choix ch : listeChoix) {
if (ch.isValeurC()) {
throw new Quiz_Multi_Rep();
}
}
}
if (listeChoix.size() < 5) {
listeChoix.add(c);
}
}
@Override
public void melanger() {
int n = listeChoix.size();
double[] randomKeys = new double[n];
// Étape 1 : générer un nombre aléatoire pour chaque élément
for (int i = 0; i < n; i++) {
randomKeys[i] = Math.random();
}
// Étape 2 : tri par insertion (ou autre) en fonction des randomKeys
for (int i = 1; i < n; i++) {
double key = randomKeys[i];
Choix c = listeChoix.get(i);
int j = i - 1;
while (j >= 0 && randomKeys[j] > key) {
randomKeys[j + 1] = randomKeys[j];
listeChoix.set(j + 1, listeChoix.get(j));
j--;
}
3
randomKeys[j + 1] = key;
listeChoix.set(j + 1, c);
}
}
@Override
public void poser() {
// TODO Auto-generated method stub
System.out.println("Q" + getNumQ() + ": " + getTxtQ());
int i = 1;
for (Choix c : listeChoix) {
System.out.println(i + ") " + c.getTxtC());
i++;
}
}
@Override
public double verifierQuestion(String rep) {
int choixIndex = Integer.parseInt(rep) - 1; // Convertit la réponse en index
// Vérifie si l'index est valide et dans la plage des choix disponibles
if (choixIndex >= 0 && choixIndex < listeChoix.size()) {
Choix choix = listeChoix.get(choixIndex);
return choix.isValeurC() ? getNoteQ() : 0; // Si la réponse est correcte, retourne la note
}
return 0;
}
public ArrayList<Choix> getListeChoix() {
return listeChoix;
}
public void setListeChoix(ArrayList<Choix> listeChoix) {
this.listeChoix = listeChoix;
}
}
package examen_2023;
public class Examen_vide extends Exception {
@Override
public String toString() {
return "l'examen ne contient aucune question" ;
}
}
4
package examen_2023;
import java.util.ArrayList;
public class Examen implements Melangeable {
private String nomEx;
private ArrayList<Question> listeQ;
private ArrayList<String> listeR;
public Examen(String nomEx) {
this.nomEx=nomEx;
this.listeQ=new ArrayList<>();
this.listeR=new ArrayList<>();
}
public void ajouterQuestion(Question q) {
listeQ.add(q);
}
public void ajouterReponse(String r) {
listeR.add(r);
}
@Override
public void melanger() {
// TODO Auto-generated method stub
int n=listeQ.size();
double[] randomKeys= new double [n];
//bich na3mil tab fih des nombres aleatoires pour chaque question
for(int i=0;i<n;i++) {
randomKeys[i]=Math.random();
}
// taw ta3mil tri 3al les nombres aleatoires hekom
for(int i=1;i<n;i++) {
double key = randomKeys[i];
Question q = listeQ.get(i);
int j = i - 1;
while (j >= 0 && randomKeys[j] > key) {
randomKeys[j + 1] = randomKeys[j];
listeQ.set(j + 1, listeQ.get(j)); // remplace l'ele situe a une certaine position par un
nouveau Syntaxe liste.set(index,nouvelElement)
j--;
}
randomKeys[j + 1] = key;
listeQ.set(j + 1, q);
}
for(Question q:listeQ) {
if(q instanceof Quiz) {
((Quiz)q).melanger();
}
}
}
public String getNomEx() {
5
return nomEx;
}
public void setNomEx(String nomEx) {
this.nomEx = nomEx;
}
public ArrayList<Question> getListeQ() {
return listeQ;
}
public void setListeQ(ArrayList<Question> listeQ) {
this.listeQ = listeQ;
}
public ArrayList<String> getListeR() {
return listeR;
}
public void setListeR(ArrayList<String> listeR) {
this.listeR = listeR;
}
public void lancerExamen(String[] reponses) throws Examen_vide{
if(listeQ.isEmpty()) {
throw new Examen_vide();
}
System.out.println("Examen :" + nomEx);
for(int i=0;i<listeQ.size();i++) {
Question q=listeQ.get(i);
q.poser();
String rep=reponses[i];
System.out.println("Reponse saisie :"+rep);
ajouterReponse(rep);
}
}
public double verifierExamen() {
double total =0;
for(int i=0;i< listeQ.size();i++) {
total+=listeQ.get(i).verifierQuestion(listeR.get(i));
}
return total;
}
}
6
package examen_2023;
public class TestExamen {
public static void main(String[] args) {
try {
Question question1 = new QuestionRepCourte(1, "Quelle est la capitale de la France ?", 2.0,
"Paris");
Quiz question2 = new Quiz(2, "Quelle est la meilleure équipe de football ?", 3.0);
question2.ajouterChoix(new Choix("Real Madrid", true));
question2.ajouterChoix(new Choix("Barcelone", false));
question2.ajouterChoix(new Choix("Manchester City", false));
question2.ajouterChoix(new Choix("Liverpool", false));
Examen examen = new Examen("Examen de Culture Générale");
examen.ajouterQuestion(question1);
examen.ajouterQuestion(question2);
examen.melanger();
// he4i 5atir les rquestion t5altou bich nijim n5ali les reponses bi nafs l'ordre mta3 les question
String[] reponses = new String[2];
for (int i = 0; i < examen.getListeQ().size(); i++) {
Question q = examen.getListeQ().get(i);
if (q instanceof QuestionRepCourte) {
reponses[i] = "Paris";
} else if (q instanceof Quiz) {
Quiz qz = (Quiz) q;
for (int j = 0; j < qz.getListeChoix().size(); j++) {
if (qz.getListeChoix().get(j).isValeurC()) {
reponses[i] = String.valueOf(j + 1);
break;
}
}
}
}
examen.lancerExamen(reponses);
double noteFinale = examen.verifierExamen();
System.out.println("Note finale : " + noteFinale + " / " + (question1.getNoteQ() +
question2.getNoteQ()));
} catch (Quiz_Multi_Rep e) {
System.out.println("Erreur : " + e);
} catch (Examen_vide e) {
System.out.println("Erreur : " + e);
}
}
}