Mohammed Ezzaim
Edition 1 cours JAVA et OPP
Cours java formation
sir(FSTM)
Chapitre 1:
NOTION DE BASE:
NOTION DE BASE:
premier programme
Public class Main() {
Possible
Public static void main(String [] args) { decrier des
mots de type
[Link](“HELLO WORD”); string
}
}
Variable
[Link]
Min=-128
Max=127
Public class Main() {
b=-128
Public static void main(String [] args) {
int b=127; concatination
b++;
[Link](“b=”+b);
}
}
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
Exercice:
Public class mohammed{
Public static void main(String [] args){
Int valeurs =316;// 100111100 (binaire)
[Link](“valeur=”+valeurs);
Long vl=valeurs;//100111100
Short vs=(short)valeurs;//100111100
Byte b=(byte)valeur;//00111100
valeurs=316
}} 8 bite Vl=316
60)2= 00111100 Vs=316
b=60
2^8 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
256 128 64 32 16 8 4 2 1
1 0 0 1 1 1 1 0 0
0 0 1 1 1 1 0 0
316)2=100111100
60)2=00111100
Exercice de base:
Hello word khalid
hello word
public class Moha{
public static void main(String[] args){
String name="khalid";
[Link]("hello word"+name);
[Link]("\nhello word"); }
}
public class Moha{
public static void main(String[] args){
byte b;
short s=2;
int i=2; Byte=2
long l=2; Short=2
Int =2
float f=2; Long=2
double d=2; Float=2
Double=2
[Link]("byte="+b);
[Link]("short="+s);
[Link]("int="+i);
[Link]("long="+l);
[Link]("float="+f);
[Link]("double="+d);
}
}
public class Moha{
public static void main(String[] args){
Double z=-17.754;
Double absz=[Link](z);
abs=17.754
[Link](“abs=”+absz); rh=20
float rh=19.754
rh=[Link](rh);//9ima te9ribiya
[Link](“rh=”+rh);
}}
public class moha{
static boolean b3;
public static void main(String[] args){
boolean b1=true;
boolean b2=false;
b2=false
[Link]("b1="+b1); b3=false
[Link]("b2="+b2); b4=false
b5=true
[Link]("b3="+b3);
boolean b4=!b1;
[Link]("b4="+b4);
boolean b5=9>8;
[Link]("b5="+b5); }}
public class moha{ public static void
main(String[] args){
char c1='h';
char c2='_'; C1=h
char c3='u'; C2=_
C3=u
Nb=@
[Link]("c1="+c1);
[Link]("c2="+c2);
[Link]("c3="+c3);
char nb='\u0040'; //code acii
[Link]("nb="+nb); }}
public class moha
{ public static void main(String[] args){
int t[]={3,10,4,7,9};
[Link]("t="+[Link]); dimention t=5
Chars=c
char chars[]={'A','O','C'};
[Link]("chars="+chars[2]);
}}
import [Link];
public class moha{
public static void main(String[] args){
int t[]={3,10,4,7,-1};
[Link](t); // en arab farze comme les trie
[Link](t[0]);
[Link](t[1]);
[Link](t[2]);
[Link](t[3]);
-1 3 4 7 10
[Link](t[4]); ABCD
char c[]={'A','C','B','D'};
[Link](c);
[Link](c[0]);
[Link](c[1]);
[Link](c[2]);
[Link](c[3]); }}
public class Main{
public static void main(String[] args){
int n=10;
[Link](n++);
[Link](++n); 10
char c='A'; 12
B
c++; 0
[Link](c); 1
int a=10,b=5,r;
r=a>b?0:1;//?=alors et :=sino
[Link](r);
a=1;
b=5;
r=a>b?0:1;
[Link](r);
}
}
import [Link];
public class Main {
public static void main(String[] args) {
Scanner ez =new Scanner([Link]);
[Link]("n=");
int n=[Link](); n=2
you are from
switch(n){ tunisian
case 0:
[Link]("you are from morocco");
break;
case 1:// si n==1
[Link]("you are from amercane");
break;
case 2:
[Link]("you are from tunisian");
break;
case 3:
[Link]("you are from algerian");
break;
default:
[Link]("you are nvier nationalite");
break;
}
}
}
Break et continue:
1)Break
//Sorte le boucle
public class Main {
public static void main(String[] args) {
// foreach
int t[]={5,4,7,1,2}; number=5
number=4
number=7
for(int number:t){
if(number==1)
break;
[Link]("number="+number);
}
}
2)Continue
public class Main {
public static void main(String[] args) {
// foreach
int t[]={5,4,7,1,2}; number=5
number=4
number=7
for(int number:t){ number=2
if(number==1)
continue;
[Link]("number="+number);
}
}
Class String:
public class Main {
public static void main(String[] args) {
String charac="my name is mohammed ezzaim i from morrocco";
[Link](charac);
String v=new String("this another string");
my name is mohammed
[Link](v); ezzaim i from morrocco
String v2="My name is ";
String v3="Khalid i am "; this another string
String v4=v2+v3+ 93 +" years old.";
My name is Khalid i am 93
[Link](v4);
years old.
char t[]={'K','h','a','l','i','d'};
String cc=new String(t); Khalid
[Link](cc);
char q[]=[Link](); Khalid
for(char c:q){
[Link](c+" "); } Tathwel tableau
} deyal caractere
}
public class Main {
public static void main(String[] args) {
String str1="bye";
String str2="Hi";
[Link]("str1="+str1);
[Link]("str2="+str2);
String str3=str1+" "+ str2; str1=bye
[Link]("str3="+str3); str2=Hi
String str4="bye Hi"; str3=bye Hi
[Link]("str4="+str4); str4=bye Hi
if(str3==str4){ str3!=str4
[Link]("str3==str4");
}
else
[Link]("str3!=str4");
// str3 ma3ndouch belasa fe memery y3ni ma3ndouch adress mais str4 3ando
adress wahd nota comparaison tatkon mabinat adress machi contoni
}
}
public class Main {
public static void main(String[] args) {
[Link]("Hello, World!");
//[Link]("Hello, World!");
int _mn;
Hello, World!
_mn=20; 20
[Link](_mn); 15
Exception in thread
int $jg; "main"
$jg=15;
[Link]( $jg);
// la yomk ane tabd2 bi ba9i romoze
byte !iy=2;
[Link](*iy);
}
}
import [Link];
public class Main {
enter 1er nombre: 1
enter 2 eme nobre:1
public static void main(String args[]) { la somme :2
la somme :11
Scanner val=new Scanner([Link]);
[Link](" enter 1er nombre: ");
int x=[Link]();
[Link](" enter 2 eme nobre:");
int y=[Link]();
[Link]("la somme :"+(x+y));
[Link]("la somme :"+x+y);
}
}
public class Main {
public static void main(String args[]) {
int n=10;
[Link](n++);
[Link](","+n);
10,11
int x=10; 11,11
[Link](++x);
[Link](","+x);
}
}
public class Main {
public static void main(String args[]) {
int n=10;
[Link](n--);
[Link](","+n); 10,9
9,9
int x=10;
[Link](--x);
[Link](","+x);
}
}
import [Link];
public class Main {
public static void main(String args[]) {
Scanner ez=new Scanner([Link]); entre 1er nombre x=2
entre 2eme nombre y=4
int x,y,m; le contenue de x est x=4
[Link]("entre 1er nombre x="); le contenue de y est
y=2
x=[Link]();
[Link]("entre 2eme nombre y=");
y=[Link]();
m=x;
x=y;
y=m;
[Link]("le contenue de x est x="+x);
[Link]("le contenue de y est y="+y);
}
}
import [Link];
public class Main {
public static void main(String[] args) {
Scanner ez=new Scanner([Link]);
int n1,n2,n3;
[Link]("n1=");
n1=[Link]();
[Link]("n2="); n1=11
n2=[Link](); n2=0
[Link]("n3="); n3=1
n3=[Link](); le maximum est:11
int max;
max=n1;
if(max<n2){
max=n2;
}
if(max<n3){
max=n3;
}
[Link]("le maximum est:"+max);
}
}
import [Link];
public class Main {
public static void main(String[] args) {
Scanner ez=new Scanner([Link]);
int n1,n2;
[Link]("n1=");
n1=[Link]();
[Link]("n2=");
n2=[Link]();
int nbr;
[Link]("***************calculatrice**************")
;
[Link]("cliquer 1 pour adittion .");
[Link]("cliquer 2 pour multiplication. ");
[Link]("cliquer 3 pour soustraction. ");
[Link]("cliquer 4 pour difffision. ");
[Link]("entre le nombre nbr= ");
nbr=[Link]();
switch(nbr){
case 1:[Link]("n1+n2="+(n1+n2));
break;
case 2:[Link]("n1*n2="+(n1*n2));
break;
case 3:[Link]("n1-n2="+(n1-n2));
break;
case 4:[Link]("n1/n2="+(n1/n2));
break;
default : [Link]("le nbr choisir incorect");
break;
}
}
}
n1=5
n2=5
***************calculatrice**************
cliquer 1 pour adittion .
cliquer 2 pour multiplication.
cliquer 3 pour soustraction.
cliquer 4 pour difffision.
entre le nombre nbr= 2
n1*n2=25
Deference entre while et do while
import [Link];
public class HelloWorld {
public static void main(String[] args) {
Scanner ez=new Scanner([Link]);
int n1;
n1=2;
while(n1>3){ affichage de *do while* 2
[Link](n1);
}
do{
[Link](n1);
}while(n1>3);
}
}
import [Link];
public class Main {
public static void main(String[] args) {
Scanner ez=new Scanner([Link]);
int n;
[Link]("enter les series des nombre puis en affiche le maximum:");
int max;
int i=1;
[Link]("n"+i+"=");
n=[Link]();
max=n; enter les series des nombre puis
while(n!=0){ en affiche le maximum:
i++; n1=-7
[Link]("n"+i+"="); n2=-6
n=[Link](); n3=-3
if(max<=n){ n4=0
max=n; le maximum est est 0
}
}
[Link]("le maximum est est "+max);
}
}
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
Scanner ez=new Scanner([Link]); enterz un nombre:5
Random r=new Random(); notre nombre est plus
int nbraliatoire;
grand
int nbrenter;
nbraliatoire=[Link](11);// nbre mabine 0 we 10
int temp=0; enterz un nombre:4
do{ Bravo vous avez trover le
[Link]("enterz un nombre:"); nombre en 2 tentatives
nbrenter=[Link]();
if(nbrenter==nbraliatoire){
temp++;
[Link]("Bravo vous avez trover le nombre en "+temp+" tentatives");
}
else if(nbrenter<nbraliatoire){
[Link]("notre nombre est plus petite\n");
temp++;
}
else{
[Link]("notre nombre est plus grand\n");
temp++;
}
}while(nbrenter!=nbraliatoire);
}}
public class Main {
public static int x,y;
public static void main(String[] args) {
x=5
lire(); y=4
adition(x,y); la somme est 9
la defference est 1
soustraction(x,y);
le produit est 20
multiplication(x,y);
}
}
//****************************************************
public static void adition(int x,int y){
int somme=x+y;
[Link]("la somme est "+somme);
}
//*****************************************************
public static void soustraction(int x,int y){
int somme=x-y;
[Link]("la defference est "+somme);
}
//****************************************************
public static void multiplication(int x,int y){
int somme=x*y;
[Link]("le produit est "+somme);
}
//*****************************************************
public static void lire(){
Scanner ez=new Scanner([Link]);
[Link]("x=");
x=[Link]();
[Link]("y=");
y=[Link]();
}
//**********************************************************
}
Surcharge de methode:
Plusieur des methode de meme nom :
• Nbr variable defferent
• Type de reture defferent
import [Link];
public class Main{
public static void main(String [] args){
int x=addition(4,5,1); 10
[Link](x); 9.0
}
public static int addition(int n1,int n2){
int somme=n1+n2;
return somme;
}
public static double addition(double n1,double n2){
double somme=n1+n2;
return somme;
}
public static int addition(int n1,int n2,int n3){
int somme=n1+n2+n3;
return somme;
}
}