Professional Documents
Culture Documents
Practical 1: Basic Java Programming: Exercise 1: Hello World
Practical 1: Basic Java Programming: Exercise 1: Hello World
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
*/
Task
b) Create a directory called “PREF” in your home directory and store all the programming
exercises there; [hint: mkdir]
e) Go to the command line, move to the directory where the file is saved; [hint: ls, cd]
1
Programming Refresher - MSc Computing & IT
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.
Task
a) Log on to windows;
b) Create a directory called “PREF” in H: and store all the programming exercises there;
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;
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
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).
You should have noted that modifications (a) to (d) did not compile, as the changes invoked compile-
time 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
The header of the main method specifies a run time parameter array, i.e. 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.
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
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.
Task
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
5
Programming Refresher - MSc Computing & IT
Now we want to go to the Eclipse Workbench (i.e. the work area of the IDE).
Task
6
Programming Refresher - MSc Computing & IT
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
b) In the New Java Project dialogue window, type Hello World as the project name;
c) Click: Finish.
7
Programming Refresher - MSc Computing & IT
Within this project we need to add a new class, in order to create a source file.
Task
The New Java Class dialogue will be displayed (see page 9).
8
Programming Refresher - MSc Computing & IT
d) Check the method stub for public static void main(String[] args)
Note in this case we want a main method, so we might as well let Eclipse
auto code the header for us, i.e. method stub;
However in future you may have classes with no main method, so on those
occasions do not check this option.
e) Click: Finish.
9
Programming Refresher - MSc Computing & IT
The IDE will display ‘HelloWorld.java’ in the main panel. The Package Explorer panel will display
an expanded view of our project as well.
Notice that Eclipse in addition to the method stub also auto generates a block comment and an inline
comment.
The block comment is for use in generating documentation for our class (JavaDoc);
The inline comment is to remind us to type some code into the body of the method.
Many programmers prefer for line numbers to be displayed (especially when reviewing compile error
messages). To display line numbering we have to use the preferences dialogue.
Task
10
Programming Refresher - MSc Computing & IT
d) Click: OK.
You should now see line numbering against the statements in the source file. Now we are ready to
proceed by completing the source code.
Task
a) Modify the source code of your java file, so it is the same as that listed on page 1;
11
Programming Refresher - MSc Computing & IT
Compiling and Running a Java application in Eclipse are automated under a simple Run command.
If the compile fails, Eclipse will not run the application and instead will report back the compile
errors;
If compile succeeds then Eclipse automatically will run the compiled class.
So let’s now try to compile and run the Hello world application.
Task
If compile succeeds then a console panel will be displayed beneath the source file:
Note the console panel can also accept command line input, whilst the application is still running.
12
Programming Refresher - MSc Computing & IT
Write a program that declares and initialises two string variables (to different values) and then swaps
the values of the variables.
To achieve the swap you will have to use a third string variable (temp). Display the value of all three
variables before and after the swap.
Write a program that allows the user to input two numbers and then applies the five arithmetical
calculations to the two numbers.
In terms of output, display the figures in the form of a calculation. For example, if the inputs are 10 and
2.5, then the output should be like:
10 + 2.5 = 12.5
10 – 2.5 = 7.5
10 * 2.5 = 25
10 / 2.5 = 4
10 % 2.5 = 0
Do the calculations in the main method, and use a single variable for the results of the calculation.
Write a program to calculate the circumference and the area of a circle when a radius (in cm) is inputted
as an integer. The formulae you must apply are the following:
circumference = 2 x PI x radius
area = PI x radius2
Make use of PI from Java’s Maths package and output your results to two decimal places. If the radius
is 5cm then the output should look like:
13
Programming Refresher - MSc Computing & IT
Create an application which allows the user to enter the dimensions of a box (as integers) and in return
displays the volume of the box, the perimeter of the box and the surface area of the box. The formulae
you should use are:
Extra Exercise
If you’re making good progress and have time to spare, maybe have a go at PREF practical sheet 2.
14