LorientobjetenlangageJAVA~Cour2
Exercice 1:
1.Ecrireleprogramme"Banque.java"permettantd'implmenterlaclasse"Banque"
2.Ecrireunprogramme"test.java"pour:
Creruncompte:1,5000.75,"AB1200"
Afficherlesinformationsdececompte
Affichersonsolde
Dposer500
Afficherlesinformationsdececompte
Affichersonsolde
Retirer200
Afficherlesinformationsdececompte
Affichersonsolde
3.Refairelaquestionprcdentedetellesortequelesinformationsducompte
serontpassesenargument
Solution:
publicclassbanque{
privateintNCompte
privatefloatSolde
privateStringCIN
publicbanque(intNCompte,floatSolde,StringCIN){
this.NCompte=NCompte
this.Solde=Solde
this.CIN=CIN}
publicintgetn(){
http://cour2.blogspot.com/2012/12/lorienteobjetenlangagejava.html
1/4
5/19/2016
LorientobjetenlangageJAVA~Cour2
returnNCompte}
publicvoiddeposer(floatSomme){
Solde=Solde+Somme}
publicvoidretirer(floatSomme){
if(Solde<Somme)
System.out.println("SOLDEINSUFFISANT")
else
Solde=SoldeSomme}
publicfloatavoirSolde(){
returnSolde}
publicStringavoirInf(){
return("Ncarte:"+CIN+"\nsolde:"+Solde+"NCompte"+NCompte)}}
publicclasstest{
publicstaticvoidmain(String[]args){
banqueb=newbanque(1,5000,"AB1200")
System.out.println(b.avoirInf())
System.out.println(b.avoirSolde())
b.deposer(500)
System.out.println(b.avoirInf())
System.out.println(b.avoirSolde())
b.retirer(7000)
System.out.println(b.avoirInf())
System.out.println(b.avoirSolde())}}
Exercice 2:
Soitlaclasseprdfiniedans"java.net"
classInetAddress{
publicstaticInetAddressgetLocalHost()
//retournel'adresseInternetdelamachinelocale
publicstaticInetAddressgetByName(String)
//retournel'adressed'unmachinepartirdesonnom
publicStringgetHostName()
//retournelenomd'unemachinepartird'uneadresseInternet
}
1Ecrireunprogramme"test.java"pourafficherl'adresseInternetetlenomd'une
machine(localeoudistante).Evoquerlecasolamachineestdonneen
argument
Solution:
importjava.net.InetAddress
publicclasstest{
publicstaticvoidmain(String[]args)throwsException{
//TODOAutogeneratedmethodstub
InetAddressadr=InetAddress.getLocalHost()
InetAddressadr2=InetAddress.getByName("iga1")
http://cour2.blogspot.com/2012/12/lorienteobjetenlangagejava.html
2/4
5/19/2016
LorientobjetenlangageJAVA~Cour2
Stringnom=adr.getHostName()
System.out.println("@delamachinelocal"+adr)
System.out.println("@delamachinea"+adr2)
System.out.println("lenomdelamachine"+adr+"est:"+nom)}}
Exercice 3 :
Soitlediagrammedeclassesd'UMLsuivant:
1.Crerlesclasses"Personne.java"et"Salarie.java",sachantquelaprimeest
calculelabasedelaformulesuivante:prime=(5*Salaire*Nombred'enfant)/100
2.Ecrireunprogramme"test1.java"pourtestervosclasses
Solution:
publicclassPersonne{
protectedintnbEnfants
protectedStringnom
publicPersonne(intnbEnfants,Stringnom){
this.nbEnfants=nbEnfants
this.nom=nom}
publicStringgetNom(){
returnnom}
publicintgetnbEnfants(){
returnnbEnfants}}
publicclassSalarieextendsPersonne{
privatefloatsalaire
publicSalarie(intnbEnfants,Stringnom,floatsalaire){
super(nbEnfants,nom)
this.salaire=salaire}
publicStringgetInf(){
return"lenom:"+super.getNom()+"lenombred'enfants:"+nbEnfants}
publicfloatgetSalaire(){
returnsalaire}
publicfloatgetPrime(){
return(5*salaire*nbEnfants)/100}}
http://cour2.blogspot.com/2012/12/lorienteobjetenlangagejava.html
3/4
5/19/2016
LorientobjetenlangageJAVA~Cour2
publicclassTest{
publicstaticvoidmain(String[]args){
//TODOAutogeneratedmethodstub
Personnep=newPersonne(5,"ali")
Salaries=newSalarie(5,"ali",15000)
Salaries1=newSalarie(2,"amal",5000)
System.out.println(s.getInf())
System.out.println("lesalaire:"+s.getSalaire())
System.out.println("laprime:"+s.getPrime())
System.out.println("laprimede:"+s1.getNom()+""+s1.getPrime())
}}
http://cour2.blogspot.com/2012/12/lorienteobjetenlangagejava.html
4/4