Lab: Your First Java Program
Preparation
You will need to begin by setting up your COURSES directory. You can find instructions in the Course Directory Setup page. Note that you will only be able to do this if you are enrolled in the course and have Moodle access.
Be sure your partner has a chance to follow these steps to set up their COURSES directory, too.
If you encounter any errors, have me or the prefect contact Mike Tie to set up your account.
Exercise 1
a. In your StuWork/USERNAME
directory, create a folder named lab-09-10
.
b. Open your Text Editor of Choice and create a file HelloWorld.java
in today’s lab directory that contains the following text.
/**
* A simple "hello world" program
*
* @author Titus Klinge
* @author YOUR NAME
* @author YOUR PARTNER'S NAME
*/
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello world!");
}
}
c. Now open the Terminal
and navigate to your folder StuWork/USERNAME/lab-09-10
. If you need a refresher on how to use the terminal, feel free to review the UNIX Tutorial for Beginners.
Before we can run our Java program, we must first compile it. This produces a runnable version of the program.
d. Run the following command in your terminal. Note that you do NOT need to type the $
in the terminal; it is included to remind you that it is a prompt.
$ javac HelloWorld.java
e. Verify that the file HelloWorld.class
has been created using the ls
command.
f. Run the program by running the command:
$ java HelloWorld
Exercise 2
When learning a new language, it is useful to explore what type of errors you receive when you do something that the compiler does not expect. Explore how the javac
and the java
program work on the following inputs and what error messages you receive.
a. What happens if you rename your HelloWorld.java
so that it has a different file extension than .java
? Would javac
still compile it correctly?
b. What if the file is named differently than the class declaration of public class HelloWorld
?
c. What if the file is missing entirely?
d. What happens if you run java HelloWorld.class
instead of java HelloWorld
? Does it still work as you’d expect?
e. Is Java case sensitive? What happens if you renaming the main
method to Main
or the static
keyword to Static
?
f. What happens if we move the main
method outside of the class declaration?
Java is a pure object-oriented language and every function must be housed within a class declaration. Python is more flexible and allows you to ignore its object-oriented capabilities and program in a purely imperative fashion. This makes Java feel more restrictive and forces us to include more lines of code.
Exercise 3
We will utilize the course directory to occasionally turn in lab exercises and assignments. Doing this is simple.
a. Modify your HelloWorld.java
program to say "Hello from <YOUR NAME(S) HERE>
.
b. Navigate to the Hand-in
directory of the course folder.
c. Create a new folder named lab-09-10
and copy your HelloWorld.java
into this directory.
That’s all there is to it! I will be able to access your Hand-in
folder to retrieve your lab submission from my account.
Before You Leave
Get in the habit of emailing the code written during class to your partner. Since only one account is being used at a time, the files created will only be accessible from the user that is logged in. Later in the course, having these in-class files will be essential for reviewing and studying!