CS 0401 – Lab 1

In this first lab, we will introduce the computing environment in the lab classroom, give you some hints about how to set up Java at home, and walk you through the submission process that we will use for labs and projects throughout the term.

Getting Started With Programming in Java

The computers in our lab are running Windows 8 and are already configured with the tools we will need to create and run programs written in Java.

After you log in, you’ll be presented with the large Windows start menu. From the Utilities column (you may need to scroll to the right to see it), launch the Notepad++ application.

Text Editing

Notepad++ is one of many programmers’ text editors. The source code for Java is just plain, human-readable text. There’s no formatting or styles like bold or italic or underline, just letters, punctuation, and whitespace. We could then, write programs entirely in a basic, no frills text editor like Windows’ own Notepad. However, the specific task of programming has some common challenges that these more-specialized programs like Notepad++ hope to make easier on us.

You may get a dialog box asking you to update some plugins. It’s doubtful this would install correctly due to us not having permissions to install software on the lab computers, so let’s just skip that. At home, if you decide to use Notepad++, you can certainly update when it prompts you to.

You are now able to type something in. So let’s type in the Hello World program we saw in class. Here’s the code if you need it:

class Hello {

      public static void main(String[] args) {

            System.out.println(“Hello, world!”);

      }

}

 

I suggest that each indented line be indented with a tab. So before public, hit tab once. Before System, hit tab again. You’ll note that you didn’t have to hit tab twice since when you hit enter it remembered your previous level of indentation. This is something a basic text editor wouldn’t do that a programmer’s text editor like Notepad++ will do for you.

You might also notice it highlighting the left parenthesis when you type a right parenthesis. Or the same when you type the curly braces ({ and }). These help you to know which open grouping character you are closing so that you can have the right number when there are many.

But there’s more things that a programmer’s text editor can do if it knows what programming language you are writing in. Right now, Notepad++ doesn’t know that you want to program in Java. You can tell it one of two ways, you can either save the file with a .java extension, or you can go to the Language menu and tell it which language you’re programming in.

So let’s try both. First, go to the Language menu, select the J submenu and then choose Java.

All of a sudden your plain text no longer looks that plain. Notepad++ does what is called syntax-highlighting. This means that when you type something special, a reserved word, a string, a number, etc., something that has meaning in Java, it will turn a different color than the identifiers and expressions we write. That gives us strong hints about the structure of our code and reminds us to not name variables with words that Java forbids.

Notepad++ also does something else. After the System.out.println(“Hello, world!”); line, hit enter and add a new line. Let’s have you print the string, “Goodbye, cruel world!”. As you start to type System, you’ll note that it pops up a box that suggests what you might be typing, similar to how your smartphone might. At any point, you can hit the TAB key to have it finish typing what you started if the highlighted selection is what you want. If it isn’t, you can keep typing to get better suggestions, or use the mouse to select a different option.

Note though, that just because it doesn’t suggest something, doesn’t mean you’re typing something wrong. It really only remembers things you’ve already typed that you might reuse or some common snippets it can guess at.

Syntax highlighting and auto-completion can make us very productive programmers. But make sure that they don’t become a crutch – you still must know the language well enough to write code on paper. One of our goals is fluency so that even if we don’t have the tools we are used to, we can still get work done.

Compiling and Running Java

Now save the file you are editing with the name Hello.java

The default location to save is probably your documents folder and we are okay with that. If you’re somewhere else, navigate to your documents and save there.

Notepad++ is not a full Integrated Development Environment (IDE). An IDE would give you one-click access to the compiler and to run the resulting program. It would have syntax highlighting and even better auto-completion. However, they are complex and can hide the details of what you are doing to prevent you from learning the basics. We won’t advocate using an IDE in the class, so we are going to need direct access to the compiler to build our program.

Bring up the Windows Start menu either by dragging your mouse all the way into the lower left-hand corner or by hitting the Windows key on the keyboard. From there, find the System/Accessories category of applications and choose the Command Prompt to run.

You’ll now have a small, black window pop up with a cursor that is prompting you to type a command. This is a command-line interface and it is an older and (in some ways) simpler way of interacting with the computer than the graphical user interface (GUI) than what you’re maybe used to.

The line showing lowest on the screen, where the cursor is blinking, tells you what directory we are currently in. We want to go into our Documents directory where we saved our Hello.java file. So we must issue a command rather than clicking on an icon. That command is “cd” which stands for change directory. We tell it what the name of the directory is and hit enter. So type:

cd Documents

 

And now the text at the current prompt should say that you are in the Documents directory.

The lab computers have been configured with the software and settings necessary to compile and run java. We can test that by asking what version of java we have installed. Execute the command:

javac -version

 

It will likely report back a version that starts with 1.7.0_ and some other numbers that indicate that bugs have been fixed several times. The latest version of Java is actually 1.8 and so if you do this at home, you might find a bigger number. That’s okay. In this class, we’ll mostly be okay with version 1.6 or later.

That was just a test to see if everything was okay. We don’t need to run that command that way to build our program. Let’s do that now. Type the command:

javac Hello.java

 

and hit enter.

The compiler (javac) follows the Unix model of program output. If you see no output on the screen, nothing went wrong.

If you do see a message, it is telling you that you have typed something against the rules of the Java language. It will tell you what line it guesses you messed up on. It’s not always right, but it is usually close. Double check what you typed with what the program was supposed to be, save again, and try to recompile.

When you succeed, you’ll have produced a new file, Hello.class. You can see the list of files in the current directory by typing:

dir

 

With your code successfully compiled, we can now run it. We use the java command plus the name of the class file we just produced, minus the .class extension. So for our program, we would say:

java Hello

If you mistakenly put the .class extension or you try to run the .java file without compiling, you’ll get an error such as “Error: Could not find or load main class Hello.class”.

Turning it in

Open up Windows Explorer by clicking the folder icon in the taskbar. Navigate to Documents if you aren’t already there so that you can see the Hello.java and Hello.class files you created.

To submit, we are going to need to create a zip file and upload it to the class submission site.

To create a zip file, select the files you want to compress, which in this case is just Hello.java. (We can create Hello.class from that, so no need to submit it.) Right click and say Send To -> Compressed (Zipped) Folder. This will create Hello.zip.

Now, go to the submission site in your preferred web browser.

http://intranet.cs.pitt.edu/~jmisurda/submit/

Log in with your @pitt.edu email address (include the @pitt.edu part)  and peoplesoft number.

Click the Browse… button and navigate to your Hello.zip file in your Documents folder. Select it for opening and then on the webpage click the Send button.

If you try this from off campus, you will be met with an Unauthorized error page. Go instead to:

http://www.cs.pitt.edu/~jmisurda/submit/

 The directions on that page will show you how to access the submission site.