COM121 2025 Lab Session 1 (week 2)
Aim
The aim of this session is familiarize the student with the Java programming environment which we
are going to use in this course.
Objectives
After completing this week’s session, you should be able to:
1. Create Java source code files using a text editor such as Notepad
2. Compile Java programs from the command-line
3. Run Java programs from the command line
4. Extract and print textual input from the terminal using command-line arguments
Tasks
1. Create a folder which you are going to use for all your Java code in this course.
2. Open the Windows Command Prompt and navigate to your Java folder
Hints: You may use the command prompt $g to tidy up your prompt. Use the cls
command to clear the console.
3. In your java folder, create a text file and name it HelloWorld.java. Make sure to type the
file name exactly as it has been given, i.e., no spaces in the name, capital H in Hello and
capital W in World.
4. Copy the following code (preferably by typing, rather than copy and paste) into the
HelloWorld.java file. Make sure to copy verbatim (that is, exactly as it appears).
public class HelloWorld{
public static void main(String [] args){
System.out.println("Hello world");
}
}
5. Save the file and compile your program by running the command
>javac HelloWorld.java
from the command prompt. If you are successful, a blank line will be inserted into the
console. If you made any mistakes, error messages will be displayed. Make the necessary
corrections until the program compiles.
6. Run the program using the command
>java HelloWorld
Describe what happens.
7. Change the program to print “Hello world” 10 times
8. Change the program to print your name instead.
COM121 2025 Lab Session 1 (week 2)
9. Create another program with the following code:
public class SimpleUserArgs{
public static void main(String []args){
System.out.print("You entered ");
System.out.println(args[0]);
}
}
10. Compile the program and run it with the following commands
>java SimpleUserArgs one
>java SimpleUserArgs two
Describe the behavior of the program.
11. Describe what happen when you run the program using the command
>java SimpleUserArgs
12. Describe what happen when you run the program using the command
>java SimpleUserArgs one two
13. Modify the program in 9 as follows
public class SimpleUserArgs2{
public static void main(String []args){
System.out.print("You entered ");
System.out.print(args[0]);
System.out.println(args[1]);
}
}
Note: File name must match program name!
14. Run the program with the following command and note the output.
>java SimpleUserArgs2 one two
15. Modify the program so that command
>java SimpleUserArgs2 one two
produces the output
You entered one two
COM121 2025 Lab Session 1 (week 2)
16. Modify the program so that when user enters three names, it should print the names in
reverse order. For example:
>java SimpleUserArgs3 Opa Mphatso Lusungu
should produce
Lusungu Mphatso Opa
Note: File name must match program name!