CS401 Lab 1: Getting Started with Java and the Mac Lab

Important Note:  I strongly recommend reading over and printing out this lab PRIOR to your first laboratory session.  I also recommend thoroughly reading the Mac OSX Tutorial and the Pico tutorial to familiarize yourselves with the Mac OS and with a simple console-based text editor.  If you forget or have trouble printing out this lab in advance, ask your TA and he can give you a copy.


Introduction

This laboratory exercise will introduce you to the CS401 programming environment in the laboratory in 6110 Sennott Square. You will use the Mac Mini  computers (with Max OSX) and the Java Software Development Kit (JDK).

Max OSX (which is a UNIX system) and SDK are powerful systems that will take some effort to learn. Count on spending several hours becoming comfortable in this new environment. I hope that you can do this with a minimum of frustration, but you should remember that even the best programmers experience some difficulty when they move to a new programming environment.

Although this exercise might seem simple and uninteresting, you should take this opportunity to master the tools you will need to succeed in this course. Be sure to complete all the examples in this exercise. You should also experiment with modifications to the examples, and create new ones of your own.
 


Logging In

Log in to the Macs using your Pitt account (id and password). After a few moments you should see the Mac desktop.  A lot of icons should show on the screen.  Experiment with some of these if you'd like.  Note that the windows that appear and the behavior of the mouse are not the same as MS Windows.  Experiment with these as well so that you become familiar with the desktop and the environment.

Looking at your desktop, you should see an icon in the middle of the bottom of your screen that says OpenAFS.  Click on this icon and then click on the Get Tokens button.  In the UserName and Password fields, enter your Pitt id and password.  This will enable you to access your Pitt AFS files from the Mac Mini computers.  Make sure you do this each time you log into the Macs.

Start a terminal window (which is very much like a DOS window) by clicking on the icon of a monitor on the bottom of your screen. A window with a prompt should appear.

Find and open the Safari Web Browser by clicking on the appropriate icon.  Go to the CS 0401 Web page and find this lab.  Follow the Mac OSX Tutorial link at the top of the page.  Look over some of the basic Unix commands and get ready to try them in your terminal window.


Basic Commands

While it is possible to issue commands through icons and mouse clicks, it is sometimes more convenient to type commands into a terminal window. If you are used to Windows, this might seem awkward at first, but will quickly become automatic to you.

Issue the command date, which will tell you the current date and time. You can get documentation on most commands with man, as in man date. You can view subsequent pages of documentation by hitting the space bar (or Control-C to stop reading).

The pwd command displays the path to your current directory. Try it, and it should display:

/Users/<yourid>

Where <yourid> is your Pitt AFS Id.  UNIX has a hierarchical file system. The output above says that you are in the <yourid> directory, which is in the Users directory, which is in the root directory (/) of the Mac. The directory (folder) structure of MS Windows was based on that of UNIX, so if you are familiar with MS Windows folders, UNIX directories should seem familiar as well.  One important difference in the two, however, is the separator used between subdirectories.  In UNIX the forward slash is used (as shown in the example above) while in MS Windows the back slash is used.

This directory above is local to the Mac that you are using (i.e. it is specific to each Mac -- not shared between them), and will not be backed up, so you don't want to save your regular files here.  Rather, you want to save your files in your Pitt AFS file space.  To get to these files, use the cd command:

cd /afs/pitt.edu/home/<X>/<Y>/<yourid>

Where

<X> is the first letter of your id

<Y> is the second letter of your id

<yourid> is your id

You can list the your files and subdirectories with the ls command.  If you have not used this account before you might have no files.  However, you should have at least two subdirectories in your main directory: public and private.  The public directory is used to store files that you wish other people to be able to read, such as your Web page files (if you have one).  The private directory is used to store files that can only be accessed by you.  All files you create for this course should be within your private directory (or within a subdirectory of your private directory).

Change to your private directory by typing

   cd private

Now type pwd to see your new current directory path.  To go back up one directory level, type

   cd ..

Experiment with the cd command so that you are comfortable moving from one directory to another in your account.  Now make sure that you are in your private directory.  Then type the commands:

  mkdir cs401
 cd cs401

This will make a new subdirectory within your private directory that you can use for your cs401 labs and projects, and change your current directory to that new subdirectory.  If you wish, you can make additional subdirectories within your cs401 directory for different labs and projects.

The command cp <FILE1> <FILE2> copies one file to another. You can copy the first Java handout from my Web directory to your current directory with the following command:

  cp /afs/cs.pitt.edu/usr0/ramirez/public/html/cs401/handouts/ex1.java ex1.java

You can view the contents of a file with the command cat <FILE>. Thus, cat ex1.java  will (very quickly) print the contents of ex1.java on the screen. If that was too fast to read, you can type more ex1.java. Hitting the space bar will display subsequent pages and Control-C will interrupt the command.

Finally, mv <FILE1> <FILE2> will change the name of a file, and rm <FILE> will delete a file.

There are many other Unix commands that may be useful to you, but the ones above should be enough to get you started.  For more information on basic Unix commands, read the Mac OSX Tutorial.  Some of the information there is somewhat advanced; for now just focus on the basic commands.  For more detailed information on the Unix operating system, read a good book on Unix such as Learning the UNIX Operating System (Peek, Todino, and Strang, O'Reilly 1997). The UNIX system includes a set of very powerful tools that are useful to any professional programmer.


Editing Files

You will need to create Java code files. You can do this using any editor you choose (there are several available -- for example emacs and vi).  In addition, there are graphical user environments (such as Eclipse, which is installed on the Macs) available.  For now, however, we will stick to simple text editors. However, for those of you unfamiliar with Unix I recommend pico, since it is easy to learn and use.  To get started with pico, first read over the following Pico tutorial.

Now practice editing with the ex1.java file that you downloaded from the handouts directory above:

    pico ex1.java

Try all of the commands to become familiar with the editor.  Don't worry about changing the file -- you can always copy it again if you need to.

When developing Java applications on these Macs, one good idea is to open two terminal windows (as explained above in the Logging In section) and set both windows to your working directory.  Then edit your file in one window using pico, and compile and run your program in the other window using javac and java (as explained below).  You can open even more terminal windows if you wish.


Compiling and Running a Java Program

As discussed in lecture, running a Java application is a two-step process.  Try this process on the ex1.java handout:

1) Compile the program into byte code via the javac command:

    javac ex1.java

2) Run the byte code via the java command

    java ex1

Now use the pico editor to type in the new Java program below.  Note that the program name is Lab1, so, by the naming rules of Java, your file name must be Lab1.java.  Thus, start your pico editor with the command:

    pico Lab1.java

 

// CS 0401 Lab1

// Practice Java program with a dual purpose:

// 1) To familiarize you with the pico editor

// 2) To familiarize you with Java syntax

public class Lab1

{

    public static void main(String [] args)

    {

        int total = 87, number = 10;

        double doubleAve;

        int intAve;

        doubleAve = ((double)total)/number; // floating point division

        intAve = total/number;              // integer division

        System.out.println("The double average is " + doubleAve);

        System.out.println("The integer average is " + intAve);

        int value1 = 3 + 4 * 5;         // default precedence

                    int value2 = (3 + 4) * 5;       // change precedence with parens

        System.out.println("Value1 is " + value1);

        System.out.println("Value2 is " + value2);

    }

}

 

Once you have typed in the program above, compile and run it using the java compiler and interpreter programs.  If you get any compilation errors, it is likely due to a typing error.  The compiler will indicate the line number of the error -- find it and correct the error, then compile the program again.
 


Java Versions

As you may know the Java language has evolved and different versions are now available.  Our lab uses a somewhat (but not the most) recent version, Java 1.5 (also called Java 5.0).  To check which version of Java you are using, type in

java -version

Most likely, you will see

java version "1.6.0_22"

followed by some other information. For this course it is important that the version of Java you use is at least 1.5 or higher.  If you have Java installed on your computer or you use some other machine, check the version to make sure it is at least 1.5.
 


What Can Go Wrong?

What can go wrong? Plenty. Here are some common problems.

You cannot access javac, java, or pico. You should ask your TA to help you set your path variable. In the meantime, you can start all of these commands using their absolute path names [ex: /usr/bin/pico  for pico -- similar for the others].  For a given command, you can find out its path by typing whereis.  For example:

whereis java

to see the location of the java command.

You have no available disk space. If you have used your allocation of disk space you might not be able create any new files. You will need to delete some files before you can continue. Ask your TA to help you with this.  It is also possible to increase your disk space quote (to a limited extent).  If you want to do this, ask at any of the CSSD Campus Computing Labs.

You have some other problem.  Ask your TA for help!
 


SDK and Windows

If you want to work at home using your own computer, you will need to install the SDK yourself. It is included in the disk with your text.  If you have a high-speed internet connection you could alternatively download an updated version from the Java web site (now at Oracle) at http://www.oracle.com/technetwork/java/index.html. Go to the page http://www.oracle.com/technetwork/java/javase/downloads/index.html for details.  Note: You may download the netBeans bundle if you wish, but you do not need netBeans if you are using some other editor / environment on your PC (ex: Eclipse or simply command line compilation).

Despite Java's well-earned reputation for portability, you should remember that there are often small differences between programming environments. If you do your work outside the CS401 lab, you should compile and test it in the lab before submitting it. Your work is expected to compile and run on those machines.
 


Submission and Grading

This lab will not be graded and requires no submission.  However, it is VERY important that you complete it so that you become comfortable with the Macs and Java.
 


Thanks to Dr. Aronis for some portions of this lab