Download probe04loes - oth

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Fachhochschule Regensburg
Probeklausur
Prüfungsfach: PG1 (Programmieren in Java)
Name: ______________________________
Aufgabensteller: Prof. Sauer
Vorname:______________________________
Semester: _______ Matrikel-Nr.: ___________
Prüfungstermin: 20.01.2005
Zugel. Hilfsmittel: keine Einschränkung
Arbeitszeit: 90 Minuten
Anzahl der erreichten Punkte: _______________
Erstprüfer: ________________________
Benotung: ________________________
Zweitprüfer: _______________________________
Bewertung: Aufgabe 1: Punkte Aufgabe 2: Punkte Aufgabe 3: Punkte Aufgabe 4: Punkte
Aufgabe 5: Punkte
Lösungen
1. Aufgabe
import java.awt.*;
import java.awt.event.*;
public class ButtonTest extends Frame implements ActionListener
{
/* Erzeugt eine Warnung
* Falls ein Schaltflaeche gedrueckt wird,
* macht sie das Fenster rot
*/
Button warteButton;
Button rebootButton;
String[] nachricht = {
"W A R N U N G",
"Moelicherweise Virus entdeckt",
"Reboot und lasse den Virus-",
"
Entferner ablaufen"
};
ButtonTest()
{
setBackground(Color.lightGray);
setForeground(Color.black);
setLayout(new FlowLayout(FlowLayout.CENTER,15,10));
for (int i = 0; i < nachricht.length; i++)
add(new Label(nachricht[i]));
warteButton = new Button("Warten");
warteButton.addActionListener(this);
add(warteButton);
rebootButton = new Button("Reboot");
rebootButton.addActionListener(this);
add(rebootButton);
setTitle("Warnung");
setSize(220,200);
setBackground(Color.lightGray);
setForeground(Color.black);
addWindowListener(new WindowAdapter()
1
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == rebootButton)
{
setVisible(false);
dispose();
System.exit(0);
}
else
if (e.getSource() == warteButton)
{
setForeground(Color.white);
setBackground( Color.red);
}
}
public static void main(String args [])
{
new ButtonTest();
}
}
2. Aufgabe
import java.awt.*;
import java.awt.event.*;
/* A class to test dialogs and component parenting */
public class Dialoge extends Frame
{
protected static ActionListener dismiss = new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand().equals("exit"))
System.exit(0);
Button b = (Button)e.getSource();
((Dialog)b.getParent()).dispose();
}
};
protected Dialog dialog (boolean modal)
{
Dialog d = new Dialog(this, modal ? "Modal" : "Dialog", modal);
String s = modal ? "This is a modal dialog" : "This is a dialog";
d.add(new Label(s), BorderLayout.CENTER);
Button down = new Button("Ok");
down.setActionCommand("dismiss"); down.addActionListener(dismiss);
d.add(down, BorderLayout.SOUTH);
d.pack(); d.show();
return d;
}
public Dialoge ()
{
setLayout(new GridLayout(3,1));
Button up;
add(up = new Button("Dialog"));
up.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
dialog(false).setLocation(100,100);
2
}
});
add(up = new Button("Modal"));
up.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
dialog(true).setLocation(200,200);
}
});
add(up = new Button("Quit"));
up.setActionCommand("exit"); up.addActionListener(dismiss);
setSize(220,200);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
public static void main(String args[])
{
Dialoge dial = new Dialoge();
}
}
3. Aufgabe
import java.awt.*;
import java.awt.event.*;
/* A class to test a scrollbar connected to a textfield */
public class ScrollbarTest extends Frame
{
Panel p = new Panel();
public ScrollbarTest ()
{
p.setLayout(new GridLayout(3,1));
final TextField tf;
p.add(tf = new TextField(30)); tf.setText("50");
final Scrollbar sb;
p.add(sb = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 100));
Button b;
p.add(b = new Button("Quit"));
tf.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
sb.setValue(Integer.parseInt(tf.getText()));
}
});
tf.addFocusListener(new FocusAdapter()
{
public void focusLost (FocusEvent e)
{
sb.setValue(Integer.parseInt(tf.getText()));
}
});
sb.addAdjustmentListener(new AdjustmentListener()
{
public void adjustmentValueChanged (AdjustmentEvent e)
{
tf.setText(""+sb.getValue());
3
}
});
b.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
});
add(p);
setSize(200,100);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
public static void main(String args[])
{
ScrollbarTest scroll = new ScrollbarTest();
}
}
4. Aufgabe
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Zins4 extends Applet implements ActionListener
{
int n=1;
Panel bereich1, bereich2;
Label l1, l2, l3;
TextField akapital, zins;
Button b;
Label zeit[]
= new Label[11];
Label kapital[] = new Label[11];
public void init()
{
bereich1 = new Panel(); bereich2 = new Panel();
add(bereich1); add(bereich2);
bereich1.setLayout(new GridLayout(13,1,10,10));
bereich2.setLayout(new GridLayout(13,1,10,10));
bereich1.setBackground(Color.yellow);
bereich2.setBackground(Color.yellow);
l1=new Label("Anfangskapital");
bereich1.add(l1);
akapital = new TextField(" ", 8); bereich1.add(akapital);
l2=new Label("Zinssatz");
bereich2.add(l2);
zins = new TextField(" ", 8);
bereich2.add(zins);
l3=new Label("Kapitalwachstum"); bereich1.add(l3);
b=new Button("berechnen");
bereich2.add(b);
b.addActionListener(this);
for (int i=1; i<11; i=i+1)
{
zeit[i] = new Label(" ");
kapital[i] = new Label(" ");
bereich1.add(zeit[i]);
bereich2.add(kapital[i]);
}
}
public void f(double k, double p)
{
if (n<11)
{
4
k = k * (1 + p/100);
zeit[n].setText(""+n);
kapital[n].setText("" + k);
n=n+1;
f(k,p);
}
}
public void actionPerformed(ActionEvent e)
{
String kapitalString=akapital.getText();
double ka=Double.valueOf(kapitalString).doubleValue();
String zinsString=zins.getText();
double zi=Double.valueOf(zinsString).doubleValue();
f(ka,zi);
}
}
5. Aufgabe
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class Spots extends Thread
{
Color farbe;
Graphics g;
int mx, my;
int change;
int radius;
// Der Konstruktor registriert die Farbe des Threads
Spots(Color c,Graphics g,int mx,int my,int change,int radius)
{
farbe = c;
this.g = g;
this.mx = mx;
this.my = my;
this.change = change;
this.radius = radius;
}
public void run()
{
while (true)
{
draw();
try { sleep(500); }
catch(InterruptedException e) { }
}
}
public void draw()
{
// Graphics g = getGraphics();
g.setColor(farbe);
// berechne einen neuen Punkt und
// zeichne ihn
mx = (int) (Math.random() * 1000) % change;
my = (int) (Math.random() * 1000) % change;
g.fillOval(mx,my,radius,radius);
}
}
public class SpotTest extends Applet
{
/* Zeichnen von Punkten in verschiebenen Farben
* Veranschaulichung einfacher Threads
*/
int mx, my;
5
int radius
= 10;
int boardSize = 200;
int change;
public void init()
{
boardSize = getSize().width - 1;
change = boardSize - radius;
// Erzeugen und Start von drei neuen Threads
Graphics g = getGraphics();
new Spots(Color.red,g,mx,my,change,radius).start();
new Spots(Color.blue,g,mx,my,change,radius).start();
new Spots(Color.green,g,mx,my,change,radius).start();
}
}
6. Aufgabe
/**
* <H2>WordParser</H2>
* <P>
* Problem description:
* <P>
* A text consists of words separated by spaces or other special
* characters (like signs or symbols). We are interested in getting
* the words, one by one, without the surrounding extra characters.
* <P>
* WordParser is a class that allows an application to break a String
* into words. Words are defined as contiguous sequences of letters
* and/or digits. All other characters are treated as "white space"
* and are thrown away.
*/
public class WordParser {
private String text;
private int currentIndex;
//
//
//
//
// text to be parsed
// current position within text String
Note: Between calls of nextWord() currentIndex points to the first
character of the next word. If there ar no more words available then
currentIndex points to the position after the last character of
the text String; i.e. currentIndex == text.length()
/**
* Construct a WordParser for a given String.
*/
public WordParser (String text) {
this.text = text;
currentIndex = 0;
skipWhiteSpace ();
// be prepared for the first request
}
/**
* See whether there are more words available.
*/
public boolean hasMoreWords() {
return currentIndex < text.length();
}
/**
* Return the next word.
*/
public String nextWord () {
if (hasMoreWords ()) {
String word = String.valueOf(text.charAt(currentIndex));
currentIndex++;
while (currentIndex < text.length() &&
Character.isLetterOrDigit(text.charAt(currentIndex))) {
word += String.valueOf(text.charAt(currentIndex));
6
currentIndex++;
}
skipWhiteSpace ();
return word;
} else {
return "Error!";
// instead of 'return "Error!"' we should:
// throw (new java.util.NoSuchElementException());
}
}
/*
* Skip white space (i.e. all characters that are not
* characters or letters).
*/
private void skipWhiteSpace () {
while (currentIndex < text.length() &&
! Character.isLetterOrDigit(text.charAt(currentIndex)))
currentIndex++;
}
}
/**
* <H2>WordParserTest
* <P>
* A test program for the WordParser class.
*/
import java.io.*;
public class WordParserTest {
public static void main(String[] args) throws IOException
{
System.out.print("\nWordParserTest\n");
System.out.println("Please enter some test sentences. " +
"Terminate with an empty line");
boolean moreInput;
do {
System.out.print("> ");
String inputString = "";
BufferedReader ein = new BufferedReader(
new InputStreamReader(System.in));
inputString = ein.readLine();
WordParser wp = new WordParser(inputString);
if (wp.hasMoreWords()) {
moreInput = true;
String word = wp.nextWord();
System.out.print (word);
while (wp.hasMoreWords()) {
System.out.print(" / ");
System.out.print(wp.nextWord());
}
System.out.println();
} else {
moreInput = false;
}
} while (moreInput);
}
}
7