Javakurs/Übungsaufgaben/OOPStateCharts: Unterschied zwischen den Versionen
< Javakurs | Übungsaufgaben
Mario (Diskussion | Beiträge) (Statechart aufgabe) |
(→Aufgabenstellung) |
||
| Zeile 1: | Zeile 1: | ||
= Aufgabenstellung = | = Aufgabenstellung = | ||
[[Bild:OOPStatechart.png|thumb|Javakurs 2011 - StateChart]]. | [[Bild:OOPStatechart.png|thumb|Javakurs 2011 - StateChart]]. | ||
| − | Schreibe ein Programm, welches in | + | Schreibe ein Programm, welches in objektorientierter Weise, das hier gezeigte Diagramm als [http://de.wikipedia.org/wiki/Zustands%C3%BCbergangsdiagramm StateChart] erzeugt. |
= Hilfestellung = | = Hilfestellung = | ||
Version vom 22. März 2011, 10:02 Uhr
Aufgabenstellung
.
Schreibe ein Programm, welches in objektorientierter Weise, das hier gezeigte Diagramm als StateChart erzeugt.
Hilfestellung
Benutze die folgende Klasse als Basis für alle weiteren Zustände:
/**
* Base Class for all states
*/
class State {
// ! returns a new state, if transition is correct
public State transition(String signal) {
return this;
}
}
Wobei jeder Aufruf von transition entweder das gleiche Objekt (this) oder ein neues Objekt zurückliefert, je nach dem, ob ein Statusübergang statt findet, oder nicht. Die main-Methode könnte dann wie folgt aussehen:
/**
* Class file starting main loop
*/
public class StateCharts {
/**
* Main class, creating the state chart
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// set the state chart to be initial at state initial
State current_state = new InitialState();
// loop through the state till exit is reached
while (current_state != null) {
// print information about current class
System.out.println("Current state: "
+ current_state.getClass().getName());
// get signal for next state
System.out.print("signal to be send: ");
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String signal = reader.readLine();
// get next state if needed
current_state = current_state.transition(signal);
System.out.println();
}
// inform user
System.out.println("StateChart is at end state.");
}
}
