• Keine Ergebnisse gefunden

Aufgabe1ParametrischeTypen LösungvorschlagzumÜbungsblatt13:Software-EntwicklungI(WS2007/08) TUKaiserslautern

N/A
N/A
Protected

Academic year: 2022

Aktie "Aufgabe1ParametrischeTypen LösungvorschlagzumÜbungsblatt13:Software-EntwicklungI(WS2007/08) TUKaiserslautern"

Copied!
8
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 13:

Software-Entwicklung I (WS 2007/08)

Aufgabe 1 Parametrische Typen

import java.io.*;

import java.lang.*;

class Point { int x;

int y;

Point(int x, int y){

this.x=x;

this.y=y;

}

public String toString() { return "("+x+","+y+")";

}

} // Point<T>

class SList<T>{

T elem;

SList<T> next;

} // class SList<T>

class Queue<T>{

SList<T> first;

SList<T> last;

boolean is_empty(){ return first==null; } Queue enqueue(T t){

SList tmp=new SList();

tmp.elem=t;

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

last=tmp;

return this;

} else {

last.next=tmp;

last=tmp;

return this;

} } // enqueue

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

throw new Exception();

} else {

T tmp = first.elem;

first=first.next;

return tmp;

} } // dequeue

(2)

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

Iterator(){

this.current=(SList<T>)first;

}

boolean hasNext(){

return current != null;

}

boolean hasNextNext(){

return current != null && current.next != null;

}

T next()throws Exception {

if(current==null){ throw new Exception(); } T res = current.elem;

current = current.next;

return res;

} }

Iterator createIterator(){ return new Iterator(); }

// x is equal memory usage of an element of the type T

int compute_memory_usage_of_a_queue(int x) throws Exception { int usage = 0;

Iterator iter = createIterator();

while(iter.hasNext()){

usage += x;

iter.next();

}

return usage;

} // compute_memory_usage

public String toString() { String res = "[";

Iterator iter = createIterator();

T tmp;

try{

while(iter.hasNextNext()){

res = res + iter.next().toString() + ",";

}

if (iter.hasNext()){

res = res + iter.next().toString();

}

} catch (Exception e) { res = res + "";

}

return res + "]";

}

} // Queue<T>

public class Main { // public class Main

static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

static Queue<Object> list = new Queue<Object>();

public static void main(String [] args) {

System.out.println("Listenverwalter Version 2.0:");

String opt = read_option();

if(opt.equals("i")) { maintain_number_list();

} else {

maintain_point_list();

} } // main

static void maintain_number_list(){

for(;;){

try {

int tmp = read_number();

list = list.enqueue(new Integer(tmp));

System.out.println("Aktueller Speicherverbrauch der Liste: "+

list.compute_memory_usage_of_a_queue(6));

System.out.println("Aktueller Zustand der Liste : "+ list);

(3)

} catch (Exception e) {

System.out.println("Programfehler");

System.exit(1);

} }

} // maintain_number_list

static void maintain_point_list(){

for(;;){

try {

int x = read_x_coord();

int y = read_y_coord();

list = list.enqueue(new Point(x,y));

System.out.println("Aktueller Speicherverbrauch der Liste: "+

list.compute_memory_usage_of_a_queue(8));

System.out.println("Aktueller Zustand der Liste : "+ list);

} catch (Exception e) {

System.out.println("Programfehler");

System.exit(1);

} }

} // maintain_point_list static String read_option (){

String line = "";

System.out.println("Bitte wählen Sie eine Option (default Option ist i):");

System.out.println("i : Listenverwalter soll eine Integer-Liste verwalten.");

System.out.println("p : Listenverwalter soll eine Point-Liste verwalten.");

System.out.println("q : Abbruch.");

for(;;){

try{

// readLine() throws IOException line = in.readLine();

if(line.equals("") || line.equals("i")){

return "i";

}

if(line.equals("p")){

return "p";

}

if(line.equals("q")){

System.exit(0);

}

System.out.println("Die Angabe ist nicht korrekt");

continue;

} catch (Exception e) {

System.out.println("Die Angabe ist nicht korrekt oder ein I/O-Fehler.");

} }

} // read_option

static int read_number(){

String line = "";

System.out.println("Bitte geben Sie die einzufügende Zahl "+

"oder \"q\", um abzubrechen, ein:");

for(;;){

try{

line = in.readLine();

if(line.equals("q")){

System.exit(0);

}

if(line == ""){

System.out.println("Die Angabe \"\" ist nicht korrekt");

continue;

}

int res = Integer.parseInt(line);

return res;

} catch (Exception e) {

System.out.println(line + "Die Angabe ist nicht korrekt");

} }

} // method read_number

static Point read_point() {

return new Point(read_x_coord(),read_y_coord());

(4)

} // read_point

static int read_x_coord (){

String line = "";

System.out.println("Bitte geben Sie die x-Koordinate des einzufügenden Punktes "+

"oder \"q\", um abzubrechen, ein:");

for(;;){

try{

line = in.readLine();

if(line.equals("q")){

System.exit(0);

}

if(line == ""){

System.out.println("Die Angabe \"\" ist nicht korrekt");

continue;

}

int res = Integer.parseInt(line);

return res;

} catch (Exception e) {

System.out.println(line + "Die Angabe ist nicht korrekt");

} }

} // method read_x_coord static int read_y_coord (){

String line = "";

System.out.println("Bitte geben Sie die y-Koordinate des einzufügenden Punktes "+

"oder \"q\", um abzubrechen, ein:");

for(;;){

try{

line = in.readLine();

if(line.equals("q")){

System.exit(0);

}

if(line == ""){

System.out.println("Die Angabe \"\" ist nicht korrekt");

continue;

}

int res = Integer.parseInt(line);

return res;

} catch (Exception e) {

System.out.println(line + "Die Angabe ist nicht korrekt");

} }

} // method read_y_coord } // public class Main

Aufgabe 2 Packages

Die Klasse TextComponent :

Die Klasse TextArea :

Zugriffsmodifikator darf TextArea zugreifen?

private nein

kein ja

protected ja

public ja

Die Klasse TextEditor :

Zugriffsmodifikator darf TextEditor zugreifen?

private nein

kein ja

protected ja

public ja

(5)

Die Klasse Book :

Zugriffsmodifikator darf Book zugreifen?

private nein

kein nein

protected nein

public nein

Die Klasse TextDisplay :

Zugriffsmodifikator darf TextDisplay zugreifen?

private nein

kein nein

protected nein

public nein

Die Klasse TextArea :

Die Klasse TextEditor :

Zugriffsmodifikator darf TextEditor zugreifen?

private nein

kein ja

protected ja

public ja

Die Klasse Book :

Zugriffsmodifikator darf Book zugreifen?

private nein

kein nein

protected nein

public ja

Die Klasse TextDisplay :

Zugriffsmodifikator darf TextDisplay zugreifen?

private nein

kein nein

protected ja

public ja

Die Klasse TextEditor :

Die Klasse TextArea :

Zugriffsmodifikator darf TextArea zugreifen?

private nein

kein ja

protected ja

public ja

Aufgabe 3 Störe meine Kreise nicht 2

interface Figure {

public void draw(SEGraphics seg);

public void moveX (int x);

public void moveY (int y);

public void moveXY (int x, int y);

}

class ClassicalFigure implements 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;

}

(6)

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;

}

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);

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

} }

class Square implements Figure { int x;

int y;

int color;

int length;

String name;

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

this.x = x;

this.y = y;

color = c;

length = l;

}

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;

}

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

}

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

}

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

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

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

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

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

} }

public class DrawEdit2 {

public static int MAXFIGURE = 20;

(7)

SEGraphics seg;

Figure[] figures;

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

figures = new Figure[MAXFIGURE];

while(true) {

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

"3) eine Figure verschieben? 4) 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);

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("Farbe?");

int c = 255*InputOutput.readInt();

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

break; } case 3: {

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 DrawEdit2();

}

}

(8)

Aufgabe 4 Listen – gekapselt und doppelt verkettet

public class DLL {

private static DLL firstelement;

private static DLL actelement;

private int content;

private DLL pred;

private DLL succ;

public DLL(int content) { this.content = content;

if (actelement == null) { actelement = this;

firstelement = this;

pred = this;

succ = this;

} else {

firstelement.pred = this;

actelement.succ = this;

pred = actelement;

succ = firstelement;

actelement = this;

} }

public static void printElements() { DLL d = firstelement;

do {

InputOutput.println (d.content);

d = d.succ;

} while (d != firstelement);

}

public static void printRevElements() { DLL d = firstelement;

do {

InputOutput.println (d.content);

d = d.pred;

} while (d != firstelement);

}

public static void main(String[] args) { DLL d=new DLL(1);

new DLL(2);

new DLL(3);

new DLL(4);

d.printElements();

d.printRevElements();

} }

Referenzen

ÄHNLICHE DOKUMENTE

Perlenketten sind ringförmige Schnüre, auf die kleine Perlen aufgereit sind. In dieser Aufgabe wollen wir solche Ketten näher betrachten. Perlen können sechs verschiedene Farben

∗ wenn nein: lege die Karte auf den vierten Stapel c) Beschreibung: Sie brauchen drei Stapel. • wiederhole Folgendes f¨ ur alle Karten auf dem

a) Die Korrektheit der Eingabe wird nicht überprüft.. “nat list” eignet sich

[r]

i) xs ist zulässiger Parameter in dem Funktionsaufruf (tuples

[r]

22 public Buchung buchen(String von,String nach, String wann, String fluglinie,String kunde){. 23 Flug flug

– lege oberste Karte vom ersten auf den zweiten Stapel – wiederhole Folgendes f¨ ur alle Karten auf dem ersten Stapel:. pr¨ ufe, ob die aktuelle Kartennummer (oberste Karte vom