Object-Oriented Programming 2023/24

Exercise 4



To provide input and output in your project, you can prepare a graphic user interface (even if just provisional) or you can use standard input and output, for which an example of getting command line input can help you. You can implement a graphical user interface using the JavaFX or Swing frameworks (consult using any other framework with the teacher at exercises). In any case, take care in separating the inner logic from user interface.



Instead of tasks 1 and 2, you can realize a graphical user interface in your own project. Apply also the Observer design pattern in the same way as it has been applied to the ogres and knights game.

To be used in Eclipse, JavaFX has to be installed. See the manual to installing and using JavaFX in Eclipse. JavaFX is already installed on the computers in CPU, but it's still necessary to do the steps introduced in the second part of the manual.

  1. Add another button into the window for controlling the ogres and knights game which would serve for deleting the contents of the text field in the window.
  2. Since the last lecture, in the ogres and knights game, the Observer design pattern is implemented. The Clash class itself plays the role of Subject, while the Observer role is represented by the ClashObserver interface. The EnergyOfWarriors class is the only concrete observer implemented. Create another implementation of the Observer role and the corresponding element into the game window.
  3. One of the possibilities to handle a button click event in the ogres and knights game was by an anonymous class. Anonymous classes are a special case of inner classes. Experiment with further, separate examples of inner classes which are provided with the lecture notes (besides one class, all other types are provided with no accessibility modifiers, so that you can put each example into one file and compile it):
    1. In example 1, note all the places an inner class can be created at. Delete the comment mark in the line for creating instances of the D and C class. Note that if a class is defined within a different range (block), it is unknown outside of it.
    2. Check what class files have been created during the example 1 compilation. Is there a file there that corresponds to the D class, whose creation is conditional? Will the situation change if you provide a positive parameter to the f() metho call in the main() method?
    3. Example 2 demonstrates accessing an inner/anonymous class instance using an outer interface.
    4. Example 3 demonstrates inner/anonymous classes to transfer the functionality into another context. Similarly as with the previous example, this is achieved using an interface defined in the outer context which is implemented by the corresponding inner class. This is exactly how EventHandler is implemented in JavaFX.
  4. Handling a button click event in the game with ogres and knights was realized also by a method reference and lambda expression. An example that demonstrates using method references and different variants of lambda expressions in general sense is provided to the lecture notes. Try and study all variants (by removing the corresponding comment marks).


Valentino Vranić
vranic at stuba.sk