• Keine Ergebnisse gefunden

TU Kaiserslautern

N/A
N/A
Protected

Academic year: 2022

Aktie "TU Kaiserslautern"

Copied!
10
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. J. O. Blech Dipl.-Inform. M. J. Gawkowski Dipl.-Inform. N. Rauch

TU Kaiserslautern

Fachbereich Informatik AG Softwaretechnik

Lösungvorschlag zum Übungsblatt 14:

Software-Entwicklung I (WS 2007/08)

Aufgabe 1 Störe meine Kreise nicht 3

Im ersten Teil dieser Aufgabe soll im wesentlichen die Notwendigkeit von abstrakten Klassen erkannt wer- den. Die verschieben Methoden werden ueberall gleich implementiert. Die draw-Methode ist ueberall vor- handen, es gibt aber keine default Implementierung. Da abstrakte Klassen in der Vorlesung nicht vorgestellt wurden, koennen Java-Klassen erstellt werden, bei denen Methoden ”leer” implementiert werden.

abstract class Figure { int x;

int y;

abstract public void draw(SEGraphics seg);

public void moveX (int x) { this.x += x;

}

public void moveY (int y) { this.y += y;

}

public void moveXY (int x, int y) { this.x += x;

this.y += y;

} }

class ClassicalFigure extends Figure { int x;

int y;

int color;

int diameter;

String name;

public ClassicalFigure(String name, int x, int y, int c, int d) { this.name = name;

this.x = x;

this.y = y;

color = c;

diameter = d;

}

public void setColor (int c) { color = c;

}

public void setDiameter (int d) { diameter = d;

}

public void draw(SEGraphics seg) { seg.drawCircle(x,y,diameter,color);

(2)

seg.drawString(name,x,y,0);

} }

class Rectangle extends Figure { int color;

int length;

int height;

String name;

public Rectangle(String name, int x, int y, int c, int l,int h) { this.name = name;

this.x = x;

this.y = y;

color = c;

length = l;

height = h;

}

public void setColor (int c) { color = c;

}

public void setLength (int l) { length = l;

}

public void setHeight (int h) { height = h;

}

public void draw(SEGraphics seg) { seg.drawLine(x,y,x+length,y,color);

seg.drawLine(x,y,x,y+height,color);

seg.drawLine(x+length,y,x+length,y+height,color);

seg.drawLine(x,y+height,x+length,y+height,color);

seg.drawString(name,x,y,0);

} }

class Square extends Rectangle {

public Square(String name, int x, int y, int c, int l) { super(name,x,y,c,l,l);

}

}

public class DrawEdit3 extends InputOutput { public static int MAXFIGURE = 20;

SEGraphics seg;

Figure[] figures;

public DrawEdit3 () { seg = new SEGraphics();

figures = new Figure[MAXFIGURE];

while(true) {

InputOutput.println("Wollen Sie 1) eine Klassische Figure Anlegen? 2) Ein Quadrat Anlegen"+

"3) Ein Rechteck Anlegen 4) eine Figure verschieben? 5) aufhören?");

switch (InputOutput.readInt()) { case 1: {

InputOutput.println("Welche Figure wollen Sie anlegen?");

int j = InputOutput.readInt();

InputOutput.println("X-Coordinate?");

int x = InputOutput.readInt();

InputOutput.println("Y-Coordinate?");

int y = InputOutput.readInt();

InputOutput.println("Farbe?");

int c = 255*InputOutput.readInt();

figures[j] = new ClassicalFigure("Figure "+j,x,y,c,10);

(3)

break; } case 2: {

InputOutput.println("Welche Figure wollen Sie anlegen?");

int j = InputOutput.readInt();

InputOutput.println("X-Coordinate?");

int x = InputOutput.readInt();

InputOutput.println("Y-Coordinate?");

int y = InputOutput.readInt();

InputOutput.println("Laenge?");

int l = InputOutput.readInt();

InputOutput.println("Farbe?");

int c = 255*InputOutput.readInt();

figures[j] = new Square("Figure "+j,x,y,c,l);

break; } case 3: {

InputOutput.println("Welche Figure wollen Sie anlegen?");

int j = InputOutput.readInt();

InputOutput.println("X-Coordinate?");

int x = InputOutput.readInt();

InputOutput.println("Y-Coordinate?");

int y = InputOutput.readInt();

InputOutput.println("Hoehe?");

int h = InputOutput.readInt();

InputOutput.println("Laenge?");

int l = InputOutput.readInt();

InputOutput.println("Farbe?");

int c = 255*InputOutput.readInt();

figures[j] = new Rectangle("Figure "+j,x,y,c,l,h);

break; } case 4: {

InputOutput.println("Welche Figure wollen Sie verschieben?");

int j = InputOutput.readInt();

InputOutput.println("rel. X-Coordinate?");

int x = InputOutput.readInt();

InputOutput.println("rel. Y-Coordinate?");

int y = InputOutput.readInt();

figures[j].moveXY(x,y);

break; }

default: return;

} draw();

}

// seg.drawCircle(20,50,10,255);

//seg.drawLine(20,50,20,80,255*255);

//seg.clear();

//seg.drawString("Hallo",20,50,255*255*255);

}

public void draw() { seg.clear();

for (int i = 0; i < MAXFIGURE;i++) { if (figures[i]!=null)

figures[i].draw(seg);

} }

public static void main (String[] args) { new DrawEdit3();

}

}

Aufgabe 2 Subtypenbildung mit Schittstellen

Main.java

// Uebungsblatt 14, Aufgabe 2

(4)

import java.util.NoSuchElementException;

// datatype expr = Const of int | Var of string | Add of expr * expr // die Variablen werden in unserer Loesung durch int-Werte modelliert interface Environment {

int lookup(int var);

void update(int var, int val);

}

class SList<T>{

T elem;

SList<T> next;

SList(T e) {elem=e; next=null;}

} // class SList<T>

class Stack<T>{

SList<T> first;

boolean is_empty(){ return first==null; } Stack<T> push(T t){

SList<T> tmp=new SList<T>(t);

if (first==null) { first=tmp;

return this;

} else {

tmp.next=first;

first=tmp;

return this;

} } // push

T top() throws Exception { if (first==null) {

throw new Exception();

} else {

return first.elem;

} } // top

Stack<T> pop() throws Exception { if (first==null) {

throw new Exception();

} else {

first=first.next;

return this;

} } // pop

class Iterator<T> { SList<T> current;

public boolean hasNext(){

return current != null;

}

public T next() {

T res = current.elem;

current = current.next;

return res;

} // next

public void remove() { first=first.next;

} // remove }

Iterator<T> createIterator(){ return new Iterator<T>(); } }

class Binding { int ident;

int value;

Binding(int i, int v){ident=i; value=v;}

}

class SimpleEnvironment implements Environment {

(5)

Stack<Binding> stack = new Stack<Binding>();

SimpleEnvironment() {};

public int lookup(int ident){

Stack<Binding>.Iterator<Binding> iter = stack.createIterator();

while(iter.hasNext()){

Binding b = (Binding)iter.next();

if (b.ident==ident) return b.value;

}

throw new NoSuchElementException();

} // lookup

public void update(int ident, int value){

stack=stack.push(new Binding(ident, value));

} // update

} // SimpleEnvironment

interface Expression {}

class Const implements Expression { int value;

Const(int v){ value = v; } }

class Var implements Expression { int ident;

Var(int v){ ident = v; } }

class Add implements Expression { Expression left;

Expression right;

Add(Expression e1, Expression e2){

left=e1;

right=e2;

} }

public class Main {

static SimpleEnvironment env = new SimpleEnvironment();

public static void main(String[] argf){ // main env.update(1,10);

env.update(2,11);

Expression e = new Add(new Const(2), new Const(4));

int res1 = eval(e,env);

} // main

public static int eval (Expression e, Environment env) { if (e instanceof Const){

return ((Const)e).value;

} else if (e instanceof Var) {

return env.lookup( ((Var)e).ident );

} else { // (e instanceof Add)

return eval(((Add)e).left,env)+eval(((Add)e).right,env);

} } // eval } // class Main

Aufgabe 3 Observer-Muster

interface Beobachter {

void aktualisiere( Subjekt s );

}

import java.util.*;

interface Subjekt {

void meldeAn(Beobachter b);

void meldeAb(Beobachter b);

(6)

void benachrichtige();

}

public class BrummiFahrer implements Beobachter, Fahrer { private int aktueller_standort;

private int bis_standort;

BrummiFahrer(int strecke, int ziel ){

aktueller_standort = strecke;

bis_standort = ziel;

}

public void aktualisiere( Subjekt s ) { return;

}

public int get_standort(){

return aktueller_standort;

}

public int get_ziel(){

return bis_standort;

}

public boolean angekommen (){

return bis_standort == aktueller_standort;

}

public void fortbewegen(int naechstmoegliches_ziel){

if( aktueller_standort < naechstmoegliches_ziel ) { aktueller_standort++;

} } }

public interface Fahrer {

public void fortbewegen(int ziel);

public boolean angekommen();

public int get_standort();

public int get_ziel();

}

public class Pendler implements Beobachter, Fahrer { private int aktueller_standort;

private int bis_standort;

private int geschw=2;

Pendler(int strecke, int ziel){

aktueller_standort = strecke;

bis_standort = ziel;

}

public void aktualisiere( Subjekt s ) { geschw++;

}

public int get_standort(){

return aktueller_standort;

}

public int get_ziel(){

return bis_standort;

}

public boolean angekommen (){

return bis_standort == aktueller_standort;

}

public void fortbewegen(int naechstmoegliches_ziel){

if( aktueller_standort < naechstmoegliches_ziel ) {

if( naechstmoegliches_ziel <= (aktueller_standort+geschw) ) { aktueller_standort=naechstmoegliches_ziel;

return;

}

(7)

aktueller_standort=aktueller_standort+geschw;

} }

} // class Pendler

public class SkiUrlauber implements Beobachter, Fahrer { private int aktueller_standort;

private int bis_standort;

private int geschw=3;

private int pause=0;

private int [] staus= new int [0];

SkiUrlauber(int strecke, int ziel){

aktueller_standort = strecke;

bis_standort = ziel;

}

public void aktualisiere( Subjekt s ) { pause=5;

if (s instanceof StauWarnungsSystem) {

staus = ((StauWarnungsSystem)s).getStauListe();

} }

public int get_standort(){

return aktueller_standort;

}

public int get_ziel(){

return bis_standort;

}

public boolean angekommen (){

return bis_standort == aktueller_standort;

}

public void fortbewegen(int naechstmoegliches_ziel){

if (pause==0){

if( aktueller_standort < naechstmoegliches_ziel ) {

if( naechstmoegliches_ziel <= (aktueller_standort+geschw) ) { aktueller_standort=naechstmoegliches_ziel;

} else {

aktueller_standort=aktueller_standort+geschw;

} } } else {

pause--;

}

} // fortbewegen } // class SkiUrlauber

import java.util.*;

public class StauWarnungsSystem implements Subjekt { private ArrayList beobachterListe;

private int [] stausListe;

StauWarnungsSystem(){

beobachterListe = new ArrayList();

stausListe = new int[0];

}

public void setStauListe( int [] si ){

if (si.length != stausListe.length) { stausListe=si;

benachrichtige();

return;

}

// si.length == stausList.length if(Arrays.equals(si,stausListe)) {

return;

} else {

(8)

stausListe=si;

benachrichtige();

}

} // setStauInfo

public void benachrichtige() { ListIterator it =

beobachterListe.listIterator();

while( it.hasNext() ){

Beobachter b = (Beobachter)it.next();

b.aktualisiere( this );

} }

public void meldeAn( Beobachter b ){

beobachterListe.add( b );

}

public void meldeAb( Beobachter b ){

beobachterListe.remove( b );

}

public int [] getStauListe(){

int [] res = new int [stausListe.length];

for(int i=0;i<stausListe.length;i++){

res[i]=stausListe[i];

}

return res;

} }

import java.util.*;

public class Strasse{

private ArrayList fahrerliste;

private int [] stauliste;

Strasse(){

fahrerliste = new ArrayList();

stauliste = new int [0];

}

Strasse(ArrayList fl, int [] staus){

fahrerliste = fl;

stauliste = staus;

}

void aktualisiere_staus(int [] l){

stauliste = l;

}

int naechstmoegliches_ziel(int stand, int ziel){

if(stauliste.length==0){return ziel;}

int res = ziel;

for(int i=0; i<stauliste.length; i++){

if(stauliste[i]<stand){ break; } if(stauliste[i]==stand){ res=stand; } if(stand < stauliste[i]){

if(stauliste[i]<ziel){

res = stauliste[i];

} else {

res = ziel;

} } }

return res;

}

boolean alle_angekommen(){

boolean alle = true;

ListIterator it = fahrerliste.listIterator();

while( it.hasNext() ){

Fahrer f = (Fahrer)it.next();

if( !f.angekommen() ){

alle = false;

(9)

break;

} } // while return alle;

} // alle_angekommen

void verkehr_fortbewegen(){

ArrayList tmp = new ArrayList();

ListIterator it = fahrerliste.listIterator();

while( it.hasNext() ){

Fahrer f = (Fahrer)it.next();

if( !f.angekommen() ){

int nmz = naechstmoegliches_ziel(f.get_standort(),f.get_ziel());

f.fortbewegen(nmz);

}else{

tmp.add(f);

} }// while

it = tmp.listIterator();

while( it.hasNext() ){

Fahrer f = (Fahrer)it.next();

fahrerliste.remove(f);

}// while

} // verkehr_fortbewegen void add_fahrer(Fahrer f){

fahrerliste.add(f);

}

void remove_fahrer(Fahrer f){

fahrerliste.remove(f);

}

} // class Strasse import java.util.*;

class Main {

public static void main( String [] argf ){

int [] [] stau_erreignise = { {23, 465},

{23, 465}, {23, 465}, {23, 34}, {23, 34}, {23, 34}, { 34, 465}, { 34, 465}, { 34, 465}, { 34, 465}, {}

};

StauWarnungsSystem sws = new StauWarnungsSystem();

BrummiFahrer f1 = new BrummiFahrer(34,55);

SkiUrlauber f2 = new SkiUrlauber(56,78);

Pendler f3 = new Pendler(23,40);

Strasse strasse = new Strasse();

strasse.add_fahrer(f1);

strasse.add_fahrer(f2);

strasse.add_fahrer(f3);

sws.meldeAn(f1);

sws.meldeAn(f2);

sws.meldeAn(f3);

int zeitpunkt = 0;

while(zeitpunkt < stau_erreignise.length){

sws.setStauListe(stau_erreignise[zeitpunkt]);

strasse.aktualisiere_staus(stau_erreignise[zeitpunkt]);

strasse.verkehr_fortbewegen();

zeitpunkt++;

(10)

} // while } // main } // class Main

Aufgabe 4 Eine Hashtabelle

Die vorgegebene Implementierung ist irgendwann voll. Mit verketteten Listen kann die Hashtabelle beliebig wachsen. Bleibt die Menge der Hashschluessel immer konstant kann es zu Skalierungsproblemen kommen.

public class HashAufgabe {

public static String[][] hashmap = new String[100][100];

public static void main(String[] args) { hashAndStore("Hallo");

hashAndStore("Hello");

System.out.println(isIn("Hollo"));

hashAndStore("Hollo");

hashAndStore("Hollo");

System.out.println(isIn("Hollo"));

}

public static void hashAndStore(String s) { int h = s.hashCode() % 100;

for (int i = 0; i < 100;i++) {

if (hashmap[h][i] ==null) { hashmap[h][i] = s; return; } }

}

public static boolean isIn(String s) { int h = s.hashCode() % 100;

for (int i = 0; i < 100;i++) {

if (hashmap[h][i] !=null) {if (hashmap[h][i].equals(s)) return true;}

}

return false;

}

public static boolean deleteFrom(String s) { int h = s.hashCode() % 100;

for (int i = 0; i < 100;i++) {

if (hashmap[h][i] !=null) {if (hashmap[h][i].equals(s)) hashmap[h][i] = null;}

}

return false;

}

}

Referenzen

ÄHNLICHE DOKUMENTE

Entscheiden Sie, ob die folgenden Formeln Axiome des deduktiven Systems F sind. Falls ja, geben Sie an zu welchem der 6 Schemata die Formel

Geben Sie jeweils einen konstruktiven Beweis für die beiden folgenden Aussagen an. “Konstruktiv” heißt hier, dass aus dem Beweis hervorgehen soll, wie man die gewünschte

Fachbereich Informatik AG Softwaretechnik. Übungsblatt 13: Logik

Wenn der Rest der Liste die leere Liste ist, kann diese auch weggelassen werden.. Wir wollen in dieser Aufgabe zum Üben unsere eigenen

Fachbereich Informatik AG Softwaretechnik. Übungsblatt 3: Logik

Zeigen Sie, dass sich Beweise in F 0 verallgemeinern lassen: Wenn es einen Beweis mit einer Variablen p gibt, dann lässt sich p durch eine beliebige Formel ersetzen und man erhält

b) Schreiben Sie eine Funktion addLineNumbers: string -&gt; string -&gt; unit, die jeder Zeile aus Datei 1 (1. Parameter) eine Zeilennummer voranstellt und das Ergebnis in eine

Wenn v eine lokale Variable einer Prozedur ist, dann wird mit v immer die gleiche Spei- chervariable angesprochen... Auf jede Variable kann während der gesamten