Practical 1: Basic Java Programming - University of York

[Pages:14]Programming Refresher - MSc Computing & IT

Practical 1: Basic Java Programming

Lecturers: Alvaro Miyazawa and Ana Cavalcanti Material Prepared by Rob Alexander

The aims of this practical are: To get you up and running with both the command-line Java tools and the Eclipse IDE; To get you comfortable with the very basics of Java.

Exercise 1: Hello World

New Challenge: Compile and run a minimal Java program from the command line.

The aim of this exercise to create, compile and run the simple `Hello World' Java program on Linux or Windows (not both ? one is enough), using a text editor and the JDK command-line tools.

/* * The HelloWorld class implements an application that * simply displays "Hello World!" out to the standard * output on the computer, i.e. the monitor */

public class HelloWorld { public static void main(String[ ] args) { System.out.println("Hello World!"); }

}

HelloWorld.java

1.1 Hello World on Linux

Task

a) Log on to Linux and open a terminal window;

b) Create a directory called "PREF" in your home directory and store all the programming exercises there; [hint: mkdir]

c) Open an editor (e.g., nedit) and type the code of HelloWorld.java;

d) Save the source file as HelloWorld.java into the PREF directory; Be careful with upper and lowercases.

e) Go to the command line, move to the directory where the file is saved; [hint: ls, cd]

f) Compile your program, by typing: javac HelloWorld.java; If you have done it properly, a file HelloWorld.class should now appear in your directory and the compiler (i.e., the javac command) should not have printed any message (depending on compilers and/or versions a message might appear to say that no errors were found); If you receive an error message check that:

1

Programming Refresher - MSc Computing & IT

o You saved the file with the right name; o You saved the file in the same directory where you issued the javac command; o You typed the file exactly as it appears above. g) After successfully compiling the program, run it by typing: java ?cp . HelloWorld The "-cp ." option helps the java command to find your compiled file; You should now see "Hello World!" on the screen. If so congratulations! You have now compiled and run your first program in Linux.

1.2 Hello World on Windows

Task a) Log on to windows; b) Create a directory called "PREF" in H: and store all the programming exercises there; c) Open Notepad and type the code of HelloWorld.java as per page 1; d) Save the source file as HelloWorld.java into the PREF directory;

Be careful with upper and lowercases.

e) Open a DOS Command Prompt (Start > Run, then type cmd and click OK); f) From the command line, move to the directory where the file is saved; g) Compile your program, by typing: javac HelloWorld.java.

At this stage you may get a message like "javac is not recognized as an internal or external command [...]" this means the Environment has not been set up properly;

To fix this problem, type: o path=C:\Program Files\Java\jdk1.7.xxx\bin o Note jdk1.7.xxx needs to be replaced with the correct path; o This command will tell the computer how to find the javac command.

Compile the program again by typing the command above.

h) If you receive an error message check that: You saved the file with the right name; You saved the file in the same directory where you issued the javac command; You typed the file exactly as it appears above.

i) After successfully compiling the program, run it by typing: java ?cp . HelloWorld Remember the "-cp ." option helps the java command to find your compiled file; You should now see "Hello World!" on the screen. If so congratulations! You have now compiled and run your first program in Windows.

2

Programming Refresher - MSc Computing & IT

Exercise 2: Modifying Hello World

New Challenge: experience for yourself what is and what isn't a valid Java program.

2.1 Introducing some changes

The best way to understand program code is to experiment by making incremental changes and observing the result. Accordingly, you will now make minor changes to your Hello World program and observe whether it can be compiled and run successfully. In cases where it does not compile or run, please take note of any error messages displayed. See if you can relate them to the error you have introduced!

Task Edit your program code to make the changes below one at a time, such that after each change you:

Save as HelloWorld.java; Recompile source file (try to understand any error messages produced); Run the class file (if the compile was successful); Undo the change (so that you're ready to try the next change). The Source Code Changes: a) Change the public keyword to public; b) Change the class name from HelloWorld to Helloworld, or helloworld; c) Remove one of the semicolons (;); d) Remove /* from first line; e) Change main to mmmain; f) Modify the program to display your full name instead of "Hello World!". You should have noted that modifications (a) to (d) did not compile, as the changes invoked compiletime errors. Change (e) should compile, but should not run. This is a runtime error (definition: either the program does nor run, or if it does it gives an incorrect result). Runtime errors are trickier to pinpoint than compile-time errors; good design and thorough testing go a long way towards finding them. Why do you think the Java Interpreter did not like change (e)?

3

Programming Refresher - MSc Computing & IT

2.2 Runtime Arguments

The header of the main method specifies a run time parameter array, i.e. String[ ] args. public static void main(String[ ] args) {

This parameter array will receive and store any information entered into the command line, when requesting the program to run. This information can then be used within the program. The tasks below will help you understand better the use and implementation of parameter passing.

Task Before undertaking the tasks below, make sure you revert your Hello World java program back to its original source (as per page 1) and successfully compile it.

a) At the command line, type: java HelloWorld PREF; Here we are giving some extra information (i.e. the string PREF); This has no effect on the program's behaviour, however, because we have not coded how to use the information.

b) Modify the System.out.println statement such that you replace "Hello World!" with args[0];

c) Compile it as usual (you should not get any error messages!);

d) Run the program by typing: java HelloWorld PREF; Now instead of Hello World! you should see PREF displayed.

e) Run the program again by typing: java HelloWorld; You should see an error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at HelloWorld.main(HelloWorld.java:12); This is because we did not send any information to be used by the parameter; In programming if you code to use a parameter, then you must make sure that a corresponding argument is passed.

f) Run the program again by typing: java HelloWorld Hello World Can you see "Hello World!" on the screen? Why not?

g) Modify your program to display two arguments. [hint: use args[0], args[1]] Compile it; Run by typing: java HelloWorld Hello World

The advantage of using arguments is that you can display different messages without having to modify and recompile the program.

4

Programming Refresher - MSc Computing & IT

Exercise 3: Eclipse for Java

New Challenge: use Eclipse to compile and run the Hello World program Eclipse for Java is an IDE that allows us to create, compile, run and debug Java programs. As an introduction, this exercise asks you to create and run the `Hello World' program as a project within Eclipse. Note you can use Eclipse on Windows, Linux or Mac OX, however, the screenshots within this practical are of the Windows version.

3.1 Starting Eclipse

Task a) Start Eclipse for Java.

Before the IDE starts, Eclipse will prompt you to confirm the location of the workspace.

The workspace will be the default save location for Eclipse projects. Task a) Browse for your PREF folder; b) Select the PREF folder to be the workspace; c) Check the checkbox, to make PREF the default workspace folder; d) Click OK button.

5

Programming Refresher - MSc Computing & IT You should now see the Eclipse Start Page.

Now we want to go to the Eclipse Workbench (i.e. the work area of the IDE). Task a) Click: The go to workbench icon.

You will now see the Workbench, i.e. the IDE.

6

Programming Refresher - MSc Computing & IT

3.2 Starting a new project

Within Eclipse all work is contained within a project, regardless of whether the project contains a single class or multiple sub-projects. Accordingly we need to start a new java project

Task a) Click: File (menu) > New > Java Project;

b) In the New Java Project dialogue window, type Hello World as the project name;

c) Click: Finish. 7

Programming Refresher - MSc Computing & IT Hello World should now be listed in the Package Explorer Pane.

3.3 Starting a new class

Within this project we need to add a new class, in order to create a source file. Task a) Right Click: The Hello Project; b) Click: New > Class;

The New Java Class dialogue will be displayed (see page 9).

8

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download