Vous êtes sur la page 1sur 3

Correction Devoir de contrôle 2017

Partie 1
public class Question {

private String énoncéQuestion;

private final String thèmeQuestion;

protected String tabRépProposées[];

protected int nbRépCorrectes;

public Question(String énoncéQuestion, String thèmeQuestion, int nbRépCorrectes ){

this.énoncéQuestion=énoncéQuestion; this.thèmeQuestion=thèmeQuestion;

this.nbRépCorrectes =nbRépCorrectes ;

this.tabRépProposées=new String[3];}

public void setTabRépProposées(String tab[]){

this.tabRépProposées=tab;}
public String toString(){

String str="";

for (int i=0; i<this.tabRépProposées.length; i++)

str+="\n\t"+(i+1)+"- " +this.tabRépProposées[i];

return this.énoncéQuestion +"("+this.nbRépCorrectes+") \n"+str;}

public boolean estIdentique(Question Q){

return this.énoncéQuestion.equals(Q.énoncéQuestion) &&


this.thèmeQuestion.equals(Q.thèmeQuestion); }

public static boolean estIdentique(Question Q1,Question Q2){

return Q1.estIdentique(Q2) ;}

public String getThème()

{return this.thèmeQuestion;}}

Partie II
public class QuestionRépUnique extends Question{

private String repCorrecte ;

public QuestionRépUnique(String énoncéQuestion, String thèmeQuestion)

{super(énoncéQuestion, thèmeQuestion,1);}

1
public void setRepCorrecte( String repCorrecte) {this.repCorrecte= repCorrecte;}

public int numRepCorrecte(){

int i=0;

while (i<this.tabRépProposées.length && !this.tabRépProposées[i].equals(this.repCorrecte))

i++;

if( i< this.tabRépProposées.length)

return i+1;

else return -1;}}

import java.util.Scanner;

public class QCM {

private String thèmeQCM;

private QuestionRépUnique tabQuestions[];

private int nbQuestions;

public QCM(String thèmeQCM) {

this.thèmeQCM= thèmeQCM;

this.tabQuestions= new QuestionRépUnique[30];}

public void ajouterQuestion( QuestionRépUnique Q ) {

if(nbQuestions <30 && Q.getThème().equals(thèmeQCM))

{this.tabQuestions[this.nbQuestions]=Q; this.nbQuestions++;}}

public int répondreQCM(){

int nbcorrecte=0;

int numréponse;

Scanner S=new Scanner(System.in);

for(int i=0;i<this.nbQuestions;i++)

{ System.out.println(this.tabQuestions[i].toString());

System.out.println("Saisissez le numéro de votre réponse");

numréponse=S.nextInt();

if (numréponse==this.tabQuestions[i].numRepCorrecte ()) nbcorrecte++; }

return nbcorrecte;}}

2
public class Test {

public static void main(String args[])

{QCM QCMPOO= new QCM("POO");

QuestionRépUnique Q1=new QuestionRépUnique("Java est un langage", "POO");

Q1.setRepCorrecte("compilé et interprété");

String tab1[]={"compilé", "interprété","compilé et interprété" };

Q1.setTabRépProposées(tab1);

QuestionRépUnique Q2=new QuestionRépUnique("L'interprétation du langage JAVA est effectuée


par", "POO");

Q2.setRepCorrecte("JVM");

String tab2[]={"API", "JDK","JVM" };

Q2.setTabRépProposées(tab2);

QCMPOO.ajouterQuestion(Q1);

QCMPOO.ajouterQuestion(Q2);

int score= QCMPOO.répondreQCM();

System.out.println("le nombre de réponse correcte est: " + score);

}}

Vous aimerez peut-être aussi