1. Turn on the PC
When you turn the PC on, it will start loading the operating system. The lab PCs use MS Windows as well as Linux. You might want to choose MS Windows.After MS Windows has been loaded you will see a message window prompting you to log into the system. To log in, you need to press together three keys: CTRL, ALT and DELETE. Note that on the german keyboard layout these are labeled STRG, ALT and ENTF - on the italian keyboard layout they're labeled CTRL, ALT and CANC.
After pressing CTRL, ALT and DELETE, give your username and password to log in. Choose the unibz domain to access your data from any one of the lab PCs.
To shut the PC down click Start on the MS Windows task bar on the lower left corner and select log off. After that, you can choose to shut down the PC or just to log out.
2. Access to the course web site
The course web site is online at http://www.inf.unibz.it/~calvanese/teaching/ip/.To access the web site you need to start a web browser application. You can use Firefox or Internet Explorer. Find them in the Start menu or just click the corresponding icons on your desktop!
Once the browser is running, just enter the above web site address in its location bar. Navigate the web site simply by using the mouse and clicking the links you find interesting.
To save Java programs from the web site, right-click the link to the program and select save link target as (or similar, depending on which browser you're using). Choose the folder you want to save the file into and click ok.
Try it out with Program1.java.
3. Edit, compile and run a Java program
It is possible to edit, compile and run a Java program on any operating system (including Windows) by using just the Java command-line compiler + interpreter (JDK), without the need for additional development environments.To do that on MS Windows you can use any text editor (Notepad, WordPad, etc.) to write the program code. Once the code is written, save it as a plain text giving the file the same name as the class your defining plus the extension
.java
. For instance, a file holding the classProgram1
must be saved asProgram1.java
.To compile a program file (regardless whether it's one written by you or one saved from the course web site) run the command prompt. You can find it on your desktop or in the menu under Start -> Programs -> Accessories.
At the command prompt change to the directory (aka folder) where your saved file is. For example if you've saved the file in Programs\Java\ on the E: hard disk, you need to digit the following commands to change to that directory (cd is for change directory):
Next, use the command> e:
> cd \Programs\Javajavac
(Java compiler) to compile the file:If there is no error message, the program has been successfully compiled and a file called> javac Program1.java
Program1.class
has been generated. If there have been compilation errors,javac
will show them and you need to go back correcting the errors with the text editor.To run the Java program use the command
java
(Java interpreter):Please note you must not give the extension> java Program1
.class
and Java names are case sensitive. If, for instance, the name of the class isProgram1
the commandjava program1
will throw an error.
4. Using a development environment
Open the BlueJ development environment and write a Java program inside the editor window. Note that some Java instructions have already been filled in by BlueJ. Following is an example program (Program1.java):public class Program1 { public static void main(String[] args) { System.out.println("Welcome To Java!"); } }A few tips:
Common operations performed in a development environment:
- Use the four cursor keys to move around in the editor window;
acquaint yourself with the backspace and delete keys.
- If you're using the italian keyboard layout, unfortunately you won't find these characters: {, }, [, ] and ~.
MS Windows allows for getting these characters also by holding ALT GR and giving numeric codes:Typically, on an Italian keyboard, the characters [ and ] can be obtained by pressing simultaneously ALT GR and the key with the corresponding bracket.
- 123 to get '{',
- 125 to get '}',
- 91 to get '[',
- 93 to get ']',
- 126 to get '~'.
- The Java API (application programming interface) documentation is available online at http://java.sun.com/j2se/1.5.0/docs/api/, mirrored locally at http://www.inf.unibz.it/~calvanese/teaching/java-docs/current/api/.
- Opening files
You can open a file that has been saved on disk before. The file is loaded into the development environment so you can edit it, compile it again, etc.
- Saving files
If you don't save your files to disk, they are stored only in RAM. The RAM's contents are lost if the PC crashes or is shut down, so remember to save the files you're editing from time to time. This will save yourself the hassle to enter the code all over again after a crash.
An important point is to backup the discs as well. There could be hard disk failures, notebooks can be stolen, etc...
- Compiling
To run a program it must be first compiled from the human readable program source code into a machine readable format. The result of the compilation is shown in an output window. If there are compilation errors, they are shown - you then need to correct the errors in the code and compile again.
- Running programs and showing program output
After a successful compilation, the program can be run (executed). The development environment will open a window with the program's output.
Exercise 1
Download the file Exercise1.java and
save it. Run the BlueJ development environment, open
Exercise1.java
and compile it.
The program contains errors. Fix all the errors in order to obtain a
correct program. When you're done, run the program.
Exercise 2 (optional)
" Hello "
, print it in upper case and without leading
and trailing spaces by using methods from the String class."
World! "
String object, also in upper case and without leading
and trailing spaces (see
http://www.wikipedia.org/wiki/Hello_world).