From 8240e68ed05769290c59e3008902e89ac083c888 Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Wed, 29 Mar 2023 23:30:56 +0200 Subject: [PATCH] u05 --- u05/src/Loc.java | 69 +++++++++++++++++++++++++++++++++++++++++++++++ u05/src/Test.java | 11 ++++++++ 2 files changed, 80 insertions(+) create mode 100644 u05/src/Loc.java create mode 100644 u05/src/Test.java diff --git a/u05/src/Loc.java b/u05/src/Loc.java new file mode 100644 index 0000000..54426aa --- /dev/null +++ b/u05/src/Loc.java @@ -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); + } +} diff --git a/u05/src/Test.java b/u05/src/Test.java new file mode 100644 index 0000000..bceda1d --- /dev/null +++ b/u05/src/Test.java @@ -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)); + } +}