Algorithmen und Programmierung II
Übungsblatt 6 - Musterlösung
Aufgabe 1
public class Eintrag {
String name;
int nummer;
Eintrag naechster;
Eintrag(String name, int nummer, Eintrag naechster) {
this.name = name;
this.nummer = nummer;
this.naechster = naechster;
}
int nummer(String name) {
Eintrag test = this;
while(test != null && name.compareTo(test.name) > 0)
test = test.naechster;
if (test == null || name.compareTo(test.name) != 0)
return 0;
return test.nummer;
}
void eintragen(String name, int nummer) {
if (name.compareTo(this.name) < 0) {
this.naechster = new Eintrag(this.name, this.nummer, naechster);
this.name = name;
this.nummer = nummer;
return;
}
Eintrag test = this, vorg = null;
while(test != null && name.compareTo(test.name) > 0) {
vorg = test;
test = test.naechster;
}
if (test != null && name.compareTo(test.name) == 0)
test.nummer = nummer;
else
vorg.naechster = new Eintrag(name, nummer, test);
}
}
Aufgabe 2
public class Weekday {
String day;
Weekday next;
Weekday(String day, Weekday next) {
this.day = day; this.next = next;
}
static Weekday current;
static {
current = new Weekday("Samstag", null);
Weekday d = new Weekday("Freitag", current);
d = new Weekday("Donnerstag", d);
d = new Weekday("Mittwoch", d);
d = new Weekday("Dienstag", d);
d = new Weekday("Montag", d);
d = new Weekday("Sonntag", d);
current.next = d;
}
static String next() {
return (current = current.next).day;
}
}
Aufgabe 3
public class Triangle {
public static void main(String[] args) {
int width = 640;
int height = 480;
DrawingWindow df = new DrawingWindow(width, height);
Point a = new Point(100, 100);
Point b = new Point(300, 100);
Point c = new Point(200, 300);
df.addLine(new Line(a, b));
df.addLine(new Line(b, c));
df.addLine(new Line(c, a));
int axstep, aystep, bxstep, bystep, cxstep, cystep;
axstep = aystep = cxstep = cystep = 2;
bxstep = bystep = 3;
while (true) {
if (a.x + axstep > width || a.x + axstep < 0) axstep = -axstep;
if (a.y + aystep > height || a.y + aystep < 0) aystep = -aystep;
if (b.x + bxstep > width || b.x + bxstep < 0) bxstep = -bxstep;
if (b.y + bystep > height || b.y + bystep < 0) bystep = -bystep;
if (c.x + cxstep > width || c.x + cxstep < 0) cxstep = -cxstep;
if (c.y + cystep > height || c.y + cystep < 0) cystep = -cystep;
a.x += axstep;
a.y += aystep;
b.x += bxstep;
b.y += bystep;
c.x += cxstep;
c.y += cystep;
df.update();
}
}
}