Exercice 6, Partie 3.

This commit is contained in:
Yohan Boujon 2023-10-19 22:30:44 +02:00
parent 02a782ad1e
commit 2b362a9d03
4 changed files with 96 additions and 0 deletions

23
src/exercice6part3.java Normal file
View file

@ -0,0 +1,23 @@
import partie6.ErrConst;
import partie6.ErrDepl;
import partie6.Point;
// TestPoint
public class exercice6part3 {
public static void main(String[] args) {
try {
Point a = new Point(Integer.parseInt(args[0]), 4);
a.affiche();
a.deplace(10,Integer.parseInt(args[1]));
a.affiche();
} catch (ErrConst e) {
System.out.println("Value of y: " + e.getOrd());
System.out.println("Value of x: " + e.getAbs());
e.printStackTrace();
System.exit(-1);
} catch (ErrDepl e) {
e.printStackTrace();
System.exit(-1);
}
}
}

36
src/partie6/ErrConst.java Normal file
View file

@ -0,0 +1,36 @@
package partie6;
public class ErrConst extends Exception {
String message;
int abs;
int ord;
ErrConst() {
this.message = "Exception thrown.";
}
ErrConst(String s) {
this.message = s;
}
ErrConst(int x, int y) {
this.abs = x;
this.ord = y;
if (x < 0)
this.message = "X can't be negative";
if (y < 0)
this.message = "Y can't be negative";
}
public int getAbs() {
return this.abs;
}
public int getOrd() {
return this.ord;
}
public String getMessage() {
return message;
}
}

13
src/partie6/ErrDepl.java Normal file
View file

@ -0,0 +1,13 @@
package partie6;
public class ErrDepl extends Exception {
String message;
ErrDepl() {
this.message = "When moving a point, the result can't be negative.";
}
public String getMessage() {
return message;
}
}

24
src/partie6/Point.java Normal file
View file

@ -0,0 +1,24 @@
package partie6;
public class Point {
public int x;
public int y;
public Point(int x, int y) throws ErrConst {
if ((x < 0) || (y < 0))
throw new ErrConst(x, y);
this.x = x;
this.y = y;
}
public void affiche() {
System.out.println("Coordonnees [X= " + this.x + "],[Y= " + this.y + "]");
}
public void deplace(int dx, int dy) throws ErrDepl {
if (((x + dx) < 0) || ((y + dy) < 0))
throw new ErrDepl();
x += dx;
y += dy;
}
}