Object-Oriented Programming 2023/24

Exercise 2


This week, you start working on your project. In the lecture, you learned about the general project topic. You have to consult your project objective with your teacher at exercises. You can do this at this one and next two exercises. In order for the teacher to able to advise you, you must come with a sufficiently elaborated basis and thought through questions.

You have to submit your objective to AIS by the deadline. However, the deadline is set so that you could achieve this easily a week in advance, by which you would gain more time for project realization.


Using the On-Line Java SE API Documentation

The Java SE documentation is available on the web.

Java comes with an extensive API, i.e. application programming interface. To use this interface, you need its documentation. Here's a direct link: Platform, Standard Edition & Java Development Kit Version 17 API Specification.

You've been using the println() method. Find the documentation of this method. You may use search, but notice that this method is always called in the context of the System.out object. Therefore, find System class, and then proceed by inspecting its attribute named out, which is of the PrintStream class. Within this class, you'll find the println() method.



In tasks, you will work with the code of the ogres and knights game (use the final version, which is in the Game2 folder).

  1. Add constructors with energy as a parameter to all the ogre classes (according to the Knight class). Take the opportunity to call the constructor of the class they inherit from using the super() keyword. Modify the main() method of the Clash class so that it uses constructors of the ogre classes.
  2. Similarly as in the previous exercise, create another class derived from the BadOgre class (e.g., TerrifyingOgre) which would override the eat() method. Call the overridden eat() method using the super keyword. After this, let additional behavior follow, e.g., calling your own new method roar(), which would simply print a message. Include ogres of this new kind in the clash. Does the clash loop have to be modified?
  3. Into the Clash class add the
    int countEnergy(Energy[] e)
    method, which would be capable of counting the energy of ogres or knights supplied as arguments. Call this method in the main() method and print its return value. You must provide an Energy[] array as an argument to this method. If you'd like to provide individual objects, you'd have to wrap them into an Energy[] array like this:
    countEnergy(new Energy[]{o[47], k[2], o[99]});
    However, if you introduce the signature of this method like this:
    int countEnergy(Energy... e)
    which is a syntax expression for a method with a variable number of parameters, you may provide the objects directly:
    countEnergy(o[47], k[2], o[99]);
    With this signature, you can still provide an Energy[] array because the methods with variable number of parameters are actually realized using arrays.
  4. Organize the game project into packages. For example, put ogres and knights together with the energy interface into the characters package, and the Game class into the clash package. In Eclipse, you may create a new package by right clicking the project name in the Package Explorer view and selecting New / Package. It's sufficient to pull the elements into the package. Note that the environment reports an error. You have to put this clause into each file in the package:
    package package_name;
    Into each file that uses the elements of this package, you must add a clause by which you make this package available:
    import package_name.*;
    Otherwise you'll have to use fully qualified names, such as:
    characters.Ogre[] o = new characters.Ogre[100];
    Open the source code and observe its folder structure.
  5. Note that outside the Knight class its energy attribute can't be accessed. Set the private access to the energy and hungry attributes of the Ogre class. How do you make these attributes available? What would have to be modified if the energy attribute of the Ogre class would have to be internally represented as float with the rest of the game would continue to operate with this attribute as if it was int?
  6. Remove the comment in front of the hungry attribute of the BadOgre class. Try the ogre and knight clash now. Note that no ogre eats a knight. This is a consequence of the concept called attribute hiding: an equally named attribute hides the original one. Since Boolean attributes are implicitly false, the conditional statement in the eat() method does not execute and the ogre does not eat the knight.
  7. After going through the instruction section on compiling and running a program from a command line, try to compile the game with ogres and knights from the command line or at least find the folder of the corresponding Eclipse project (in the Eclipse working folder, so-called workspace) and observe how it is organized, i.e. the structure of its folders and where are java, and where are class files placed. Run the program from the command line using the java command. Which class you have to provide as an argument?


Compiling and Running a Program from the Command Line

For the purposes of compiling from the command prompt, the PATH and CLASSPATH system variables have to be set correctly. On CPU computers this should be ensured. In case you encounter some problems in program compilation or running, do the following:

  1. in the Start menu open the Control Panel and select User Accounts, and then Change my Environment Variables
  2. in the User variables section, create a variable named PATH with the following content: C:\Program Files\Java\jdk1.8.0_[xy]\bin (replace [xy] with the actual version number); if such a variable already exists, add a semicolon to the end of its contents and following this semicolon, add the mentioned path
  3. in the same section create a variable named CLASSPATH in which you will store only a dot (indicates the current folder): .; if such a variable already exists, add a semicolon to the end of its contents and following this semicolon, add a dot

The changes to system variables hold only for newly opened command prompts. An alternative to setting the CLASSPATH system variale is providing the class path directly during the compilation using the -classpath switch.

Compiling and Running the HelloWorld.java program

After setting the paths, you can try compiling and running the HelloWorld.java program. Save it to a working directory (create your own subfolder in a folder to which you have write access, probably C:\work).

You can open the source code file in Eclipse (using the File> menu or by simply dragging the file icon onto the Eclipse window, i.e., without creating a project) or in any other text editor (even Notepad would do). For the purposes of compilation and running the program as such, this is, of course, not necessary.

You run the compilation from the command prompt, to which we can get through the Start menu where you enter the cmd command. From the command prompt, by issuing the command:

javac HelloWorld.java

you run the program compilation. Program should be compiled successfully, and the compiler should not report any issues. The compiltion results the HelloWorld.class file. This can be run by issuing the following command:

java HelloWorld

which results in printing a greeting and today's date.

Compiling and Running a Program with Several Packages

(If this wouldn't fit into the exercise, then it should be attended in the next exercise.)

The structure of the folders that contain source and resulting files must correspond to the structure of the packages. Download an example of the program with packages and unpack it to your working folder. By this, you should get the C:\work\Project1) folder with two source files. One of them is Class1.java. This file contains an equally named class that contains the main() method, i.e., it can be run.

The other file is ClassP.java. This file also contains one, equally named class. At the beginning of this file, it is declared that it belongs to the my.pack1 package and that's why it has to be placed into the my\pack1 folder. Class1.java imports the my.pack package, which consists of a single class, ClassP.

Let's compile the program from the command prompt using javacom. Open the command prompt window and go to the C:\work\Project1 folder. Enter:

javac Class1.java

If you haven't added the current folder into the classpath system variable, you have to use the equally named switch:

javac -classpath . Class1.java

After the compilation, the Class1.class is created. You may run by simply entering this command:

java Class1

For running a program, it also holds that if you haven't added the current folder into the classpath system variable, you have to use the equally named switch.

Since you know that the Class1 class needs the ClassP class, you guess that the ClassP.java file must also have been compiled. Make sure that javac placed the compiled file there where it has found the source file, i.e., into the C:\work\Project1\my\pack1 folder.

Mostly, you would like to have compiled files separate from source files, though. Thus, create the src folder for source files and the bin file for compiled files. Run the compilation from the src folder with the destination folder introduced:

javac -d C:\work\Project1\bin Class1.java

In the destination folder, you will find all compiled files along with the folder structure that corresponds to the package hierarchy. To run the program, you must be in this folder.

Although command prompt compilation is not appropriate for developing extensive programs – thus, not even for your projects – it is good to know it. The recommended environment is Eclipse, you worked with already in the first exercise. On your own computers, you may use a different environment for Java program development, but your project has to be compilable in the Eclipse environment installed in CPU.

Valentino Vranić
vranic at stuba.sk