Devoir java:
Exercie1:
1et2:
public class book {
String titre;
String auteur;
int prix;
public String gettitre()
{
return titre;
}
public String getauteur()
{
return auteur;
}
public int getprix()
{
return prix;
}
public void settitre(String titre)
{
this.titre=titre;
}
public void setauteur(String auteur)
{
this.auteur=auteur;
}
public void setprix(int prix)
{
this.prix=prix;
}
public static void main(String[] args) {
book b1=new book();
b1.settitre("les meserables");
b1.setauteur("vector hergo");
b1.setprix(80);
System.out.println(b1.gettitre()+ "\n"+b1.getauteur()+"\n"+ b1.getprix());
}}
3-
public class book {
String titre;
String auteur;
int prix;
public book(String titre,String auteur,int prix)
{
this.titre=titre;
this.auteur=auteur;
this.prix=prix;
}
public static void main(String[] args) {
book b1=new book("LE PRINCE","NOUHAILA CHARAF",200);
book b2=new book("LA POMME","VECTOR HEGOF",100);
}}
4-
public class book {
String titre;
String auteur;
int prix;
public book(String titre,String auteur,int prix)
{
this.titre=titre;
this.auteur=auteur;
this.prix=prix;
}
public void affiche(){
System.out.println(titre +"\n"+auteur +"\n" +prix);
}
public static void main(String[] args) {
book b1=new book("LE PRINCE","NOUHAILA CHARAF",200);
book b2=new book("LA POMME","VECTOR HEGOF",100);
b1.affiche();
b2.affiche();
}}
5-
public class test {
public static void main(String[] args) {
book[] livres = new book[3];
Scanner sc = null;
Object book;
for (int i = 0; i < livres.length; i++) {
sc = new Scanner(System.in);
System.out.print("Donner le titre du livre n° " + (i + 1) + " :");
String titre = sc.nextLine();
System.out.print("Donner l'auteur du livre n° " + (i + 1) + " :");
String auteur = sc.nextLine();
System.out.print("Donner le prix du livre n° " + (i + 1) + " :");
int prix = sc.nextInt();
book[i] = new book(titre, auteur, prix);}
sc.close();
for (book l :book)
System.out.println(l);
System.out.println("Le nombre de livres est " + book.count);
}}
Exercice2 :
1&2 :
package employe;
import java.util.Scanner;
public class employe {
String matricule,nom,prenom;
long AnneNaiss,AnneEmbauche;
int salaire;
public String getmatricule()
{
return matricule;
}
public String getnom()
{
return nom;
}
public String getprenom()
{
return prenom;
}
public long getAnneNaiss()
{
return AnneNaiss;
}
public long getAnneEmbauche()
{
return AnneEmbauche;
}
public int getsalaire()
{
return salaire;
}
public void setmatricule(String matricule)
{
this.matricule=matricule;
}
public void setnom(String nom)
{
this.nom=nom;
}
public void setprenom(String prenom)
{
this.prenom=prenom;
}
public void setAnneNaiss(long AnneNaiss)
{
this.AnneNaiss= AnneNaiss;
}
public void setAnneEmbauche(long AnneEmbauche)
{
this.AnneEmbauche=AnneEmbauche;
}
public void setsalaire(int salaire)
{
this.salaire=salaire;
}
public static void main(String[] args) {
employe E1=new employe();
E1.setmatricule("dacias");
E1.setnom("chacha");
E1.setprenom("ahmed");
E1.setAnneNaiss(1977);
E1.setAnneEmbauche(2009);
E1.setsalaire(20384);
System .out.println("les information de l'employe est:\n");
System .out.println(E1.getmatricule()+"\n" +E1.getnom()+ "\n"+E1.getprenom()+"\n"
+E1.getAnneNaiss()+"\n"+E1.getAnneEmbauche()+"\n"+E1.getsalaire());}}
3&4&5&6&7:
package employe;
import java.util.Scanner;
public class employe {
String matricule,nom,prenom;
long AnneNaiss,AnneEmbauche;
int age,Anciennete,salaire,nvsalaire;
public int getage()
{
return age;
}
public int getAnciennete() {
return Anciennete;
}
public void AugmentationDuSalaire(int Anciennete) {
if(Anciennete<5) {
nvsalaire = (int)(salaire +salaire*0.02);
System.out.println("le nv salaire est: "+nvsalaire);
}
else if (Anciennete>5 && Anciennete<10) {
nvsalaire =(int)(salaire+salaire*0.05);
System.out.println("le nv salaire est: "+nvsalaire);
}
else if(Anciennete>=10)
nvsalaire =(int)(salaire+salaire*0.1);
System.out.println("le nv salaire est: " +nvsalaire);
}
public void afficheremploye()
{
System.out.println("les information de l'employe est:\n");
System.out.println("la matricule est: "+matricule );
System.out.println("le nom et prenom et l'age est: "+nom +prenom +" "+age);
System.out.println("l'anneNaissance est: "+AnneNaiss);
System.out.println("l'annee d'embauche est: " +AnneEmbauche);
System.out.println("le nombre d'anciennete est: "+ Anciennete);
System.out.println("le salaire est: "+salaire);
public employe(String matricule,String nom,String prenom,long AnneNaiss,long
AnneEmbauche,int salaire,int Anciennete,int age)
{
this.matricule=matricule;
this.nom=nom;
this.prenom=prenom;
this.AnneNaiss=AnneNaiss;
this.AnneEmbauche=AnneEmbauche;
this.salaire=salaire;
this.Anciennete=Anciennete;
this.age=age;
public static void main(String[] args) {
employe E1=new
employe("dacias","chacha","ahmed",1977,2010,2000,10,30);
E1.afficheremploye();
E1.AugmentationDuSalaire(E1.Anciennete);}}
8-
Exercice3:
public class document {
String titre;
int numero;
static int numerosuivant=0;
public document(String titre) {
this.numero = numerosuivant;
numerosuivant++;
this.titre = titre;
}
public document(String titre, int numero) {
if (numero < numerosuivant) {
numero = numerosuivant;
}
this.numero = numero;
numerosuivant = numero + 1;
this.titre = titre;
}
public int getNumero(){
return numero;
}
public String getTitre(){
return titre;
}
public String toString() {
return ("numero: "+numero+" titre: "+titre);
}
public static void main(String[] args) {
document d1 =new document("abdjhbjhd",1);
document d2 =new document("abdjhbjhd",2);
document d3 =new document("abdjhbjhd",3);
document d4 =new document("abdjhbjhd",4);
document d5 =new document("abdjhbjhd",5);
System.out.println(d1.toString() +"\n"+d2.toString() +"\n"+ d3.toString());
Exercice 4 :
//Fichier Figure.java
import java.util.Vector ;
public class Figure {
private static Vector instances = new Vector();
private int abscisse;
private int ordonnee;
private int couleur;
public Figure( int abscisse , int ordonnee , int couleur ) {
this.abscisse = abscisse;
this.ordonnee = ordonnee;
this.couleur = couleur;
instances.add(this);
}
public static Vector getInstances() { return instances ; }
public String toString(){
return (abscisse+" "+ordonnee+" "+couleur);
}
}
public class Carre extends Figure {
private int cote ;
public Carre(int abscisse, int ordonnee, int couleur, int cote) {
super(abscisse,ordonnee,couleur);
this.cote = cote;
}
public static Vector getInstances() {
int nCarre = 0;
Vector instancesCarre = new Vector();
Vector instances = Figure.getInstances();
Enumeration e = instances.elements();
Figure uneFigure;
while(e.hasMoreElements()) {
uneFigure = (Figure)e.nextElement();
if (uneFigure instanceof Carre)
instancesCarre.add(uneFigure);
}
return instancesCarre;
}
public String toString(){
return (super.toString()+" "+cote);
}
}
//Fichier Test.java
import java.util.Vector ;
import java.util.Enumeration ;
public class Test {
public static void main(String[] argv) {
Figure f1 = new Figure(1,1,1);
Figure f2 = new Figure(2,2,2);
Carre c3 = new Carre(3,3,3,3);
Figure f4 = new Figure(4,4,4);
Carre c5 = new Carre(5,5,5,5);
5System.out.println("Liste des figures") ;
Enumeration e = Figure.getInstances().elements() ;
while(e.hasMoreElements()){
System.out.println(e.nextElement()) ;
}
System.out.println("Liste des carres") ;
e = Carre.getInstances().elements() ;
while(e.hasMoreElements()){
System.out.println(e.nextElement()) ;
}
}
}