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) {
[Link]=numQ;
[Link]=txtQ;
[Link]=noteQ;
}
public int getNumQ() {
return numQ;
}
public void setNumQ(int numQ) {
[Link] = numQ;
}
public String getTxtQ() {
return txtQ;
}
public void setTxtQ(String txtQ) {
[Link] = txtQ;
}
public double getNoteQ() {
return noteQ;
}
public void setNoteQ(double noteQ) {
[Link] = 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);
[Link]=reponseCorrecte;
}
public String getReponseCorrecte() {
return reponseCorrecte;
}
public void setReponseCorrecte(String reponseCorrecte) {
[Link] = reponseCorrecte;
}
@Override
public void poser() {
[Link](getTxtQ());
}
@Override
public double verifierQuestion(String rep) {
// TODO Auto-generated method stub
if([Link](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) {
[Link]=txtC;
[Link]=valeurC;
}
public String getTxtC() {
2
return txtC;
}
public void setTxtC(String txtC) {
[Link] = txtC;
}
public boolean isValeurC() {
return valeurC;
}
public void setValeurC(boolean valeurC) {
[Link] = valeurC;
}
}
package examen_2023;
import [Link];
public class Quiz extends Question implements Melangeable {
private ArrayList<Choix> listeChoix;
public Quiz(int numQ,String txtQ,double noteQ) {
super(numQ,txtQ,noteQ);
[Link]=new ArrayList<>();
}
public void ajouterChoix(Choix c) throws Quiz_Multi_Rep {
if ([Link]()) {
for (Choix ch : listeChoix) {
if ([Link]()) {
throw new Quiz_Multi_Rep();
}
}
}
if ([Link]() < 5) {
[Link](c);
}
}
@Override
public void melanger() {
int n = [Link]();
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] = [Link]();
}
// Étape 2 : tri par insertion (ou autre) en fonction des randomKeys
for (int i = 1; i < n; i++) {
double key = randomKeys[i];
Choix c = [Link](i);
int j = i - 1;
while (j >= 0 && randomKeys[j] > key) {
randomKeys[j + 1] = randomKeys[j];
[Link](j + 1, [Link](j));
j--;
}
3
randomKeys[j + 1] = key;
[Link](j + 1, c);
}
}
@Override
public void poser() {
// TODO Auto-generated method stub
[Link]("Q" + getNumQ() + ": " + getTxtQ());
int i = 1;
for (Choix c : listeChoix) {
[Link](i + ") " + [Link]());
i++;
}
}
@Override
public double verifierQuestion(String rep) {
int choixIndex = [Link](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 < [Link]()) {
Choix choix = [Link](choixIndex);
return [Link]() ? 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) {
[Link] = 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 [Link];
public class Examen implements Melangeable {
private String nomEx;
private ArrayList<Question> listeQ;
private ArrayList<String> listeR;
public Examen(String nomEx) {
[Link]=nomEx;
[Link]=new ArrayList<>();
[Link]=new ArrayList<>();
}
public void ajouterQuestion(Question q) {
[Link](q);
}
public void ajouterReponse(String r) {
[Link](r);
}
@Override
public void melanger() {
// TODO Auto-generated method stub
int n=[Link]();
double[] randomKeys= new double [n];
//bich na3mil tab fih des nombres aleatoires pour chaque question
for(int i=0;i<n;i++) {
randomKeys[i]=[Link]();
}
// taw ta3mil tri 3al les nombres aleatoires hekom
for(int i=1;i<n;i++) {
double key = randomKeys[i];
Question q = [Link](i);
int j = i - 1;
while (j >= 0 && randomKeys[j] > key) {
randomKeys[j + 1] = randomKeys[j];
[Link](j + 1, [Link](j)); // remplace l'ele situe a une certaine position par un
nouveau Syntaxe [Link](index,nouvelElement)
j--;
}
randomKeys[j + 1] = key;
[Link](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) {
[Link] = nomEx;
}
public ArrayList<Question> getListeQ() {
return listeQ;
}
public void setListeQ(ArrayList<Question> listeQ) {
[Link] = listeQ;
}
public ArrayList<String> getListeR() {
return listeR;
}
public void setListeR(ArrayList<String> listeR) {
[Link] = listeR;
}
public void lancerExamen(String[] reponses) throws Examen_vide{
if([Link]()) {
throw new Examen_vide();
}
[Link]("Examen :" + nomEx);
for(int i=0;i<[Link]();i++) {
Question q=[Link](i);
[Link]();
String rep=reponses[i];
[Link]("Reponse saisie :"+rep);
ajouterReponse(rep);
}
}
public double verifierExamen() {
double total =0;
for(int i=0;i< [Link]();i++) {
total+=[Link](i).verifierQuestion([Link](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);
[Link](new Choix("Real Madrid", true));
[Link](new Choix("Barcelone", false));
[Link](new Choix("Manchester City", false));
[Link](new Choix("Liverpool", false));
Examen examen = new Examen("Examen de Culture Générale");
[Link](question1);
[Link](question2);
[Link]();
// 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 < [Link]().size(); i++) {
Question q = [Link]().get(i);
if (q instanceof QuestionRepCourte) {
reponses[i] = "Paris";
} else if (q instanceof Quiz) {
Quiz qz = (Quiz) q;
for (int j = 0; j < [Link]().size(); j++) {
if ([Link]().get(j).isValeurC()) {
reponses[i] = [Link](j + 1);
break;
}
}
}
}
[Link](reponses);
double noteFinale = [Link]();
[Link]("Note finale : " + noteFinale + " / " + ([Link]() +
[Link]()));
} catch (Quiz_Multi_Rep e) {
[Link]("Erreur : " + e);
} catch (Examen_vide e) {
[Link]("Erreur : " + e);
}
}
}