Voici les corrections pour les exercices mentionnés :
Exercice 1 : Nombres premiers
import [Link];
public class NombresPremiers {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Entrez un nombre entier : ");
int n = [Link]();
int total = 0;
[Link]("Nombres premiers entre 1 et " + n + " :");
for (int i = 2; i <= n; i++) {
if (estPremier(i)) {
[Link](i + " ");
total++;
[Link]("\nNombre total de nombres premiers : " + total);
public static boolean estPremier(int nombre) {
if (nombre < 2) return false;
for (int i = 2; i <= [Link](nombre); i++) {
if (nombre % i == 0) return false;
return true;
}
---
Exercice 2 : Capital et intérêts composés
import [Link];
public class InteretsComposes {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Entrez le capital initial : ");
double capital = [Link]();
[Link]("Entrez le taux d'intérêt annuel (en %) : ");
double taux = [Link]();
[Link]("Entrez le nombre d'années : ");
int annees = [Link]();
double[] capitaux = new double[annees];
double[] interets = new double[annees];
for (int i = 0; i < annees; i++) {
double interet = capital * taux / 100;
capital += interet;
capitaux[i] = capital;
interets[i] = interet;
}
[Link]("Année\tCapital\t\tIntérêts");
for (int i = 0; i < annees; i++) {
[Link]((i + 1) + "\t" + capitaux[i] + "\t" + interets[i]);
---
Exercice 3 : Factoriel
import [Link];
public class Factoriel {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Entrez un nombre entier : ");
int nbre = [Link]();
if (nbre < 0) {
[Link]("Le factoriel n'est pas défini pour les nombres négatifs.");
} else {
long factoriel = 1;
for (int i = 1; i <= nbre; i++) {
factoriel *= i;
[Link]("Factoriel de " + nbre + " est : " + factoriel);
}
}
---
Exercice 4 : Classe Point et cercle
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
public double distance(Point autre) {
return [Link]([Link](autre.x - this.x, 2) + [Link](autre.y - this.y, 2));
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public class Cercle {
public static void main(String[] args) {
Point A = new Point(10, 5);
Point B = new Point(12, 7);
double distance = [Link](B);
[Link]("Distance euclidienne : " + distance);
double perimetre = 2 * [Link] * distance;
double surface = [Link] * [Link](distance, 2);
[Link]("Périmètre : " + perimetre);
[Link]("Surface : " + surface);
[Link](2);
[Link](2);
[Link]("Nouvelle distance : " + [Link](B));
---
Exercice 5 : Classe Fascicule
import [Link];
class Fascicule {
private static int compteur = 0;
private int id;
private String titre, auteur;
private double prix;
public Fascicule(String titre, String auteur, double prix) {
[Link] = ++compteur;
[Link] = titre;
[Link] = auteur;
[Link] = prix;
}
public void afficher() {
[Link]("Fascicule n°" + id + " : " + titre + " de " + auteur + " - Prix : " + prix);
public class TestFascicules {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Fascicule[] fascicules = new Fascicule[3];
for (int i = 0; i < 3; i++) {
[Link]("Titre du fascicule n°" + (i + 1) + " : ");
String titre = [Link]();
[Link]("Nom de l'auteur : ");
String auteur = [Link]();
[Link]("Prix : ");
double prix = [Link]();
[Link](); // Pour consommer la nouvelle ligne
fascicules[i] = new Fascicule(titre, auteur, prix);
for (Fascicule f : fascicules) {
[Link]();
}
---
Exercice 6 : Héritage Animal
class Animal {
public void communiquer() {
[Link]("Un animal communique avec les autres.");
class Chat extends Animal {
@Override
public void communiquer() {
[Link]("Le chat miaule.");
class Chien extends Animal {
@Override
public void communiquer() {
[Link]("Le chien aboie.");
public class Test {
public static void main(String[] args) {
Animal animal = new Animal();
Animal chat = new Chat();
Animal chien = new Chien();
[Link]();
[Link]();
[Link]();
Si vous avez besoin d'explications sur un exercice ou d'une personnalisation, faites-moi savoir !