u05
This commit is contained in:
parent
00f13f2269
commit
8240e68ed0
|
@ -0,0 +1,69 @@
|
||||||
|
/**
|
||||||
|
* Klasse zum Abspeichern eines Ortes mittels x und y Koordinate
|
||||||
|
*/
|
||||||
|
public class Loc {
|
||||||
|
private int x;
|
||||||
|
private int y;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor für die Loc Klasse ohne Argumente
|
||||||
|
* x und y werden auf 0 initialisiert
|
||||||
|
*/
|
||||||
|
public Loc() {
|
||||||
|
this(0,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor für die Loc Klasse mit Argumenten
|
||||||
|
* x und y werden auf die übergebenen Werte gesetzt
|
||||||
|
*/
|
||||||
|
public Loc(int x, int y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter für x Wert
|
||||||
|
*/
|
||||||
|
public int getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter für y Wert
|
||||||
|
*/
|
||||||
|
public int getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setter für x Wert
|
||||||
|
*/
|
||||||
|
public void setX(int x) {
|
||||||
|
this.x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setter für y Wert
|
||||||
|
*/
|
||||||
|
public void setY(int y) {
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gibt den Abstand (Nach Pythagoras) zwischen diesem und einem anderen Loc Objekt zurück
|
||||||
|
*/
|
||||||
|
public double distance(Loc l) {
|
||||||
|
int dx = this.x - l.x;
|
||||||
|
int dy = this.y - l.y;
|
||||||
|
return Math.sqrt(dx * dx + dy * dy);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gibt die "Manhattan-Distanz" zwischen diesem und einem anderen Loc Objekt zurück
|
||||||
|
*/
|
||||||
|
public int manhattanDistance(Loc l) {
|
||||||
|
return Math.abs(this.x - l.x) + Math.abs(this.y - l.y);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
/**
|
||||||
|
* Test Klasse für Loc Objekte
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Loc l1 = new Loc(3,5);
|
||||||
|
Loc l2 = new Loc(4,6);
|
||||||
|
System.out.println(l1.manhattanDistance(l2));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue