This commit is contained in:
Luca Conte 2023-03-29 23:30:56 +02:00
parent 00f13f2269
commit 8240e68ed0
2 changed files with 80 additions and 0 deletions

69
u05/src/Loc.java Normal file
View File

@ -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);
}
}

11
u05/src/Test.java Normal file
View File

@ -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));
}
}