Vous êtes sur la page 1sur 4

TD 3:

EX 1:
1- On crée un objet nommé P, println affiche 00 car les attributs x et y n'ont
aucune valeur.
2- Le problème est que les propriétés x et y sont privées donc elles ne sont
utilisées que dans les points de classe, la solution est d'utiliser fct getter.
3- L'utilisation d'accesseurs en Java n'est pas obligatoire mais est une question
de conception et de besoins spécifiques. Il est crucial de trouver un
équilibre entre encapsulation et simplicité pour chaque classe de votre
programme.

4- public class Point {

private int x;

private int y;

5- public Point(int px, int py) {

this.x = px;

this.y = py;

}}

6 - public class Point {

private int x;

private int y;

public Point() {

this.x = 0;

this.y = 0;
}} }
Le code :

public class Point {

private int x;

private int y;

public Point(int px, int py) {

this.x = px;

this.y = py;

public Point(Point point) {

this.x = point.x;

this.y = point.y;

}}

EX2:
public class Book {

String title;

String author;

String isbn;

double price;
Book(String title, String author, String isbn,
double price) {

this.title = title;

this.author = author;

this.isbn = isbn;

this.price = price;

public String toString() {

return "Title: " + this.title + "\n" +

"Author: " + this.author + "\n" +

"ISBN: " + this.isbn + "\n" +

"Price: " + this.price;

public static void main(String[] args) {

Book book = new Book("Fundamental concepts


of computer science", "Alfred Aho", "5-2365-7989-
3", 55.5);

System.out.println(book);

}
}

EX3:
public class Ex3 {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.println("entre une chaine");
String ch=sc.nextLine();
String newCh="";
for(int i=ch.length()-1;i>=0;i--) {

newCh=newCh+ch.charAt(i);
}

System.out.println(newCh);
}

Vous aimerez peut-être aussi