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 8: Software-Entwicklung I (WS 2007/08)
Aufgabe 1 Einführung in Java
public class a1 extends InputOutput {
public static void main (String []args) { String s;
int i;
println ("HalloWelt\n");
println (i = readInt()*2);
println (s = readString());
println (i);
println (s);
} }
Aufgabe 2 Primzahlen testen
public class a2neu extends InputOutput {
public static boolean isPrimHelp1(int i,int j) { if (i == 1 | i == 2) return true;
if (i % j == 0) { return false;
} else {
if (j == 2) return true;
else
return isPrimHelp1(i,j-1);
} }
public static boolean isPrim1(int i) { return isPrimHelp1(i,i-1);
}
public static boolean isPrim2(int i) { int j = i - 1;
if (i == 1 | i == 2) return true;
while (j >= 2) {
if (i % j == 0) return false;
j--;
}
return true;
}
public static void main(String[] args) { int eingabe = readInt();
if (eingabe < 1)
println ("Benutzungsfehler");
else {
if (isPrim1(eingabe)) println ("Prim");
else
println ("nicht Prim");
if (isPrim2(eingabe)) println ("Prim");
else
println ("nicht Prim");
} }
}
Aufgabe 3 Java Programme umschreiben
If-kaskaden: koennen ueberfuehrt werden, wenn Sie eine Variable mit verschiedenen Konstanten verglei- chen. Vorgehen: trivial, default als letzen else Fall, breaks nicht vergessen.
For-Schleife vs While:
for (a;b;c) { B
}
===
{ a;
while (b) { B
c;
} }
public static double p2(int i,boolean b,char c,double d) { if (c == ’c’) while (true) {};
for (;i < 100;i+=2) { switch (c) {
case ’d’: d *=2; break;
case ’e’: b = true; break;
default : b = false;
}
if (d == 5) b = true;
if (b) break;
}
return d;
}
Aufgabe 4 Punkte, Linien, Dreiecke
1 strictfp class Point {
3 // repreasentiert
4 private double a;
5 private double b;
6 public Point(double x, double y){
7 a=x;
8 b=y;
9 }
10 double getX(){ return a;}
11 double getY(){ return b;}
12 public String toString(){
13 return ("("+a+","+b+")");
14 }
15
16 }
17 18 19
20 strictfp class Line {
21 // zwei Punkte (x1,y1) und (x2,y2)
22 // bestimmen eine Linie ax + b mit
23 // a = (y2-y1) / (x2 - x1)
24 // b = ( (x1 * y2) - (x2 * y1) ) / (x1-x2)
25 private double a;
26 private double b;
27
28 private double compute_a(Point p1, Point p2){
29 double tmp = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
30 // System.out.println("compute_a");
31 // System.out.println("p1 =" + p1);
32 // System.out.println("p2 =" + p2);
33 // System.out.println("y2-y1 =" + (p2.getY() - p1.getY()));
34 // System.out.println("x2-x1 =" + (p2.getX() - p1.getX()));
35 // System.out.println("(y2-y1)/(x2-x1) =" + tmp);
36 // System.out.println("a =" + tmp);
37 return tmp;
38 }
39 private double compute_b(Point p1, Point p2){
40 double tmp = ( (p1.getX() * p2.getY())
41 -
42 (p2.getX() * p1.getY())
43 ) /
44 (p1.getX()-p2.getX()) ;
45 // System.out.println("compute_b");
46 // System.out.println("p1 =" + p1);
47 // System.out.println("p2 =" + p2);
48 // System.out.println("a =" + tmp);
49 return tmp;
50 }
51 private double compute_y(double x){
52 return (a*x)+b;
53 }
54
55 public Line(Point p1, Point p2){
56 a=compute_a(p1,p2);
57 b=compute_b(p1,p2);
58 System.out.println("Line(:"+p1+","+p2+")");
59 // System.out.println("a=" + a);
60 // System.out.println("b=" + b);
61 System.out.println("line equation: "+this);
62 }
63
64 public boolean on_the_line(Point p){
65 System.out.println("---");
66 System.out.println("method: on_the_line");
67 System.out.println("checking whether the point "+p+
68 " is on the line "+this);
69 Point tmp = new Point(p.getX()-100,compute_y(p.getX()-100));
70 if (compute_a(tmp,p)==a && compute_b(tmp,p)==b){
71 System.out.println(true);
72 System.out.println("---");
73 return true; } else {
74 System.out.println(false);
75 System.out.println("---");
76 return false;
77 }
78 }
79
80 public boolean on_the_same_side(Point p1, Point p2){
81 if ( (compute_y(p1.getX())>p1.getX()) &&
82 (compute_y(p2.getX())>p2.getX()) )
83 return true;
84 if ( (compute_y(p1.getX())==p1.getX()) &&
85 (compute_y(p2.getX())==p2.getX()) )
86 return true;
87 if ( (compute_y(p1.getX())<p1.getX()) &&
88 (compute_y(p2.getX())<p2.getX()) )
89 return true;
90 return false;
91 }
92 // public boolean inside_the_triangle(Point p1, Point p2, Point p3, Point x) {
93 // Line l_p1_p2 = new Line(p1,p2);
94 // Line l_p2_p3 = new Line(p2,p3);
95 // Line l_p3_p1 = new Line(p3,p1);
96 // System.out.println("method: inside_the_triangle");
97 // System.out.println("checking whether the point "+x+
98 // " is within the triangle ("+p1+","+p2+","+p3+")");
99 // if (!(l_p1_p2.on_the_same_side(p3,x))){
100 // System.out.println("false");
101 // System.out.println("---");
102 // return false;
103 // }
104 // if (!l_p2_p3.on_the_same_side(p1,x)){
105 // System.out.println(false);
106 // System.out.println("---");
107 // return false;
108 // }
109 // if (!l_p3_p1.on_the_same_side(p2,x)){
110 // System.out.println(false);
111 // System.out.println("---");
112 // return false;
113 // }
114 // System.out.println(true);
115 // System.out.println("---");
116 // return true;
117 // }
118 public String toString(){
119 return "y="+a+"*x+"+b;
120 }
121 }
122 123 124
125 public strictfp class Main {
126 127
128 public static void main(String [] args) {
129 Point p1a = new Point(5.0,6.0);
130 Point p1b = new Point(7.0,6.0);
131 Point p1c = new Point(3.0,3.0);
132 Point p1d = new Point(5.0,4.0);
133 Point p2 = new Point(1.0,2.0);
134 Point p3 = new Point(5.0,2.0);
135 Point x = new Point(4.0,4.0);
136 Line l = new Line(p1a,p2);
137
138 on_the_line(l,p1a);
139 System.out.println("---");
140 System.out.println("1. test:");
141 inside_the_triangle(p1a,p2,p3,x);
142
143 System.out.println("---");
144 System.out.println("2. test:");
145 inside_the_triangle(p1b,p2,p3,x);
146
147 System.out.println("---");
148 System.out.println("3. test:");
149 inside_the_triangle(p1c,p2,p3,x);
150
151 System.out.println("---");
152 System.out.println("4. test:");
153 inside_the_triangle(p1d,p2,p3,x);
}
155 // Teilaufgabe a
156 static boolean on_the_line(Line l, Point p) {
157 return l.on_the_line(p);
158 }
159 // Teilaufgabe b
160 static boolean on_the_same_side(Line l, Point p1, Point p2) {
161 return l.on_the_same_side(p1,p2);
162 }
163 // Teilaufgabe c
164 static boolean inside_the_triangle(Point p1, Point p2, Point p3, Point x) {
165 Line l_p1_p2 = new Line(p1,p2);
166 Line l_p2_p3 = new Line(p2,p3);
167 Line l_p3_p1 = new Line(p3,p1);
168 System.out.println("---");
169 System.out.println("method: inside_the_triangle");
170 System.out.println("checking whether the point "+x+
171 " is within the triangle ("+p1+","+p2+","+p3+")");
172 if (!(l_p1_p2.on_the_same_side(p3,x))){
173 System.out.println("false");
174 System.out.println("---");
175 return false;
176 }
177 if (!l_p2_p3.on_the_same_side(p1,x)){
178 System.out.println(false);
179 System.out.println("---");
180 return false;
181 }
182 if (!l_p3_p1.on_the_same_side(p2,x)){
183 System.out.println(false);
184 System.out.println("---");
185 return false;
186 }
187 System.out.println(true);
188 System.out.println("---");
189 return true;
190 }
191 }