Object-Oriented Programming 2023/24

Exercise 5


back


Project

Continue working on your project according to the instructions provided so far. Read the instructions on working program version. You should have been provided with the link to the GitHub Classroom within which you should create your own repository for your working program version.

Consider applying design patterns. You may apply also different ones than those we talked about in lectures. Often, a need for a particular design pattern shows up after the initial implementation. Do not hesitate to apply a design pattern additionally, by which you would do so-called refactoring: you will change the code, but you will retain its behavior.

While using your program, objects that represent provided or derived data should be created in the memory. In order avoid repeating the activities that lead to the creation of these objects upon each program start, you may store the corresponding objects using serialization.

^


Tasks

  1. In the game with ogres and knights replace the ArrayList type with the LinkedList type (in only one variable or everywhere) and assure that the game still works.
  2. Try an example of generic singly linked list. Implement the deleteNext(SLLElement<T> sll_e) method to delete the element which follows the sll_e element.
  3. Consider the calculateEnergy() method of the Clash class in the game with ogres and knights. This method has one parameter of the List<? extends Energy> type. Replace this type with the List<Energy> type. The compiler reports an error (to see it, in the Eclipse environment switch from the Console tab to the Problems tab). What's the problem?
  4. The Composite pattern is implemented in the game with ogres and knights. This pattern is for now applied to the forming of the organizational structure of the knight expedition, but it could have been applied to ogres, too. Make this adaptation according to the following steps:
    1. In order to enable relating squads to both knights and ogres, a unifying interface is necessary. Create an empty interface named Warrior.
    2. Is it possible to make the getWarriors() method of the CombatUnit interface return both knight list and ogre list? The same return value type should be provided in this method implementation in the WarriorSquad class (hint: see the previous task). Since we don't know whether a squad will be a knight squad or ogre squad, the local individuals list must be of a general type List<Warrior>.
    3. If you have solved the last task successfully, the getWarriors() method of the Knight class remains valid. Add an analogical implementation to the Ogre class.
    4. It is sufficient to embrace a conversion of the acquired warrior list to the knight list in the Clash class, i.e., to replace this line:
      knight = knightExpedition.getWarriors();
      by this line:
      knight = (List<Knight>) knightExpedition.getWarriors();
    5. You can also try distributing ogres from their squads. It is sufficient to copy and modify a bit the initialization loops for knights.
  5. Experiment with the example of object serialization. Add further elements. See what happens when you declare the n attribute of the Data class as transient. Try the list serialization example. Notice that the serialization in the game with ogres and knights also embraces lists. (An alternative to this task is to apply serialization in your own project.)
  6. Try the examples of working with the Java input/output system. In particular, notice the following:
    1. the folder filtering example (what design pattern is applied there?)
    2. how a stream is being opened and closed
    3. wrapping streams for their effective use
    4. the standard input/output system
    5. new input/output system (nio) and memory mapped files

^


Working with JAR Archives

If you want to provide your application in a compact form, you can pack it into a so-called JAR file. The details of working with JAR files are provided in the corresponding tutorial. We are going to take a look at an example of creating a simple JAR archive out of the game with ogres and knights (use your own version or the last lecture version):

  1. Compile the source files. Locate the bin folder in the Eclipse workspace folders with class files.
  2. Start a command prompt. Go to the bin folder.
  3. Issue this command:
    jar cfe game.jar gui.ClashtGUI *
    With this command, you'll pack all the files in the current folder into a JAR archive named game.jar. The switches have the following meaning:

You run the JAR file by the following command (possibly even by a double click if there is a correct association with JVM):

java -jar game.jar

^



Valentino Vranić
vranic at stuba.sk