0% found this document useful (0 votes)
74 views

Compte-Rendu Java: Exercice 3

The document contains code examples for 5 Java exercises: 1. Inserting a value into an array at a given index and handling exceptions. 2. Defining a Date class that checks for valid dates and handles exceptions. 3. Defining Student and Note classes to calculate student averages, handling exceptions. 4. Defining a Keyboard class to get integer input from the user and handle exceptions. 5. The code shows how to use exceptions and exception handling in different contexts.

Uploaded by

Chayma Saad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Compte-Rendu Java: Exercice 3

The document contains code examples for 5 Java exercises: 1. Inserting a value into an array at a given index and handling exceptions. 2. Defining a Date class that checks for valid dates and handles exceptions. 3. Defining Student and Note classes to calculate student averages, handling exceptions. 4. Defining a Keyboard class to get integer input from the user and handle exceptions. 5. The code shows how to use exceptions and exception handling in different contexts.

Uploaded by

Chayma Saad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Chaima saad / TP10

Compte-rendu Java
Exercice 3 :
//class ex3
package ex3;
import java.util.Scanner;
import java.util.InputMismatchException;
public class ex3 {

public static void method() {


int t[]=new int[5];
t[0]=2;
t[1]=5;
t[2]=8;
t[3]=1;
t[4]=3;

try
{
System.out.println("entrer V");
Scanner vv=new Scanner(System.in);
int v=vv.nextInt();

System.out.println("entrer indice ");


Scanner ind=new Scanner(System.in);
int in=ind.nextInt();

int i;
for(i=t.length;i>in;i--)
{
t[i]=t[i-1];
}
t[in]=v;
}
catch(ArrayIndexOutOfBoundsException e)
Chaima saad / TP10

{
System.out.println("taille de tableau !:"
+e.getMessage()););
}
catch(InputMismatchException a)
{
System.out.println("il faut entrer un entier !:" +
a.getMessage());
}

}
}

//class testEx3
package ex3;
public class testEx3
{
public static void main(String[] args)
{
ex3.method();
}

Exercice 4 :
//class date
package ex4;
import java.util.InputMismatchException;
import java.util.Scanner;
public class date {
private int jour,mois,annees;
int nombrejour()
{
if(mois==1 || mois==3 || mois==5 || mois==7 ||
mois==8 || mois==10 || mois==12)
return 31;
Chaima saad / TP10

else if(mois==4 || mois==6 || mois==9 || mois==11)


return 30;
else if(mois==2)
{ if(annees%4==0)
return 29;
else
return 28;}
else return 0;

}
public date()
{
Scanner x=new Scanner(System.in);

try {
System.out.println("entrer l'année :");
int a=x.nextInt();

System.out.println("entrer le mois");
int m=x.nextInt();

System.out.println("entrer le jour");
int j=x.nextInt();

this.jour=j;
this.mois=m;
this.annees=a;

if(this.jour<0 || this.jour>this.nombrejour() ||
this.mois>12 || this.mois<0 && this.annees<0)
{
throw new DateInvalideException();
}
}
catch(DateInvalideException e)
{
System.out.println(e.getMessage());
}
catch(InputMismatchException i)
{
Chaima saad / TP10

System.out.println("Erreur: n'est pas un


entier !!");
}
}

//class DateInvalideException
package ex4;
public class DateInvalideException extends Exception{
public DateInvalideException() {
super("Date invalide");
}
public DateInvalideException(String message)
{
super(message);
}
}

//class testDate
package ex4;
public class testDate {

public static void main(String[] args) {


date d=new date();

}
Exercice 5.1 :
//class etudiant
package ex5_1;
public class etudiant {
String nom;
String prenom;
String idUnique;
Chaima saad / TP10

note notes[]=new note[3];


etudiant(String n,String p,String i,note[] not)
{
this.nom=n;
this.prenom=p;
this.idUnique=i;
this.notes=not;

public void moyenne()


{
double nb=0;
note a;
int i;
double x,moy;
double sm=0;
try {
for(i=0;i<10;i++)
{

x=notes[i].val;
nb=nb+notes[i].coff;
sm=sm+x;
}
moy= sm/nb;
System.out.println("la moyenne est :"+moy);
}
catch(NullPointerException e)
{
System.out.println(e.getMessage());
}
catch(ArithmeticException e)
{

System.out.println(e.getMessage());
}

}
}
Chaima saad / TP10

//class note
package ex5_1;
import java.util.InputMismatchException;
import java.util.Scanner;

public class note {

String en;
int coff;
double val;
note(String e,double v,int c)
{
this.en=e;
this.val=v;
this.coff=c;
}

public static note ajoutNote()


{

Scanner s=new Scanner(System.in);


System.out.println("entrer l'enseignante:");
String e=s.nextLine();

System.out.println("entrer la valeur de note :");


double v=s.nextDouble();

System.out.println("entrer la coffecient de note :");


int c=s.nextInt();

note no=new note(e,v,c);


return no;

//class testEx5_1
Chaima saad / TP10

package ex5_1;

import java.util.InputMismatchException;
import java.util.Scanner;
public class testEx5_1 {
public static void main(String[] args)
{
note notes[]=new note[10];
try {
Scanner s=new Scanner(System.in);
System.out.println("entrer le nom :");
String nom=s.nextLine();

System.out.println("entrer le prenom :");


String prenom=s.nextLine();

System.out.println("entrer l'identifiant unique :");


String id=s.nextLine();

int i;
for(i=0;i<1;i++)
{

notes[i]=note.ajoutNote();

etudiant e1=new etudiant(nom,prenom,id,notes);

e1.moyenne();
}
catch(InputMismatchException e)
{System.out.println(e.getMessage());}
}

Exercice 5.2 :
Chaima saad / TP10

//class Clavier

package ex5_2;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Clavier {
public Clavier() {

try
{
Scanner sc =new Scanner(System.in);
System.out.println("donner une ligne
d'information de type int ");
int a=sc.nextInt();

}
catch(InputMismatchException e)
{
System.out.println("ce n'est pas un
entier:"+e.getMessage());

}
catch(Exception e)

{
System.out.println("Erreur: "+e.getMessage());
}
}

}
//class testClavier
package ex5_2;
public class TestClavier {
public static void main(String[] args)
{
while(true)
{
Clavier x=new Clavier();
Chaima saad / TP10

}
}

You might also like