0% found this document useful (0 votes)
29 views7 pages

TP6 Correction

The document contains code for working with sets and lists of numbers in Java. It defines methods for creating singleton sets, adding elements, displaying sets, performing union and intersection of sets, checking if an element belongs to a set, and getting the cardinality (size) of a set. It demonstrates using these methods by creating some sample sets, performing operations on them, and checking properties. The main purpose is to provide reusable set methods and demonstrate their use through examples.

Uploaded by

Baira Meriem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views7 pages

TP6 Correction

The document contains code for working with sets and lists of numbers in Java. It defines methods for creating singleton sets, adding elements, displaying sets, performing union and intersection of sets, checking if an element belongs to a set, and getting the cardinality (size) of a set. It demonstrates using these methods by creating some sample sets, performing operations on them, and checking properties. The main purpose is to provide reusable set methods and demonstrate their use through examples.

Uploaded by

Baira Meriem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Exercice 1 :

import java.util.ArrayList;
import java.util.Scanner;
public class Notes {

public static void ajouter(ArrayList<Double> notes, Double lanote) {


notes.add(lanote);
}

public static void afficher(ArrayList<Double> notes) {


System.out.print("Notes: ");
for (int i = 0; i < notes.size(); i++) {
System.out.print(notes.get(i) + " ");
}
System.out.println();
}

public static Double moyenne(ArrayList<Double> notes) {


Double somme = 0.0;
for (int i = 0; i < notes.size(); i++) {
somme = somme + notes.get(i);
}
return somme / notes.size();
}

//question B
public static void modifier(ArrayList<Double> notes, int ind, Double lanote) {
if (ind >= 0 && ind < notes.size()) {
notes.set(ind, lanote);
} else {
System.out.println("L'indice spécifié est hors de la plage valide.");
}
}
static Scanner scanner = new Scanner(System.in);

public static void saisieModif(ArrayList<Double> notes){


int achanger;
double nouvelle;
for (int i=0; i<notes.size(); i++){
System.out.println("Note numero " + (i+1) + ":");
System.out.println(notes.get(i));
}
System.out.println("Entrez le numéro de la note à changer: ");
achanger = scanner.nextInt()-1;
System.out.println("Entrez la note correcte: ");
nouvelle = scanner.nextDouble();
modifier(notes,achanger,nouvelle);
}
//question c

static void afficheMenu() {


String[] M = {"Afficher", "Ajouter", "Moyenne", "Modifier", "Quitter"};
System.out.println("Menu");
for (int i = 0; i < M.length; i++) {
System.out.println((i + 1) + ". " + M[i]);
}
System.out.print("Votre choix: ");
}

public static void main(String[] args) {


ArrayList<Double> lesnotes = new ArrayList<Double>();
/* ajouter(lesnotes, 12.0);
ajouter(lesnotes, 14.0);
ajouter(lesnotes, 9.0);
afficher(lesnotes);
System.out.println("Moyenne: " + moyenne(lesnotes));
ajouter(lesnotes, 13.0);
afficher(lesnotes);
System.out.println("Moyenne: " + moyenne(lesnotes));

//question B
modifier(lesnotes, 2, 19.5);
afficher(lesnotes);*/

//question c

int rep;
do {
afficheMenu();
rep = scanner.nextInt();
if (rep == 1) {
afficher(lesnotes);
} else if (rep == 2) {
System.out.print("Entrez la note: ");
ajouter(lesnotes, scanner.nextDouble());
} else if (rep == 3) {
System.out.println("Moyenne: " + moyenne(lesnotes));
} else if (rep == 4) {
saisieModif(lesnotes);
} else if (rep != 5) {
System.out.println("Vous devez choisir une option entier compris entre 1 et 5.");
}
} while (rep != 5);

}
}

import java.util.ArrayList;
import java.util.Scanner;

public class NotesCoeff {


static Scanner scanner = new Scanner(System.in);

public static void ajouter(ArrayList<Double> notes, ArrayList<Integer> coeffs, Double


lanote, int coeff) {
notes.add(lanote);
coeffs.add(coeff);
}

public static Double moyenne(ArrayList<Double> notes, ArrayList<Integer> coeffs) {


Double cumul = 0.0;
int totalcoeffs = 0;
for (int i = 0; i < notes.size(); i++) {
cumul = cumul + (notes.get(i) * coeffs.get(i));
totalcoeffs = totalcoeffs + coeffs.get(i);
}
return cumul / totalcoeffs;
}

public static void afficher(ArrayList<Double> notes, ArrayList<Integer> coeffs) {


System.out.println("Notes: ");
for (int i = 0; i < notes.size(); i++) {
System.out.print(notes.get(i) + " coefficient ");
System.out.println(coeffs.get(i));
}
}

public static void modifier(ArrayList<Double> notes, int ind, Double lanote) {


if (ind >= 0 && ind < notes.size()) {
notes.set(ind, lanote);
} else {
System.out.println("L'indice spécifié est hors de la plage valide.");
}
}
public static void saisieModif(ArrayList<Double> notes){
int achanger;
double nouvelle;
for (int i=0; i<notes.size(); i++){
System.out.println("Note numero " + (i+1) + ":");
System.out.println(notes.get(i));
}
System.out.println("Entrez le numéro de la note à changer: ");
achanger = scanner.nextInt()-1;
System.out.println("Entrez la note correcte: ");
nouvelle = scanner.nextDouble();
modifier(notes,achanger,nouvelle);
}
static void afficheMenu() {
String[] M = {"Afficher", "Ajouter", "Moyenne", "Modifier", "Quitter"};
System.out.println("Menu");
for (int i = 0; i < M.length; i++) {
System.out.println((i + 1) + ". " + M[i]);
}
System.out.print("Votre choix: ");
}

public static void main(String[] args) {


ArrayList<Double> lesnotes = new ArrayList<Double>();
ArrayList<Integer> lescoeffs = new ArrayList<Integer>();
int rep;
double note;
int coeff;
do {
afficheMenu();
rep = scanner.nextInt();
if (rep == 1) {
afficher(lesnotes, lescoeffs);
} else if (rep == 2) {
System.out.print("Entrez la note: ");
note = scanner.nextDouble();
System.out.print("Entrez le coefficient: ");
coeff = scanner.nextInt();
ajouter(lesnotes, lescoeffs, note, coeff);
} else if (rep == 3) {
System.out.println("Moyenne: " + moyenne(lesnotes, lescoeffs));
} else if (rep == 4) {
saisieModif(lesnotes);
} else if (rep != 5) {
System.out.println("Vous devez choisir une option entier compris entre 1 et
5.");
}
} while (rep != 5);
}}

Exercice 2 :

import java.util.ArrayList;

public class Ens {

public static ArrayList<Integer> singleton(int x) {


ArrayList<Integer> res = new ArrayList<Integer>();
res.add(x);
return res;
}

public static void ajouter(ArrayList<Integer> e, int x) {


if (!e.contains(x)) {
e.add(x);
}
}
public static void afficher(ArrayList<Integer> e) {
System.out.print('{');
for (int i = 0; i < e.size() ; i++) {
System.out.print(e.get(i) + " ");
}

System.out.println('}');
}

public static ArrayList<Integer> union(ArrayList<Integer> e1, ArrayList<Integer> e2) {


ArrayList<Integer> res = new ArrayList<Integer>();
for (int i = 0; i < e1.size(); i++) {
ajouter(res, e1.get(i));
}
for (int i = 0; i < e2.size(); i++) {
ajouter(res, e2.get(i));
}
return res;
}

public static ArrayList<Integer> inter(ArrayList<Integer> e1, ArrayList<Integer> e2) {


ArrayList<Integer> res = new ArrayList<Integer>();
for (int i = 0; i < e1.size(); i++) {
if (e2.contains(e1.get(i))) {
ajouter(res, e1.get(i));
}
}
return res;
}

/*************************/
public static boolean appartient(ArrayList<Integer> e, int x){
return e.contains(x);
}

public static int cardinal(ArrayList<Integer> e){


return e.size();
}

public static void main(String[] args) {


ArrayList<Integer> ens1, ens2, ens3, ens4;
ens1 = singleton(1);
System.out.print("ens1=");
ajouter(ens1, 5);
afficher(ens1);
System.out.println();

ens2 = singleton(4);
ajouter(ens2, 5);
System.out.print("ens2=");
afficher(ens2);
System.out.println();

ens3 = union(ens1, ens2);


System.out.print("ens3=");
afficher(ens3);

System.out.println();
ens4 = inter(ens3, ens2);
System.out.print("ens4=");
afficher(ens4);
System.out.println();

/*****/

System.out.println("Card(ens2)=" + cardinal(ens2));
System.out.println("3 appartient a ens2=" + appartient(ens2, 3));
System.out.println("5 appartient a ens2=" + appartient(ens4, 5));

}
}

You might also like