TECHNISCHE
UNIVERSITÄT
MÜNCHEN
FAKULTÄT FÜR INFORMATIK
Lehrstuhl für Sprachen und Beschreibungsstrukturen
Praktikum Grundlagen der Programmierung
F. Forster, T. Gawlitza, A. Flexeder
Übungen zu
WS 2007/2008
Lösungsvorschläge zu Blatt 12
18. Januar 2008
Praktikum Grundlagen der Programmierung
Aufgabe 54 Appletspielereien (Lösungsvorschlag)
a) HTML-Code: simpleapplet.html
<html>
<head>
<title>Applet - Spielereien </title>
</head>
<body>
<h1>Applet - Spielereien </h1>
<applet codebase="." code=" SimpleApplet. class " width=600
height=400 alt=" Your browser has to be Java - enabled to see the applet !" >
</applet>
</body>
</html>
b) Applet-Code: SimpleApplet.java
import
import
import
import
import
java. applet . Applet ;
java. awt . Color ;
java. awt . Font ;
java. awt . Graphics ;
java. awt . Polygon ;
public class SimpleApplet extends Applet {
public void paint ( Graphics page ) {
setBackground( Color . gray );
Font big = new Font (" SansSerif " , Font. BOLD ,30);
Font normal = new Font (" SansSerif " ,Font . BOLD ,10);
page. setFont ( big );
page. setColor ( Color . black );
page. drawString (" Applet - Spielereien ", 50 , 50);
page. setFont ( normal );
page. setColor ( Color . white );
page. drawString (" im Praktikum Grundlagen der Programmierung" , 50 , 70);
page. drawRoundRect(30 ,20 , 340 , 60 ,10 ,10);
page. setColor ( Color . blue );
page. drawOval (200 , 200 , 100 , 100);
page. drawOval (400 , 200 , 100 , 100);
page. setColor ( Color . black );
page. fillRect (235 , 235 , 30 , 30);
page. fillRect (435 , 235 , 30 , 30);
Polygon p = new
p. addPoint (350 ,
p. addPoint (330 ,
p. addPoint (370 ,
Polygon ();
240);
280);
280);
page. fillPolygon (p );
page. setColor ( Color . red );
Lösung 12/ Seite 2
page. drawLine (250 , 350 , 450 , 350);
page. drawLine (250 , 350 , 220 , 320);
page. drawLine (450 , 350 , 480 , 320);
}
}
Test durch Aufrufen der HTML-Datei im WWW-Browser oder mit appletviewer.
Aufgabe 55 (Ü)
Pater-Noster
Eine mögliche Modellierung sieht folgendermassen aus:
Thread
run():void
Fahrgast
in:int
out:int
name:String
Fahrgast(in:int,out:int,name:String)
run():void
Paternoster
mitfahren(in:int,out:int):void
public class Fahrgast extends Thread {
private String name ;
private int in , out ;
private Paternoster aufzug ;
public Fahrgast ( String name , Paternoster aufzug , int in , int out ) {
this. name = name ;
this. aufzug = aufzug ;
this. in = in ;
this. out = out ;
}
public void run () {
System . out . println ( name + " wartet im " + in + ". Stock " );
aufzug . mitfahren (in , out );
}
public String toString () {
return name;
}
}
public abstract class Paternoster extends Thread {
public abstract void mitfahren (int in , int out );
}
public class SimplePaternoster extends Paternoster {
private int stockwerke , frei , stockwerk = 0, delta = 1;
private Object monitor = new Object ();
public SimplePaternoster(int stockwerke , int frei) {
this. stockwerke = stockwerke ;
this. frei = frei ;
}
public void run () {
while (true) {
// Aufzug verweilt im Stockwerk
try { sleep (1000); } catch ( InterruptedException e) { }
synchronized( monitor ) {
System . out . println (" Aufzug faehrt los ." );
try { sleep (1000); } catch ( InterruptedException e) { }
Lösung 12/ Seite 3
stockwerk += delta ;
if (( stockwerk ==0)||( stockwerk == stockwerke -1)) delta = -delta ;
System . out . println (" Aufzug haelt an ." );
System . out . println (" Stockwerk "+ stockwerk +"; lasse Leute ein - und aussteigen " );
monitor . notifyAll ();
}
}
}
public void mitfahren (int in , int out ){
synchronized ( monitor ) {
while ( stockwerk != in || frei == 0)
try { monitor . wait ();} catch ( InterruptedException ie ) {}
System . out . println ( Thread . currentThread() + " steigt ein " );
frei - -;
while ( stockwerk != out )
try { monitor . wait ();} catch ( InterruptedException ie ) {}
frei ++;
System . out . println ( Thread . currentThread() + " steigt aus " );
}
}
}
public class ExtendedPaternoster extends Paternoster {
private int stockwerke , frei , stockwerk = 0, delta = 1;
// Monitore zum Ein- und Aussteigen
private Object [] einsteigen , aussteigen ;
public ExtendedPaternoster(int stockwerke , int frei ) {
this. stockwerke = stockwerke ;
this. frei = frei ;
einsteigen = new Object [ stockwerke ];
aussteigen = new Object [ stockwerke ];
for (int i = 0; i< stockwerke ;i ++) {
einsteigen [i] = new Object ();
aussteigen [i] = new Object ();
}
}
public void run () {
while (true) {
synchronized( einsteigen [ stockwerk + delta ]) {
synchronized( aussteigen [ stockwerk + delta ]) {
synchronized( einsteigen [ stockwerk ]) {
synchronized( aussteigen [ stockwerk ]) {
// Aufzug faehrt
try { sleep (1000); } catch ( InterruptedException e) { }
stockwerk += delta ;
if (( stockwerk ==0)||( stockwerk == stockwerke -1)) delta = - delta ;
}
}
System . out . println (" Stockwerk "+ stockwerk +"; lasse Leute aussteigen " );
aussteigen [ stockwerk ]. notifyAll ();
}
// Abstand zwischen aus- und einsteigen
try { sleep (500); } catch ( InterruptedException e) { }
System . out . println (" Stockwerk "+ stockwerk +"; lasse Leute einsteigen " );
einsteigen [ stockwerk ]. notifyAll ();
}
// Aufzug verweilt im Stockwerk
try { sleep (1000); } catch ( InterruptedException e) { }
}
}
public void mitfahren (int in , int out ){
synchronized ( einsteigen [ in ]) {
while ( stockwerk != in || frei == 0)
try { einsteigen [in ]. wait ();} catch ( InterruptedException ie ) {}
frei - -;
}
System . out . println ( Thread . currentThread() + " ist eingestiegen" );
synchronized ( aussteigen [ out ]) {
while ( stockwerk != out )
try { aussteigen [ out ]. wait ();} catch ( InterruptedException ie ) {}
frei ++;
}
}
}
Lösung 12/ Seite 4
Aufgabe 56 (H) Aussichtsturm
(10 Punkte)
public class Tourist extends Thread {
private String name ;
private Aufzug aufzug ;
public Tourist ( String name , Aufzug aufzug ){
this. name = name ;
this. aufzug = aufzug ;
}
public void run () {
System . out . println ( name + " moechte rauffahren ." );
aufzug . rauffahren ();
System . out . println ( name + " befindet sich auf der Platform ." );
try {
System . out . println ( name +" verweilt auf der platform " );
sleep ((new java . util . Random ()). nextInt (8000)+2000);
} catch ( InterruptedException e) { }
System . out . println ( name + " moechte runterfahren." );
aufzug . runterfahren();
}
public String toString () {
return name ;
}
}
public class Aufzug extends Thread {
private int stockwerk = 0, platform = 0;
private boolean frei = true;
private Object rauf = new Object ();
private Object runter = new Object ();
private Object passagier = new Object ();
public Aufzug (){}
public void run () {
while (true) {
if( stockwerk == 0){
synchronized ( rauf ){ rauf. notify ();}
try { sleep (500); } catch ( InterruptedException e) { }
stockwerk = 10;
}else{
synchronized ( runter ){ runter . notify ();}
try { sleep (500); } catch ( InterruptedException e) { }
stockwerk = 0;
}
frei = true;
System . out . println ("[ Aufzug ]: Aufzug haelt an." );
System . out . println ("[ Aufzug ]: Stockwerk "+ stockwerk +": ein - und aussteigen " );
synchronized( passagier ){ passagier . notify ();}
System . out . println ("[ Aufzug ]: Aufzug faehrt los ." );
try { sleep (1000); } catch ( InterruptedException e) { }
}
}
public void rauffahren (){
synchronized ( rauf ) {
while (! frei || platform ==10 || stockwerk != 0){
System . out . println (" RAUF: Tourist muss warten . " );
try { rauf . wait ();} catch ( InterruptedException ie ) {}
}
frei = false;
platform ++;
}
synchronized( passagier ){
try {
passagier . wait ();
}catch( InterruptedException ie ){}
}
}
public void runterfahren(){
synchronized ( runter ) {
while(! frei || stockwerk != 10){
System . out . println (" RUNTER : Tourist muss warten ." );
try { runter . wait ();} catch ( InterruptedException ie) {}
}
frei = false;
Lösung 12/ Seite 5
platform - -;
}
synchronized( passagier ){
try {
passagier . wait ();
}catch( InterruptedException ie ){}
}
}
}
public class Main {
public static void main ( String [] args ) {
Aufzug p = new Aufzug ();
Tourist f1 = new Tourist (" Thomas " , p );
Tourist f2 = new Tourist (" Alex " ,
p );
Tourist f3 = new Tourist (" Peter " , p );
Tourist f4 = new Tourist (" Sylvia " , p );
Tourist f5 = new Tourist (" Markus " , p );
Tourist f6 = new Tourist (" Michael ",p );
Tourist f7 = new Tourist (" Arnie " , p );
Tourist f8 = new Tourist (" Bert " ,
p );
Tourist f9 = new Tourist (" Flo " ,
p );
Tourist f10 = new Tourist (" Betty " , p );
Tourist f11 = new Tourist (" Biggy " , p );
f1. start ();
f2. start ();
f3. start ();
f4. start ();
f5. start ();
f6. start ();
f7. start ();
f8. start ();
f9. start ();
f10 . start ();
f11 . start ();
p. start ();
}
}
Aufgabe 57 (H) Applet-Kunst
(5 Punkte)
a) HTML-Code: modernart.html
<html>
<head>
<title> Moderne Kunst </title>
</head>
<body>
<h1> Moderene Kunst </h1>
<applet codebase="." code=" ModernArt . class " width=600
height=700 alt=" Your browser has to be Java - enabled to see the applet !" >
</applet>
</body>
</html>
b) Applet-Code: ModernArt.java
import
import
import
import
import
java. applet . Applet ;
java. awt . Color ;
java. awt . Font ;
java. awt . Graphics ;
java. util . Random ;
public class ModernArt extends Applet {
public void paint ( Graphics page ) {
// how many moves are
final int MOVES = 40;
// boundaries of the canvas: TOPLEFT_X, TOPLEFT_Y, BOTTOMRIGHT_X, BOTTOMRIGHT_Y
final int[] CANVAS_BOUNDARIES = new int[] {20 ,20 ,400 ,600};
page. drawRect ( CANVAS_BOUNDARIES[0] -1 , CANVAS_BOUNDARIES[1] -1 ,
CANVAS_BOUNDARIES[2] - CANVAS_BOUNDARIES[0]+2 ,
CANVAS_BOUNDARIES[3] - CANVAS_BOUNDARIES[1]+2);
Random random = new Random ();
for (int i = 0; i < MOVES ; i ++) {
setRandomColor( page );
Lösung 12/ Seite 6
int move = random . nextInt (5);
if ( move == 0)
drawText (" modern art ",page , CANVAS_BOUNDARIES);
else if ( move == 1)
drawRect ( page , CANVAS_BOUNDARIES);
else if ( move == 2)
drawOval ( page , CANVAS_BOUNDARIES);
else
drawLine ( page , CANVAS_BOUNDARIES);
}
setBackground( Color . white );
}
private void setRandomColor( Graphics page) {
Random random = new Random ();
page. setColor (new Color ( random . nextInt (256) , random . nextInt (256) , random . nextInt (256)));
}
private void drawLine ( Graphics page , int[] boundaries ) {
Random random = new Random ();
int x1 = random . nextInt ( boundaries [2] - boundaries [0]) + boundaries [0];
int x2 = random . nextInt ( boundaries [2] - boundaries [0]) + boundaries [0];
int y1 = random . nextInt ( boundaries [3] - boundaries [1]) + boundaries [1];
int y2 = random . nextInt ( boundaries [3] - boundaries [1]) + boundaries [1];
page. drawLine (x1 ,y1 ,x2 ,y2 );
}
private void drawRect ( Graphics page , int[] boundaries ) {
Random random = new Random ();
int x = random . nextInt ( boundaries [2] - boundaries [0]) + boundaries [0];
int y = random . nextInt ( boundaries [3] - boundaries [1]) + boundaries [1];
int width = random . nextInt ( boundaries [2] - x );
int height = random . nextInt ( boundaries [3] - y);
if ( random . nextInt (2) == 1)
page . drawRect (x , y , width , height );
else
page . fillRect (x ,y , width , height );
}
private void drawOval ( Graphics page , int[] boundaries ) {
Random random = new Random ();
int x = random . nextInt ( boundaries [2] - boundaries [0]) + boundaries [0];
int y = random . nextInt ( boundaries [3] - boundaries [1]) + boundaries [1];
int width = random . nextInt ( boundaries [2] - x );
int height = random . nextInt ( boundaries [3] - y);
if ( random . nextInt (2) == 1)
page . drawOval (x , y , width , height );
else
page . fillOval (x ,y , width , height );
}
private void drawText ( String string , Graphics page , int[] boundaries ) {
int stringWidth = page . getFontMetrics(). stringWidth ( string );
int height = page. getFontMetrics(). getHeight ();
Random random = new Random ();
int x = random . nextInt ( boundaries [2] - boundaries [0] - stringWidth ) + boundaries [0];
int y = random . nextInt ( boundaries [3] - boundaries [1] - height ) + boundaries [1] + height ;
page. drawString ( string ,x ,y );
}
}
Test durch Aufrufen der HTML-Datei im WWW-Browser oder mit appletviewer.