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 9: Software-Entwicklung I (WS 2007/08)
Aufgabe 1 Das Sieb des Eratosthenes
public class a3 extends InputOutput {
public static void main(String []args) { int maxnum = readInt();
boolean[] feld = new boolean[maxnum+1];
for(int i = 0;i <=maxnum;i++) feld[i] = true;
for (int i = 2; i*i <= maxnum;i++) { if (feld[i]) {
for (int j=i*i;j <=maxnum; j+=i) feld[j]=false;
} }
for (int i = 2; i <=maxnum;i++) { if (feld[i])
println(i);
} } }
Aufgabe 2 Ein prozedurales InsertionSort
class sortier_element { public int key;
public String name;
}
public class a2 extends InputOutput {
public static void sortInsert(sortier_element[] f,sortier_element e) { sortier_element t = new sortier_element();
sortier_element t2;
t.key = 0;
boolean shiftflag = false;
for (int i = 0;i < f.length;i++) { if (shiftflag) {
t2 = f[i];
f[i] = t;
t = t2;
} else {
if (e.key <= f[i].key) { t = f[i];
f [i] = e;
shiftflag = true;
}}
} }
public static void main(String[] args) {
sortier_element[] feld = new sortier_element[10];
sortier_element[] feld2 = new sortier_element[10];
feld[0] = new sortier_element();
feld[1] = new sortier_element();
feld[2] = new sortier_element();
feld[3] = new sortier_element();
feld[4] = new sortier_element();
feld[5] = new sortier_element();
feld[6] = new sortier_element();
feld[7] = new sortier_element();
feld[8] = new sortier_element();
feld[9] = new sortier_element();
feld2[0] = new sortier_element();
feld2[1] = new sortier_element();
feld2[2] = new sortier_element();
feld2[3] = new sortier_element();
feld2[4] = new sortier_element();
feld2[5] = new sortier_element();
feld2[6] = new sortier_element();
feld2[7] = new sortier_element();
feld2[8] = new sortier_element();
feld2[9] = new sortier_element();
feld[0].key = 5;
feld[1].key = 4;
feld[2].key = 3;
feld[3].key = 10;
feld[4].key = 14;
feld[5].key = 13;
feld[6].key = 25;
feld[7].key = 24;
feld[8].key = 23;
feld[9].key = 5;
feld2[0].key=1000;
feld2[1].key=1000;
feld2[2].key=1000;
feld2[3].key=1000;
feld2[4].key=1000;
feld2[5].key=1000;
feld2[6].key=1000;
feld2[7].key=1000;
feld2[8].key=1000;
feld2[9].key=1000;
int j;
for (j = 0;j<feld.length;j++) { sortInsert(feld2,feld[j]);
}
for (j = 0;j<feld.length;j++)
println (feld2[j].key);
} }
Aufgabe 3 Sortieren durch Einfügen und Anhängen von Listen mit rekursiven Verbunden
1 class DataSet {
2 public int key;
3 public String value;
4 5
6 public DataSet(int k, String v){
7 key = k;
8 value = v;
9 }
10 public String toString(){
11 return ("("+key+","+value+")");
12 }
13 }
14
15 class DataSetList {
16 public DataSet elem;
17 public DataSetList next;
18
19 public DataSetList(DataSet ds){
20 elem = ds;
21 next = null;
22 }
23 public DataSetList(DataSet ds, DataSetList l){
24 elem = ds;
25 next = l;
26 }
27 public DataSetList Cons(DataSet ds){
28 return new DataSetList(ds,this);
29 }
30 public String toString(){
31 String res = "[";
32 DataSetList tmp = this;
33 while (tmp.next != null) {
34 res = res + tmp.elem.toString() + ",";
35 tmp = tmp.next;
36 }
37 res = res + tmp.elem.toString() + "]";
38 return res;
39 }
40 }
41 42
43 public class Main { // public class Main
44 45
46 public static void main(String [] args) {
47
48 DataSet e0 = new DataSet(0,"0");
49 DataSet e1 = new DataSet(1,"1");
50 DataSet e2 = new DataSet(2,"2");
51 DataSet e3 = new DataSet(3,"3");
52
53 DataSetList l0 = new DataSetList(e0);
54 DataSetList l1 = insert(l0,e2);
55 DataSetList l2 = insert(l1,e1);
56 57
58 System.out.println("test1: ");
59 System.out.println(e1);
60 System.out.println(l0);
61 System.out.println(l1);
62 System.out.println(l2);
63
64 System.out.println("test2:");
65 DataSetList l3 = (new DataSetList(e0)).Cons(e1).Cons(e2).Cons(e3).Cons(e2);
66 System.out.println(l3);
67 DataSetList l4 = insertionSort(l3);
68 System.out.println(l4);
69
70 System.out.println("test3:");
71 System.out.println("l5="+append1(l3,l4));
72 System.out.println("l6="+append2(l3,l4));
73 System.out.println("l7="+append3(l3,l4));
74 75 76
77 }
78
79 public static DataSetList insert(DataSetList l, DataSet e){
80 if(l==null) {
81 return new DataSetList(e);
82 }
83 else {
84 if (e.key <= l.elem.key) {
85 return new DataSetList(e,l);
86 } else {
87 return new DataSetList(l.elem,insert(l.next,e));
88 }
89 }
90 }
91
92 public static DataSetList insertionSort(DataSetList l){
93 if(l==null) { return l; }
94 else {
95 return insert(insertionSort(l.next), l.elem);
96 }
97 }
98
99 public static DataSetList append1( DataSetList l1,
100 DataSetList l2){
101 if (l1==null) {
102 return l2;}
103 else {
104 return append1(l1.next,l2).Cons(l1.elem);
105 }
106 }
107 public static DataSetList append2( DataSetList l1,
108 DataSetList l2){
109 if (l1==null) {
110 return l2;}
111 else {
112 return new DataSetList( l1.elem,
113 append2(l1.next, l2));
114 }
115 }
116
117 public static DataSetList append3( DataSetList l1,
118 DataSetList l2){
119 if (l1==null) {
120 if (l2==null) {
121 return null;
122 } else {
123 return new DataSetList( new DataSet( l2.elem.key,
124 l2.elem.value),
125 append3(l1,l2.next));
126 }
127 } else {
128 return new DataSetList( new DataSet(l1.elem.key,
129 l1.elem.value),
130 append3(l1.next,l2));
131 }
132 }
133
134 } // public class Main
135