Vous êtes sur la page 1sur 5

Correction de TP 1 en Java

FSO – 2020

Exercice 2

class Point {
private double x, y, z;
public Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double calculer() {
return x+y+z;
}

}
public class Exercie2 {
public static void main(String[] args) {
Point A = new Point(5,2,3);
System.out.println("La somme est : " +
A.calculer());
}

Exercice 3

import java.util.Scanner;
class Etudiant {
private String nom, prenom, cne;
private double note1, note2, note3;
public Etudiant(String nom, String prenom) {
this.nom = nom;
this.prenom = prenom;
}
public Etudiant(String nom, String prenom, String cne) {
this(nom, prenom);
this.cne = cne;
}
public Etudiant(Etudiant A) {
this.nom = A.nom;
this.prenom = A.prenom;
this.cne = A.cne;
}
public Etudiant(String nom, String prenom, String cne,
double note1,double note2,double note3) {
this(nom, prenom,cne);
this.note1 = note1;
this.note2 = note2;
this.note3 = note3;
}
public void afficher() {
System.out.println("Nom: " + nom + ", prenom: " +
prenom + " et cne: "+ cne);
}
public double moyenne() {
return (note1+note2+note3)/3;
}
}

public class TestEtudiant {


public static void main(String[] args) {
Etudiant A = new Etudiant("Farid","Hafid");
Etudiant B = new Etudiant("Farid","Hasnae",
"F1111");
Etudiant C = new Etudiant(B);
A.afficher();
B.afficher();
C.afficher();
Scanner s = new Scanner(System.in);
System.out.println("Entrer les trois notes");
double n1 = s.nextDouble();
double n2 = s.nextDouble();
double n3 = s.nextDouble();
Etudiant D = new
Etudiant("Salim","Asmae","t1234",n1,n2,n3);
System.out.println("La moyenne est: "+
D.moyenne());
s.close();
}
}
Exercice 4

import java.util.Scanner;
public class Cercle {
private double rayon, x, y;
public Cercle(double x, double y, double rayon) {
this.x = x;
this.y = y;
this.rayon = rayon;
}
public void deplacer(double dx, double dy){
x += dx ;
y += dy ;
}
public void setRayon(double a){
rayon = a ;
}
public double getRayon(){
return rayon;
}
public void afficher() {
System.out.println("Un cercle de centre le point de
coordonnées "+ x + " et " + y + " et de rayon "+
rayon );
}
}

public class TestCercle {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Entrer x ");
double x = sc.nextDouble();
System.out.println("Entrer y ");
double y = sc.nextDouble();
System.out.println("Entrer le rayon ");
double z = sc.nextDouble();
Cercle A = new Cercle(x,y,z);
A.afficher();
System.out.println("Entrer le nouveau rayon ");
double r = sc.nextDouble();
A.setRayon(r) ;
A.afficher();
A.deplacer(2,2) ;
A.afficher();
sc.close();
}

Exercice 5

import java.util.Scanner;
class Point {
private double x, y;
public Point(double x, double y){
this.x = x;
this.y = y;
}
public void afficher() {
System.out.println("Un point de coordonnées "+ x + "
et " + y);
}
}
class Cercle {
private double rayon;
private Point a;
public Cercle(double rayon, Point a) {
this.rayon = rayon;
this.a = a;
}
public void afficher() {
a.afficher();
System.out.println(" est le centre d'un cercle de
rayon "+ rayon );
}
}

public class Test {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Entrer x ");
double x = sc.nextDouble();
System.out.println("Entrer y ");
double y = sc.nextDouble();
Point A = new Point(x,y);
System.out.println("Entrer le rayon ");
double r = sc.nextDouble();
Cercle C = new Cercle(r,A);
C.afficher();
sc.close();
}

Vous aimerez peut-être aussi