Created
June 10, 2022 07:23
-
-
Save c-joly/77638e35c9a2052afe5b2dbbf52359b8 to your computer and use it in GitHub Desktop.
Un exemple simple de classe Point vu en cours
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Point { | |
public final Double x ; | |
public final Double y ; | |
public double getX(){ | |
return this.x ; | |
} | |
public double getY(){ | |
return this.y ; | |
} | |
public Point(final double x,final double y){ | |
this.x = x ; | |
this.y = y ; | |
} | |
public double distance(){ | |
return Math.sqrt(this.x*this.x+this.y*this.y); | |
} | |
public double distance(Point other){ | |
double dx = this.x - other.x ; | |
double dy = this.y - other.y ; | |
return Math.sqrt(dx*dx+dy*dy); | |
} | |
public static double distance(Point p1, Point p2){ | |
double dx = p1.x - p2.x ; | |
double dy = p1.y - p2.y ; | |
return Math.sqrt(dx*dx+dy*dy); | |
} | |
//public Point translate(double deltaX , double deltaY){ | |
//this.x += deltaX ; | |
// this.y += deltaY ; | |
// return this ; | |
//} | |
public String toString(){ | |
String string = "Point("; | |
string += this.x.toString()+","+String.valueOf(this.y)+")"; | |
return string ; | |
} | |
public static Point pointDiagonal(double x){ | |
return new Point(x,x); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestPoint { | |
public static void main(String[] args){ | |
Point p1,p2 ; | |
p1 = new Point(2,4); | |
System.out.println(p1); | |
p2 = new Point(2,4); | |
System.out.println(p2); | |
System.out.println(p1==p2); | |
// "==" teste l'identité entre deux objets (même emplacement mémoire) | |
// Définir equals pour tester que deux objets sont égaux | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment