Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS401 Lab 1: Getting Started with UNIX and SDK Introduction This laboratory exercise will introduce you to the CS401 programming environment in the laboratory in 6110 Sennott Square. You will use the UNIX operating system and the Java 2 Software Development Kit (SDK). UNIX 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 At the login prompt, before you login, click on the "Options" button, then select "Session," and "Common Desktop Environment". Then login using your Pitt account (id and password). After a few moments you should see the Solaris 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 are not the same as MS Windows. Experiment with these as well so that you become familiar with the desktop and the environment. A Help Viewer window should be present on the desktop. Read through the topics listed to learn more about using this desktop. Note the EXIT icon on the bottom of the screen. This will be used to log out of your session when you are finished. Always remember to log out before leaving the lab. Start a terminal window (which is very much like a DOS window) by right-clicking on the background, then selecting "Tools," and "Terminal." A window with a prompt should appear. Basic Commands While it is possible to issue commands through icons and mouse clicks, it is 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. Login and start a terminal window. Issue the command date, which will tell you the current date and time. $ date You can get documentation on most commands with man. You can view subsequent pages of documentation by hitting the space bar (or 'Q' to stop reading). $ man date The pwd command displays the path to your current directory. For instance, it might display: $ pwd /afs/cs.pitt.edu/usr0/pdillon/public/html/cs401 You do not need to know exactly what this means, but you should be aware that UNIX has a hierarchical file system. The output above says that you are in the cs401 directory, which is in the html directory, which is in the public directory, which is in the pdillon directory, which is in the usr0 directory, which is in the cs.pitt.edu cell, which is part of afs. The directory / folder structure of MS DOS / 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. You can list the your files and subdirectories with the ls command. $ ls Backup News TA TUTORIAL bin nsmail private public 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). The cd command allows you to change directories. Change to your private directory by typing cd private Now type pwd to see that your directory level has changed. 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 then 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 copy 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/pdillon/public/html/summer06/examples/HelloWorld. java . The single period refers to the current working directory, which should be your cs401 folder. The copy command accepts any path as an argument. By not specifying a filename as the second argument here, rather a directory, the file is copied to that directory with the same file name. If a different file name is specified, the file is copied to the path location and renamed to the new file name. 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 following Unix Document. 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 code files. You can do this using any editor you choose (there are several available -- for example emacs and vi). 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 HelloWorld.java file that you downloaded from the handouts directory above: $ pico HelloWorld.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 workstations, 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 HelloWorld.java handout: 1. Compile the program into byte code via the javac command: $ javac HelloWorld.java 2. Run the byte code via the java command $ java HelloWorld 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 text uses the most recent version, Java 1.5 (also called Java 5.0). To check which version of Java you are using, type $ java -version Most likely, you will see java version "1.4.0_01" followed by some other information. The most recent version of Java IS installed on these workstations, but it is in a different location in the machine's directories. To access the most recent version of Java, you must type out the entire path to the program. Try the following command $ /usr/local/java/bin/java -version You should now see java version "1.5.0_03" Since it's an enormous pain to have to type /usr/local/java/bin/ before javac every time you compile, you have to add the path of the program to the PATH variable every time you log in. Do this by typing $ export PATH=/usr/local/java/bin:$PATH But we can have UNIX do this for us every time we open a terminal window. First change directories to your home directory. This is the default directory of the cd command. $ cd Next, edit your BASH profile script that gets executed every time a terminal window is opened (or you log in remotely). $pico .bash_profile Use the Ctrl-V command to scroll down through the file. The line we need to edit is around 300 and should look like this PREPATH=$HOME/bin Add the path or our java compiler and interpreter so that it looks like this PREPATH=$HOME/bin:/usr/local/java/bin Save the file with the Ctrl-O command and exit with Ctrl-X. The change you just made haven't effected the current terminal window yet but will effect all future windows you open. To make the changes visible in the current terminal, try the following command $ source .bash_profile Now you should see the newest java version by simply typing $ java -version Zipping Files You'll need to use the zip command when submitting projects and labs to combine all assignment files into a single archive file for submission. The command works as follows: $ zip archive.zip file1 file2 file3 ... The first argument after 'zip' is the name of the archive to create (or update) followed by a list of files that should be put in the archive. Practice using this command. When you're ready, create the ZIP file for this lab: $ zip <Your-Last-Name>.zip Lab1.java Lab1.class 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 pico using the command /usr/local/bin/pico and you can access the Java compiler and the Java interpreter using their absolute path names shown above. You have no available disk space. If you have used your allocation of disk space you might not be able to login. 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. The machine freezes. You will need to reboot the machine by pressing the stop key and a simultaneously, then type boot. This will take a few minutes. 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 Sun's web site at http://java.sun.com/. For best results, install version 5.0 Update 4, available at this link: http://java.sun.com/j2se/1.5.0/download.jsp 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. If you do NOT want netBeans, click on the choice after the netBeans download -- simply JDK 5.0 Update 4 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.