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.
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).
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.
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.
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:
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.
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.
(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.