u05 update
This commit is contained in:
parent
4c3398e363
commit
b3a1dac141
|
@ -1,35 +1,36 @@
|
||||||
/**
|
package de.hsh.prog.v02;
|
||||||
* Klasse zum Abspeichern eines Ortes mittels x und y Koordinate
|
|
||||||
*/
|
import java.awt.Graphics;
|
||||||
|
|
||||||
|
/** Klasseninvariante: x>=0 und y>=0. */
|
||||||
|
|
||||||
public class Loc {
|
public class Loc {
|
||||||
private int x;
|
private int x;
|
||||||
private int y;
|
private int y;
|
||||||
|
|
||||||
|
/** Initialisiert das Objekt mit gegebenen Koordinatenwerten.
|
||||||
|
* @param initialX Vorbedingung: >= 0
|
||||||
|
* @param initialY Vorbedigung: >= 0.
|
||||||
|
* @exception IllegalArgumentException falls einer der Eingabeparameter ungültig ist.
|
||||||
|
*/
|
||||||
|
public Loc(int initialX, int initialY) {
|
||||||
|
setLocation(initialX, initialY);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor für die Loc Klasse ohne Argumente
|
* Initialisiert das Objekt mit den Koordinaten x=0, y=0
|
||||||
* x und y werden auf 0 initialisiert
|
|
||||||
*/
|
*/
|
||||||
public Loc() {
|
public Loc() {
|
||||||
this(0,0);
|
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
|
* Getter für x Wert
|
||||||
*/
|
*/
|
||||||
public int getX() {
|
public int getX() {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter für y Wert
|
* Getter für y Wert
|
||||||
*/
|
*/
|
||||||
|
@ -38,27 +39,56 @@ public class Loc {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setter für x Wert
|
* Zeichnet den Punkt auf einer Graphic g mit Angabe der Koordinaten
|
||||||
*/
|
*/
|
||||||
public void setX(int x) {
|
public void draw(Graphics g) {
|
||||||
this.x = x;
|
g.fillOval(x, y, 3, 3);
|
||||||
|
g.drawString("(" + x + ", " + y + ")", x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Setzt die Koordinaten auf neue Werte.
|
||||||
|
* @param newx Vorbedingung: >= 0
|
||||||
|
* @param newy Vorbedigung: >= 0.
|
||||||
|
* @exception IllegalArgumentException falls einer der Eingabeparameter ungültig ist.
|
||||||
|
*/
|
||||||
|
public void setLocation(int newx, int newy) {
|
||||||
|
if (newx < 0 || newy < 0) {
|
||||||
|
throw new IllegalArgumentException("x und y müssen >= 0 sein");
|
||||||
|
}
|
||||||
|
x= newx;
|
||||||
|
y= newy;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setter für y Wert
|
* Verschiebt den Punkt um ein gewisses delta x und y
|
||||||
*/
|
*/
|
||||||
public void setY(int y) {
|
public void translate(int dx, int dy) {
|
||||||
this.y = y;
|
setLocation(x + dx, y + dy);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gibt den Abstand (Nach Pythagoras) zwischen diesem und einem anderen Loc Objekt zurück
|
* Berechnet die Distanz zu einem weiteren Punkt
|
||||||
*/
|
*/
|
||||||
public double distance(Loc l) {
|
public double distance(Loc other) {
|
||||||
int dx = this.x - l.x;
|
int dx= x - other.x;
|
||||||
int dy = this.y - l.y;
|
int dy= y - other.y;
|
||||||
return Math.sqrt(dx * dx + dy * dy);
|
return Math.sqrt(dx * dx + dy * dy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnet die Distanz vom Nullpunkt (x=0, y=0)
|
||||||
|
*/
|
||||||
|
public double distanceFromOrigin() {
|
||||||
|
Loc origin= new Loc();
|
||||||
|
return distance(origin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gibt das Objekt als String zurück
|
||||||
|
*/
|
||||||
|
public String toString() {
|
||||||
|
return "(" + x + ", " + y + ")";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gibt die "Manhattan-Distanz" zwischen diesem und einem anderen Loc Objekt zurück
|
* Gibt die "Manhattan-Distanz" zwischen diesem und einem anderen Loc Objekt zurück
|
||||||
|
@ -67,3 +97,5 @@ public class Loc {
|
||||||
return Math.abs(this.x - l.x) + Math.abs(this.y - l.y);
|
return Math.abs(this.x - l.x) + Math.abs(this.y - l.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue