0% ont trouvé ce document utile (0 vote)
91 vues9 pages

Exemples de code Java pour exercices pratiques

Le document décrit plusieurs exercices de programmation en Java avec des classes et méthodes. Les exercices couvrent des sujets comme la chaîne de caractères, les tableaux, les dates et les magasins.

Transféré par

bannouromar54
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats DOCX, PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
91 vues9 pages

Exemples de code Java pour exercices pratiques

Le document décrit plusieurs exercices de programmation en Java avec des classes et méthodes. Les exercices couvrent des sujets comme la chaîne de caractères, les tableaux, les dates et les magasins.

Transféré par

bannouromar54
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats DOCX, PDF, TXT ou lisez en ligne sur Scribd

Compte rendu java N:2

TP2
Realiser par :omar bannour L2 TIC_TD1_TP1

Le code Main:
Exercice 1:
Le code:
package Ex1;
import java.util.Scanner;
public class TraitmementChaine {
String ch;
public TraitmementChaine(String ch) {
this.ch = ch;
}
public String getCh() {
return ch;
}
void LireChaine() {
Scanner input = new Scanner(System.in);
System.out.println("donner une chaine: ");
ch = input.nextLine();
}
public int compterNbreCarateres() {
return ch.length();
}
Resultat:
public int compterLettreMajuscule() {
int compteur = 0;
for (int i = 0; i < ch.length(); i++) {
char c= ch.charAt(i);
if (Character.isUpperCase(c)) {
compteur++;
}
}
return compteur;
}
public void verifierMot(String Mot){
int position = ch.indexOf(Mot);
if (position == -1){
System.out.println( "le mot n'existe pas");
}
else
System.out.println("le mot existe");
}
public void Nbroccurence(char caratere){
int compteur = 0;
for (int i = 0; i<ch.length();i++ ){
if (ch.charAt(i)==caratere){
compteur ++;
System.out.println( "le nombre d occurence est:" + compteur);

}
}

}
}

Exercice 2 : Le code Main:


Le code :
package EX3;
public class tableauStatistique {
int[] donnees;
public tableauStatistique(int[] donnees) {
this.donnees = donnees;
}
void calculPairImpaire() {
int nombrepaire = 0;
int nombreimpaire = 0;
for (int nb: donnees) {
if (nb% 2 == 0) {
nombrepaire ++;
}
else{
nombreimpaire++;}}
System.out.println("les nombres paires dans le tabLeau egale a : " +nombrepaire );
System.out.println("les nombres impaires dans le tabLeau egale a : " +nombreimpaire );
}
public int calculPGCD(int n1, int n2) {
int pgcd = 0;
for (int i = 1; i <= n1 && i <= n2; i++) {
if (n1 % i == 0 && n2 % i == 0) { Le résultat:
pgcd = i;
}}
return pgcd;
}
void verifepgcd(){
int pgcd = donnees[0];
for (int i=1 ; i<donnees.length; i++){
pgcd = calculPGCD( pgcd , donnees[i]);
}
System.out.println("le pgcd est :" + pgcd);}
public void trouverMinMax() {
int min = donnees[0];
int max = donnees[0];
for (int i = 1; i < donnees.length; i++) {
if (donnees[i] <min) {
min= donnees[i];
}
if (donnees[i]>max){
max= donnees[i];}}
System.out.println("le minimum dans le tablau est de ableau est:" + min);
System.out.println("le maximum de tableau est:" + max);
}
}

Exercice3:
Le code :
package Ex2;
public class Personne {
String nom; Main code:
int age;
char sexe;
public Personne(String nom , int
age , char sexe) {
this.nom = nom;
this.age = age;
this.sexe = sexe;
}
public String getNom() {
return nom;
}
public void setNom(String nom)
{
this.nom = nom;
} Resulat:

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}

public char getSexe() {


return sexe;
}

public void setSexe(char sexe) {


this.sexe = sexe;
}
public void estMajeur(){
if (age >= 18){
System.out.println("il est majeur");
}
else
System.out.println("n'est pas majeur");
}
public void afficheDetaills(){
System.out.println("le nom est :" +nom );
System.out.println("le age est :" +age );
System.out.println("le sexe est :" +sexe);
estMajeur();

}
public void souhaiterAnniversaire(){
age =age+1;
System.out.println("happy birthady tu est maintenat :" + age);

}
public void saluer(){

if (sexe=='m') {
System.out.println("bonjour monseiur");
}
else
System.out.println("bonjour madame");
}

}
Exercice 4:
Le code:
package EX4;
public class Etudiant {
String nom;
Double moyenne;
String matierePrefree;
public Etudiant(String nom ,Double moyenne,String matierePrefree){
this.nom=nom; Le code Main:
this.moyenne=moyenne;
this.matierePrefree=matierePrefree;
}
public String getNom(){
return nom;
}
public void setNom(String nom){
this.nom=nom;
}
public Double getMoyenne() {
return moyenne;
}
public void setMoyenne(Double moyenne) {
this.moyenne = moyenne;
}
public String getMatierePrefree() { Resultat:
return matierePrefree;
}
public void setMatierePrefree(String matierePrefree)
{
this.matierePrefree = matierePrefree;
}
public void mention(){
if (moyenne>= 16){
System.out.println("mention trés bien");}
else if (moyenne<16 && moyenne >=14) {
System.out.println("mention bien");}
else if ( moyenne<14 && moyenne>=12) {
System.out.println("mention assez bien");}
else if ( moyenne<12 && moyenne>=10 ){
System.out.println("mention passable");}
else
System.out.println("insuffisante");}
public void afficheDetaills(){
System.out.println("le nom de l'etudiant est :" + nom);
System.out.println("le moyenne de l'etudiant est :" + moyenne);
System.out.println("le matiere prefere de l'etudiant est :" + matierePrefree);
mention();
}
public void changerMatierePreferee(String matierePrefree2){
matierePrefree = matierePrefree2;
System.out.println("votre nouvelle matiere preferee est:" +matierePrefree2);
}
public void ajouterNote(double note){
moyenne =(moyenne +note)/2;
System.out.println("le noveau moyenne est:" +moyenne);}
}
Exercice 5:
Le code:
package EX5;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;

public class JavaDate {


int jour;
int mois;
int annes;

public JavaDate(int jour, int mois, int annes) { Le code Main


this.jour = jour;
this.mois = mois;
this.annes = annes;
}

public int getJour() {


return jour;
}

public void setJour(int jour) {


this.jour = jour;
}

public int getMois() {


return mois;
}

public void setMois(int mois) {


this.mois = mois;
}

public int getAnnes() {


return annes;
}

public void setAnnes(int annes) {


this.annes = annes;
}

public void saisirDate() {


Scanner input = new Scanner(System.in);
System.out.println("donner le jour:");
int jour = input.nextInt();
if (jour >= 1 && jour <= 31) {
System.out.println("le jour est:" + jour);
}
System.out.println("donner le mois:");
int mois = input.nextInt();
if (mois >= 1 && mois <= 12) {
System.out.println("le mois est:" + mois);
}
System.out.println("votre date est validé");

public boolean Biss() {

return ((annes % 4 == 0 && annes % 100 != 0)


|| (annes % 400 == 0));

public int nbJoursMois() {


switch (mois) {
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return Biss() ? 29 : 28;
default:
return 31;
}
}

public int distance(JavaDate d) {


LocalDate date1 = LocalDate.of(annes, mois, jour);
LocalDate date2 = LocalDate.of(d.getAnnes(), d.getMois(), d.getJour());
return (int) ChronoUnit.DAYS.between(date1, date2);
}
public int compareTo(JavaDate d) {
LocalDate date1 = LocalDate.of(annes, mois, jour);
LocalDate date2 = LocalDate.of(d.getAnnes(), d.getMois(), d.getJour());
return date1.compareTo(date2);
}
public String StringTo(){

return jour + "/" + mois + "/" + annes;

}
Le code Main
Exercice 6:
Le code:
package EX6;

import java.util.Arrays;

public class Magasin {


String nom;
int nombreEmplyer;
String[] produits;
public Magasin ( String
nom ,int nombreEmplyer , String[] produits ){
this. nom=nom;
this. nombreEmplyer=nombreEmplyer; Le résultat:
this. produits=produits;
}
public String getNom(){
return nom;
}
public void setNom(String nom){
this.nom=nom;
}

public int getNombreEmplyer() {


return nombreEmplyer;
}

public void setNombreEmplyer(int nombreEmplyer) {


this.nombreEmplyer = nombreEmplyer;
}

public String[] getProduits() {


return produits;
}

public void setProduits(String[] produits) {


this.produits = produits;
}
public void afficherStatusMagasin(){
if (nombreEmplyer > 10){
System.out.println("Prospére");
}
else
System.out.println("En difficulté");
}
public void afficherListeProduits(){

for ( String produit : produits){


System.out.println(produit);
}
}
public String[] ajouterProduits(String produit2){
String[] tableau2 = Arrays.copyOf(produits , produits.length +1);
tableau2[produits.length]=produit2;
return tableau2;
}
public String[] supprimerProduit(String nomProduit){
int index =-1;
for (int i=0 ;i< produits.length ;i++){
if (produits[i].equals(nomProduit)){
index = i;
break;
}
}
if (index != -1){
String[] tab2 = new String[produits.length-1];
for (int i=0 ; i< index ; i++){
tab2[i] = produits[i];
}
for (int i = index+1 ; i<produits.length ;i++){
tab2[i-1] = produits[i];
}
return tab2;

}
else return produits;

Vous aimerez peut-être aussi