u05 update
This commit is contained in:
parent
4c3398e363
commit
b3a1dac141
|
@ -1,28 +1,29 @@
|
|||
/**
|
||||
* Klasse zum Abspeichern eines Ortes mittels x und y Koordinate
|
||||
*/
|
||||
package de.hsh.prog.v02;
|
||||
|
||||
import java.awt.Graphics;
|
||||
|
||||
/** Klasseninvariante: x>=0 und y>=0. */
|
||||
|
||||
public class Loc {
|
||||
private int x;
|
||||
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
|
||||
* x und y werden auf 0 initialisiert
|
||||
* Initialisiert das Objekt mit den Koordinaten x=0, y=0
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
@ -38,28 +39,57 @@ public class Loc {
|
|||
}
|
||||
|
||||
/**
|
||||
* Setter für x Wert
|
||||
* Zeichnet den Punkt auf einer Graphic g mit Angabe der Koordinaten
|
||||
*/
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
public void draw(Graphics g) {
|
||||
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) {
|
||||
this.y = y;
|
||||
public void translate(int dx, int dy) {
|
||||
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) {
|
||||
int dx = this.x - l.x;
|
||||
int dy = this.y - l.y;
|
||||
public double distance(Loc other) {
|
||||
int dx= x - other.x;
|
||||
int dy= y - other.y;
|
||||
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
|
||||
*/
|
||||
|
@ -67,3 +97,5 @@ public class Loc {
|
|||
return Math.abs(this.x - l.x) + Math.abs(this.y - l.y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue