Lab 0: CS136 Warm-Up Exercises

We provide these exercises to help you refresh your memory about the Java programming language, and to warm you up for the first lab. If you have never programmed in Java before, or if it has been a long time, doing the steps in these exercises are especially recommended. However, this is an optional exercise, and you do not have to do it if you do not want to. The reason we are offering this exercise is that, over the years, many students have asked if there is preparation they can do ahead of the class. Indeed there is! We want you to succeed, and so we are happy to oblige with this document.

These exercise constitute a small subset of Lab 1, which will be due in the second week of class. Therefore, another incentive for doing this work is that it will give you a head start on the first assignment.

These exercises omit certain details that we will cover when we officially start class. These omissions are intentional. We would like you to discover and explore these steps yourself. By doing so, you will develop a sense of confidence and autonomy that will serve you well throughout the semester. There is no way to fail this lab. The only failure you can experience is if you do not try at all! If you get stuck, make a note what you get stuck on, and we will talk about it at the start of the semester. In fact, if you’re ready to get unstuck when the class starts, you will be in an ideal position to hit the ground running!


Step 1: Install Java

During the semester, you will have access to lab machines with all software pre-installed and configured. However, every student of computer science should know how to install software themselves. In this step, you will install Java on your own computer.


Step 2: Hello world!

Once you have Java installed, your first order of business is to write, compile, and run a simple Java program.

You should start by opening the text editor of your choice. You can use any text editor you want, provided that it can save documents in “plain text format.” Microsoft Word is not a good choice, but some good choices are gedit (Linux), TextEdit.app (macOS), nano (Linux/macOS), emacs (Linux/macOS), and Notepad (Windows). If you feel adventurous, you can download and install the editor we plan to use for class.

Here is the program that you should write, compile, and run. You are strongly advised to retype this program into your text editor. Retyping instead of copying and pasting will encourage you to notice certain details that you might not ordinarily notice.

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

Save this code in a file called HelloWorld.java and note where you saved it. Note that, unlike in Python or some other language, capitalization and punctuation matter in Java.

Next, open command-line console on your computer. The details will vary depending on which operating system you run. On the macOS, you want to open a program called Terminal.app. On Windows, you want to open the program called Command Prompt or cmd.exe. On Linux, you want to open the Terminal or xterm programs. In each case, you will be greeted by a window containing a text prompt. Mine looks like this, but don’t worry if yours looks a little different:

As a convention, in this class, we will indicate the command line prompt using a dollar sign, $. So when you see instructions like the following,

$ ls

it means that at the prompt, you should type the command ls. When I type ls and press Enter, I see:

$ ls
Desktop		Documents	HelloWorld.java	Library		Music		Pictures

This output means that there are files called Desktop, Documents, etc. in my current working directory. Terminal commands are often run on files in your filsystem, so when you open a terminal, by default, it assumes that you want to work with files in your home folder. Therefore, your default current working directory, is usually your home folder. The files shown above are the ones in my home folder. ls is short for “list”, so we usually call its output a directory listing.

You might recognize that some of the files in the directory listing above are, themselves, directories. How can you tell the difference? Try running ls with some extra arguments:

$ ls -l
total 8
drwxr-xr-x  2 dbarowy  staff   64 Jan 25 13:11 Desktop
drwxr-xr-x  2 dbarowy  staff   64 Jan 25 13:11 Documents
-rw-r--r--  1 dbarowy  staff  235 Jan 25 13:19 HelloWorld.java
drwxr-xr-x  2 dbarowy  staff   64 Jan 25 13:11 Library
drwxr-xr-x  2 dbarowy  staff   64 Jan 25 13:11 Music
drwxr-xr-x  2 dbarowy  staff   64 Jan 25 13:11 Pictures

See the cryptic text all the way to the right? It looks like drwxr-xr-x. If that text starts with a d, the entry in the listing is a directory. If it starts with a dash, -, like -rw-r--r--, then the entry is an ordinary file. So here, you can see that HelloWorld.java is a file.

If your HelloWorld.java file is not in your current working directory, as mine is, you will need to change directory in order to navigate to it. For example, if I wanted to change directory into my Desktop, I would type:

$ cd Desktop

By alternately using the ls and cd commands, you should be able to navigate your files in exactly the same way that you navigate them when you are pointing and clicking with a mouse.

Once you’ve found your HelloWorld.java program, now comes the fun part. We are going to compile our HelloWorld.java program into a form that can be run by the computer using the javac command. We call this step compilation. Type:

$ javac HelloWorld.java

If everything goes smoothly–if there are no errors in your program–you will see output like this:

$ javac HelloWorld.java 
$ 

In other words, you will see no output when things go right. In the terminal, it is conventional for programs only to produce output when things go wrong. If you get output, that’s javac’s way of telling you that there was an error and that you need to fix it. javac goes to great lengths to tell you what went wrong. Don’t be afraid of what javac prints on your screen. Read it, because it will probably help you find your issue!

For example, if I see

$ javac HelloWorld.java 
HelloWorld.java:3: error: ';' expected
        System.out.println("Hello world!")
                                          ^
1 error

then javac is telling me that at the location indicated with the ^, it was expecting a ; character, but it did not find one. Putting in a ; character at that location makes my program compile without errors.

Once you have successfully compiled your program, you should see a new file in your directory:

$ ls -l
total 16
drwxr-xr-x  2 dbarowy  staff   64 Jan 25 13:11 Desktop
drwxr-xr-x  2 dbarowy  staff   64 Jan 25 13:11 Documents
-rw-r--r--  1 dbarowy  staff  426 Jan 25 13:33 HelloWorld.class
-rw-r--r--  1 dbarowy  staff  235 Jan 25 13:33 HelloWorld.java
drwxr-xr-x  2 dbarowy  staff   64 Jan 25 13:11 Library
drwxr-xr-x  2 dbarowy  staff   64 Jan 25 13:11 Music
drwxr-xr-x  2 dbarowy  staff   64 Jan 25 13:11 Pictures

Do you see it? It is HelloWorld.class. This is a compiled Java program, which we can now run. So let’s run it.

$ java HelloWorld
Hello world!

We’ve run our first program!


Step 3: Hello Odd World: Writing Your First Program.

In this step, we will write, compile, and run a Java program that prints the first ten odd numbers.

  1. Create a new file named Odd.java. That file should contain a class called Odd, and that class should include a method called main.
  2. Once you have written your program, compile it using

     $ javac Odd.java
    
  3. After successfully compiling your program, when you type ls, you should see a new file called Odd.class.
  4. To run your code, type

     $ java Odd
    
  5. The output should look something like:

     $ java Odd
     1
     3
     5
     7
     9
     11
     13
     15
     17
     19
    

We leave the details of writing this program to you. If Java is new to you, think about how you would write this program in the language that you do know. You might be surprised to discover that Java is probably not that different.

Although we have shown you some simple steps here, you will need to discover the rest on your own. The course textbook, downloadable here, contains an excellent set of Java tutorials in Appendix B, titled “Beginnig with Java.” If you’re stuck, give it a try!


Submitting Your Lab

You do not need to submit this lab. It is solely for your own benefit. As mentioned earlier, when it is time for you to complete Lab 1, you can reuse some of the work you did here, and we will provide complete instructions for doing so.


Bonus: UNIX permission bits

Although we briefly touched on the cryptic text that appears in your directory listings (you know, the stuff that looks like drwxr-xr-x), there’s a lot more information than we discussed. What does the rest of it mean? See if you can find out.

The terminal also comes with a built-in documentation system called the “manual pages.” We often refer to this documentation as man pages for short. What happens when you type the following?

$ man ls

What else do you learn about your system?

  • CSCI 136, Spring 2020