Vous êtes sur la page 1sur 10

Exercices (révision examen final)

NUMÉRO 1

public class QuestionTableaux {

public static void bidouiller(int[] t1, int n) {


int i = 1;
int j;
t1[1] = n + 3;

while (i <= t1.length * n) {


if (i < 4) {
j = i % t1.length;
} else {
j = (i - 1) % t1.length;
}

t1[j] = t1[j] % t1.length;


i = i + n;
}
}

public static int[] rafistoler(int[] t1, int[] t2) {


int[] t3 = new int[t1.length];
int tmp = t1[2] / 6;
t3[0] = t3[2] + t1[1];
t3[3] = t1[t1[3] + t2[0] - 1];
t3[0]++;
t3[1] = t2[3] * t1[2];

t3[t2[2]] = t3[1] / 10;


t3[1] = t3[1] % t1[0] - tmp;

return t3;
}

public static void main(String[] args) {


int[] t1 = {5, 2, 10, 7};
int[] t2 = {7, 2, 6, 0};
int[] t3 = {1, 4, 2, 8};

bidouiller(t1, 2);
t2 = rafistoler(t2, t3);

//-----> Donnez l'état des tableaux t1 et t2 ici <-----

}
}

État du tableau t1 :

État du tableau t2 :

1
NUMÉRO 2
(a)

public class PassageParams1 {

public static int louer (int nbrHr, int prixParHr) {


if (nbrHr >= 10) {
prixParHr = prixParHr - 2;
} else if (prixParHr >= 5) {
prixParHr = prixParHr - 5;
}
return nbrHr * prixParHr;
}

public static void main(String[] args) {


int nbrHr = 10;
int prixParHr = 17;
int prixLocation = 0;

prixLocation = louer(nbrHr, prixParHr);

System.out.print(prixLocation); // <----------AFFICHAGE 1
System.out.print(prixParHr); // <----------AFFICHAGE 2
}
}

Dites ce qu'affiche la méthode main de la classe PassageParams1 ci-dessus.

AFFICHAGE 1 : __________________________________

AFFICHAGE 2 : __________________________________

2
(b)

public class PassageParams2 {

public static void deposerMontant (int[] comptes) {


if (comptes == null) {
comptes = new int[4];

for (int i = 0 ; i < comptes.length ; i++) {


comptes[i] = 10;
}

} else {
for (int i = 0 ; i < comptes.length ; i++) {
comptes[i] = comptes[i] * 10 / 100;
}
}
}

public static void afficherSomme (int[] tab) {


int somme = 0;

if (tab != null) {
for (int i = 0; i < tab.length; i++) {
somme = somme + tab[i];
}
}

System.out.print(somme);
}

public static void main (String[] args) {


int [] comptes1 = {100, 10, 70, 50};
int [] comptes2 = null;

deposerMontant(comptes1);
afficherSomme(comptes1); // <---------------- AFFICHAGE 1

deposerMontant(comptes2);
afficherSomme(comptes2); // <---------------- AFFICHAGE 2
}
}

Dites ce qu'affiche la méthode main de la classe PassageParams2 ci-dessus.

AFFICHAGE 1 : __________________________________

AFFICHAGE 2 : __________________________________

3
(c)

public class Point3D {


private int x;
private int y;
private int z;

public Point3D(int x, int y, int z) {


this.x = x;
this.y = y;
this.z = z;
}

public void deplacer(int deplacement) {


x = x + deplacement;
y = y + deplacement;
z = z + deplacement;
}

public void translater(Point3D point, int deplacement) {


point = new Point3D(point.x, point.y, point.z);
point.deplacer(deplacement);
}

public void afficher() {


System.out.print(x + y + z);
}
}

public class EspaceVectoriel {

public static void main(String[] args) {


Point3D point = new Point3D(3, 4, 1);
point.translater(point, 1);
point.afficher(); //<---------------- AFFICHAGE
}
}

Considérez les classes Point3D et EspaceVectoriel ci-dessus, et dites ce qui est affiché lorsque la
méthode main de la classe EspaceVectoriel est exécutée.

AFFICHAGE : __________________________________

4
NUMÉRO 3

Soit le fichier fic1.txt qui contient les six (6) lignes suivantes. Notez qu'on a indiqué les sauts de ligne en
montrant les caractères \n qui sont présents mais normalement invisibles, lorsqu'on regarde le fichier. De
plus, notez que les mots sont toujours séparés par un seul espace.

Supposez que le fichier fic1.txt existe, et qu’il se trouve à la racine de votre projet (ou dans le même
répertoire que l'application).

les cours de\n


la session\n
automne 2021\n
seront\n
en présentiel\n
yeah!\n

Soit le petit programme suivant :

import java.io.*;

public class QuestionFichiers {


public static final String FIC1 = "fic1.txt";
public static final String FIC2 = "fic2.txt";
public static final String MSG_ERR = "Erreur d’entrée/sortie !";

public static void test () throws IOException {


int nbr = 'x';
BufferedReader in = new BufferedReader(new FileReader(FIC1));
PrintWriter out = new PrintWriter(new FileWriter(FIC2));

for (char c = 'a' ; c < 'd' ; c++) {


out.print((char)in.read());
}

in.readLine();
in.read();
in.read();

while ((char)nbr != 'e') {


nbr = in.read();
}

out.println((char)(nbr + 1));
in.read();
in.read();
out.println(in.readLine() + (char)in.read());
in.readLine();

while (in.ready()) {
out.print((char)in.read());
in.readLine();
}
in.close();
out.close();
}

5
public static void main (String [] args) {
BufferedReader in;
try {
test();

in = new BufferedReader(new FileReader(FIC2));

System.out.print(in.readLine()); //<--------- AFFICHAGE 1

System.out.print(in.readLine()); //<--------- AFFICHAGE 2

System.out.print(in.readLine()); //<--------- AFFICHAGE 3

} catch (IOException e) {
System.out.print(MSG_ERR);
}
}
}

Dites ce qu'affiche la méthode main de la classe QuestionFichiers ci-dessus.

AFFICHAGE 1 : __________________________________

AFFICHAGE 2 : __________________________________

AFFICHAGE 3 : __________________________________

NUMRÉO 4

public class QuestionString {

public static void main (String [] args) {


int i;
int j;
String s1 = "fantastique";
String s2 = "bravissimo";

i = s1.lastIndexOf("a");
j = s2.indexOf("i", 5);

s1 = s1.substring(i, j + 1) + s2.charAt(0);

System.out.println(s1); //<------- AFFICHAGE

}
}

Dites ce qu'affiche la méthode main de la classe QuestionString ci-dessus.

AFFICHAGE 1 : __________________________________

6
NUMÉRO 5
public class QuestionExceptions {

public static void peindre(int valeur) {


int[] tab;
if (valeur > 3) {
tab = new int[valeur];
decouper(tab, "ab4cdefgh20k");
} else {
tab = new int[7];
try {
decouper(tab, "ab34yf8");
} catch (StringIndexOutOfBoundsException e) {
System.out.print("0");
} catch (NegativeArraySizeException e) {
System.out.print("1");
}
}
}
public static void decouper(int[] tab, String s) {
int n;
try {
n = dessiner(s, tab.length);
tab = new int[n % 2 - 1];
System.out.print(tab[100] + 20);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.print("2");
} catch (NumberFormatException e) {
System.out.print("3");
}
}
public static int dessiner(String s, int n) {
int i = 1;
try {
if (n == 6) {
i = Integer.parseInt("" + s.charAt(2) + s.charAt(n));
} else {
i = Integer.parseInt("" + s.charAt(n + 5));
}
} catch (ArithmeticException e) {
System.out.print("4");
} catch (NullPointerException e) {
System.out.print("5");
}
return n * 10 / i;
}
public static void main(String[] args) {
int valeur = 0;
try {
System.out.print("Entrez un nombre entier : ");
valeur = Clavier.lireInt(); //<----------------- VALEUR SAISIE
peindre(valeur);
} catch (NegativeArraySizeException e) {
System.out.print("6");
} catch (ArithmeticException e) {
System.out.print("7");
} catch (Exception e) {
System.out.print("8");
}
}
}

Qu'affiche la méthode main si l'utilisateur saisit la valeur 3 : _____________


Qu'affiche la méthode main si l'utilisateur saisit la valeur 4 : _____________
Qu'affiche la méthode main si l'utilisateur saisit la valeur 5 : _____________
Qu'affiche la méthode main si l'utilisateur saisit la valeur 6 : _____________
7
NUMÉRO 6

public class Trajet {

public static void seDeplacer(float vitesse, float distance) {


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

public static void seDeplacer(int vitesse, short distance) {


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

public static void seDeplacer(double vitesse, int distance) {


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

public static void seDeplacer(float vitesse, int distance) {


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

public static void seDeplacer(short vitesse, int distance) {


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

public static void seDeplacer(int vitesse, int distance) {


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

Considérez la classe Trajet ci-dessus, et donnez ce qui sera affiché lors des appels suivants. Si
l'instruction ne compile pas, veuillez écrire "Ne compile pas".

Soit les variables suivantes :


short v1 = 30;
short d1 = 100;
float v2 = 56;
short d2 = 67;

Qu'est-ce que l'instruction suivante affiche : Trajet.seDeplacer(v2, d2); _______________


Qu'est-ce que l'instruction suivante affiche : Trajet.seDeplacer(v1, d1); _______________
Qu'est-ce que l'instruction suivante affiche : Trajet.seDeplacer(97, (byte)d2); _______________
Qu'est-ce que l'instruction suivante affiche : Trajet.seDeplacer(15.3, 34L); _______________
Qu'est-ce que l'instruction suivante affiche : Trajet.seDeplacer(5L, 5F); _______________

8
NUMÉRO 7

public class Chien {

public final static String[] SEXES = {"male", "femelle"};


public final static String[] RACES = {"Labrador", "Dobermann", "Rottweiler",
"Dalmatien", "Husky", "Berger allemand", "Bulldog", "Pit Bull", "Chow-chow",
"Boxer"}

private String nom = "Toto";


private int race = 0;
private int age = 2;
private int sexe = 1;

public Chien(String nom, int race, int age, int sexe) {


this.nom = nom;
this.age = age;
if (race >= 0 && race < RACES.length) {
this.race = race;
}
if (sexe >= 0 && sexe < SEXES.length) {
this.sexe = sexe;
}
}

public Chien() {
this("Luna", 2, 6, 1);
}

public String getNom() {


return nom;
}

public void setNom(String nom) {


this.nom = nom;
}

public int getRace() {


return race;
}

public String getNomRace() {


return RACES[race];
}

public void setRace(int race) {


this.race = race;
}

public int getAge() {


return age;
}

public String toString() {


return "" + (race + age + sexe);
}
}

9
public class Chenil {

public static void main(String[] args) {

Chien[] chenil = new Chien[4];


Chien c1 = new Chien();
Chien c2 = new Chien("Max", 1, 4, 0);
Chien c3 = new Chien("Chipie", 4, 3, 2);
Chien c4 = new Chien("Pilou", 10, 1, 0);

System.out.println(c3); //<---------------------------- AFFICHAGE 1


System.out.println(c4.getNomRace()); //<--------------- AFFICHAGE 2

chenil[2] = c1;
chenil[0] = new Chien("Tor", c2.getAge() + c1.getAge() - 2, 5, 0);
chenil[3] = c3;
chenil[1] = c2;
c3.setNom(c4.getNom());
c2.setRace(6);
c1 = null;

System.out.println(chenil[1].getNomRace()); //<-------- AFFICHAGE 3


System.out.println(chenil[3].getNom()); //<------------ AFFICHAGE 4

c1 = chenil[0];
chenil[1] = chenil[3];
c3 = c2;
chenil[0] = c3;
chenil[3] = c1;

System.out.println(chenil[0]); //<--------------------- AFFICHAGE 5


System.out.println(chenil[1]); //<--------------------- AFFICHAGE 6
System.out.println(chenil[2]); //<--------------------- AFFICHAGE 7
System.out.println(chenil[3]); //<--------------------- AFFICHAGE 8

}
}

Considérez les deux (2) classes Chien et Chenil ci-dessus. Dites ce qu’affichent les huit (8) instructions d'affichage
System.out.println lorsque la méthode main de la classe Chenil est exécutée.

Rappel : La méthode toString est appelée implicitement sur l’objet qu'on affiche.

AFFICHAGE 1 : __________________________________
AFFICHAGE 2 : __________________________________
AFFICHAGE 3 : __________________________________
AFFICHAGE 4 : __________________________________
AFFICHAGE 5 : __________________________________
AFFICHAGE 6 : __________________________________
AFFICHAGE 7 : __________________________________
AFFICHAGE 8 : __________________________________
10

Vous aimerez peut-être aussi