CPSC 124 (Fall 1998): Lab 1
Introduction to Java, Linux, and the Web
WELCOME TO THE FIRST LAB for Computer Science 124: Introductory Programming, Fall 1998. In this lab, you will write, compile, and run your first Java programs. You'll also make your own home page on the World Wide Web, and you'll add an applet to that page. Since this is the first lab, you have to learn how to access and use the programs that you need to do all these things. This is the only lab in which I will explain how to do these things. After this, I presume that you'll know what to do when I tell you to edit a file, compile a program, and so on.
I encourage you to work on the labs with a partner, but I do not require you to do this. It's a good idea to have someone to talk to as you work on the lab.
At the end of the lab, you'll find a set of exercises. In general, you should not expect to finish all the exercises during the lab. Finishing the exercises is homework. In particular, I expect you to clean up any programs you write before you turn them in for grading, and I expect you to take the time to give thoughtful answers to essay questions.
You should turn in your answers to lab exercises in class on the Monday following the lab. If you work with a partner in the lab, you and your partner have the option of turning in separate reports, or turning in one report with both your names. If you turn in one report, you'll both get the same grade. (Note, by the way, that you are not required to stay with the same lab partner for the whole term.)
Outline of the Lab:
- Before you Begin
- Your Computer Accounts
- Your First Java Programs
- The Web
- Applets, HTML, and Web Publishing
- Using Email
- Exercises to Turn in
Before you Begin
If you are not familiar with using the Windows 95 interface, you might want to take some time to familiarize yourself with some of its features. You should understand how to use the Task Bar, which appears at the bottom of the screen and which looks something like this:
The Start button at the left can be used to start up programs. The other buttons in the task bar represent all the open windows and running programs. You can bring one of the windows to the front of the screen by clicking on its button. (If part of the window is visible, you can bring it to the front just by clicking on it, but the Task Bar buttons are very useful if a window is hidden.)
Most windows come with a set of three small buttons in the upper right corner: . Click on the rightmost button -- the one with the X -- to close the window. The button on the left is used to hide the window, so that only its button on the Task Bar appears. You can click on the Task Bar button to make the window visible again. The middle button is used to enlarge the window to fill the entire screen, or to shrink it back to its original size if it has already been enlarged. The middle button changes appearance when the window is in its enlarged state.
To optimize the amount of information that can fit on your screen, you might want to check the screen resolution of your computer monitor. The screen resolution is the number of pixels across the screen and down the screen. The more pixels, the more details can be displayed (although there is a limit, since you don't want the stuff shown on the screen to be too small). On the 17-inch monitors, it's a good idea to set the resolution to 1024 X 768. To do this in the Gulick Lab, click on the small monitor icon -- -- displayed near the lower right corner of the screen. You will get a pop-up menu from which you can select the resolution. Select 1024 X 768 High Color (16 bit) from the list of options.
If you are very new to Windows 95, you might want to look at the on-line Help, which you can access by clicking the Start button and selecting the Help command.
Your Computer Accounts
In the world of networked computers, you can sit at one computer and access computers from across campus and around the world. As a student at Hobart and William Smith Colleges, you have accounts on two computers, which are called chem.hws.edu and hws3.hws.edu. As a student in CS124, you also have an account on a third computer, called math.hws.edu. Having an account means that you have a user name and password that you can use to log on to the computer. You also have a directory on each computer where you can store files. And you can access files stored in other directories on the computers. Your username on each of these computers is the same. The passwords might or might not be the same.
When you log onto one of the pubically available Windows computers on campus, you are really logging onto your account on chem.hws.edu. Your directory on this computer is listed under the My Computer icon as the "M drive," with a name of the form "username$ on 'Chem' (M:)". This is your personal directory. Everyone who logs on sees their own set of personal files. No matter which public computer you log on to, you see the same "M drive". If you have a Windows computer of your own connected to the campus network, you can mount this same directory on your computer. To mount a network directory on your computer, right-click on the My Computer icon. A menu will pop up. Select the command "Map Network Drive" from that menu. You will be asked to select a drive letter. (You can use M, but you don't have to.) You also have to fill in a path. The path to your personal directory on chem is \\chem\username$ where username is your user name. Don't forget the $.
You account on hws3.hws.edu is used for email. You can log on to this computer and read your email there, by running either the program called "pine" or the program called "mail". You can use the "telnet" program to log on to hws3.hws.edu. Note that you can do this from any computer on the Internet, not just from computers on the campus network. You can also access your email with Netscape, Outlook Express, and other programs, but they must be configured to use hws3.hws.edu as the mail server.
Finally, there is your account on math.hws.edu, which you will have only for the duration of this course. You will to write all your Java programs in this course. To do this, you will log on to the computer using a program called X-Win32. This program is installed on all the publically available Windows computers on campus. (It is possible to install it on your own networked computer -- you should ask about this if you are interested.)
To run X-Win32, click on the "Start" button, go to the Programs submenu and from there to the Courseware submenu. X-Win32 is in this menu. When you start this program, you will see this logon dialog box:
It should already contain your user name. Enter your password for math.hws.edu. An "xterm" window should open. This is the window that you will use to communicate with math.hws.edu. If it does not open, you might have entered your password incorrectly. Unfortunately, you won't get any error message. To try again, click on the "X-Win32" button in the Task Bar. A menu will pop up. Go to the "Sessions" submenu and select "math.hws.edu". This will open another logon dialog box.
The main programs on math.hws.edu that you will use for this course are nedit (for typing programs), javac (for compiling Java source code files), java (for running Java applications), and appletviewer (for running Java applets). However, there are a lot of other programs on this computers. You might, for starters, want to try one of the game programs ktetris, kasteriods, or kshisen.
To use a program on math.hws.edu, you have to type a command in an xterm window. Often, this will open another window where the program will run. If you want to keep using the xterm window while the program is running in the other window, you should add a "&" to the end of the command. If you don't do this, the xterm window will lock up until you close the other window. For example, to run the ktetris program, you would use the command "ktetris &".
For more information on using your account, you should read the Web page at http://math.hws.edu/eck/unixinfo/intro.html. In this lab, you will need to know a few commands for working with files and directories on math.hws.edu. I will introduce these commands as needed. However, you should at least know about the concepts of files, directories, path names, and home directories.
The operating system on math.hws.edu is Linux, a variety of the UNIX operating system. Your interface to Linux in an xterm window is called the bash shell. If you are interested in learning more, you can look for reference books on UNIX, Linux, and the bash shell.
Your First Java Programs
The first step in each lab will be to copy a directory full of files from a public part of math.hws.edu into your personal directory on math.hws.edu. For this, you will need the copy command, "cp -r". The "-r" in this command says to copy an entire directory, not just a single file. For the first lab, you want to copy the /home/cs124/lab1 into your home directory. You can use the character ~ as an abbreviation for your home directory, so the command is:
cp -r /home/cs124/lab1 ~
Type this into the xterm window and press return. You will get your own copy of the lab1 directory and the files it contains. The name of your copy is "/home/username/lab1" or "~/lab1" or -- if your home directory is your working directory -- simply "lab1". To work with the files in the lab1 directory, you want to make this directory your working directory. Do this with the "cd" command:
cd lab1
To see a list of the files in this directory, use the "ls" command.
The file HelloWorld.java contains the basic HelloWorld program, which simply prints out the message "Hello World". Since I have not already compiled this file for you, to run the program you must first compile HelloWorld.java. Do this with the command
javac HelloWorld.java
After doing this, you should verify with the ls command that the compiler has created a file named HelloWorld.class. This is the compiled, Java bytecode version of the Hello World program. Now that the compiled program exists, you don't have to compile the program ever again, unless you want to edit it and change what it does. Run the program with the command:
java HelloWorld
The computer will say hello to the world. (Note that you say "java HelloWorld" to execute the class file, and not "java HelloWorld.class".)
For your first exercise, you will write a program that it does a computation and prints out a result. Here are some facts: Every year, 724 billion cubic yards of water flow out of the Mississippi. There are 1760 yards in a mile. There are 365 days in a year. Here is the question: How many cubic miles of water flow out of the Mississippi every day? Some Java commands that will get the job done are:
double cubicYardsPerYear = 724000000000.0; double cubicYardsPerDay = cubicYardsPerYear / 365; double cubicMilesPerDay = cubicYardsPerDay / (1760*1760*1760); System.out.println("The answer is " + cubicMilesPerDay);For a complete program, you have to put this inside the standard Java program wrapper:
public class Mississippi { public static void main(String[] args) { double cubicYardsPerYear = 724000000000.0; double cubicYardsPerDay = cubicYardsPerYear / 365; double cubicMilesPerDay = cubicYardsPerDay / (1760*1760*1760); System.out.println("The answer is " + cubicMilesPerDay); } }This should be in a file called Mississippi.java. To create this file, use the nedit command:
nedit Mississippi.java &
Don't forget the & at the end. You will be asked if you want to create the file. Say yes. A window will open in which you can type the program. This is a pretty standard text-editing window, in which you can use the mouse, keyboard, and scroll bars as you would expect. You'll notice that nedit colors some of the words in the program as you type to help show the structure and meaning of the program.
After you have typed the program, save it using the "Save" command from the file menu. Click back to the xterm window and compile your program with the command "javac Mississippi.java". If the compiler finds any errors in your program, it will print a list of error messages. Click back to the nedit window to correct the errors. Note that each error message includes the line number on which the computer noticed the error. You can use the "Goto Line Number" command in the nedit Search menu to go to a particular line in the file you are editing. After correcting the errors save the file again, go back to the xterm window, and compile the file again. (In the xterm window, you can use the up-arrow key to get back commands that you have typed previously so that you can use them again.) When you finally get the program compiled, you can run it with the command "java Mississippi". (If your program didn't contain any errors, you might want to go back and put some in, just to see what the compiler does with errors.)
Before you turn in your program, you should improve it by including comments and by making the output more informative to the user. We will discuss style requirements for programs in class. When your program is ready to turn in, you can print it using the Print command in nedit's file menu. This command is set up to print to the printer in Gulick 208. If you want to print to a different printer, you will have to enter its name in the print dialog box. Some names of other printers in other buildings include:
- wlsr105 -- HP 4000 in Williams
- lib6-2l -- HP 6 in Library 2nd floor
- lib6-2s -- HP 6 in Library 2nd floor
- lib6-3 -- HP 6 in Library 3rd floor
- liblsrb -- HP 4000 in Library 1st floor
- liblsrm -- HP 4000 in Library 1st floor
Many of the programs that you write in Java will depend on other classes besides those you write. For input and output in the xterm window, you will use the TextIO class that is described in Section 2.4 of the text. There is a copy of the TextIO.class file in your lab1 directory. The simple presense of this class file allows you to use certain subroutines in any Java programs in the same directory. In particular, TextIO.getln() can be used to read in a String typed by the user, TextIO.getlnInt() can be used to read an integer number, and TextIO.getlnDouble() can be used to read in a double number. TextIO also includes the output routine TextIO.putln that you can use instead of System.out.println if you want.
The file Converse.java contains a program that has an admittedly short and dull conversation with the user. (To read this program on the Web, click here.) Compile and run the program to see what it does. (Use the commands "javac Converse.java" and "java Converse" to do this. This is probably the last time I will tell you how to compile or run a program.))
Your second exercise in this lab is to improve this program so that it has a longer conversation with the user and does some computation with the user's answers (such as asking how old the user is and computing the year in which the user was born). You don't have to do too much. Make the program several times longer than it is now.
The Web
The World Wide Web, also called the Web or the WWW, consists of millions of interlinked pages of information on computers all around the world. It's already a useful source of information and entertainment, and it is growing all the time. Part of the excitement over Java is that it is designed to work as an integral part of the Web. You are presumably using the Web right now, since I've written the labs and notes for CPSC 124 in the form of Web pages.
It's fun just to explore the Web. One way to do this is to find a starting page and to follow links from one page to another. One of the best starting places is Yahoo, which has a large number of pages grouped by subject. An even better way to find information on a particular topic, though, is to use one of the Web search pages. These pages let you type in a word or words, and then return a list of links to pages on the Web that include the words you've listed. For example, you should try the Alta Vista search page. (After following the link, click in the text-input box, type the words you want to search for, and then click on the "Search" button. A list of links for you to try will be displayed. There is lots of help available from the Alta Vista page, if you want to learn more about it.)
Every page on the Web is identified by a URL, or Uniform Resource Locator. In Netscape, the URL of the current page is listed in the box labeled "Location" at the top of the Netscape window. If you know the URL of a page you want to go to, you can just type that URL into the "Location" box and press return. (Or use Netscape's Open Location command from the File menu, and type the URL into the dialog box that pops up.) For example, the main information page for CPSC 124 is available on the Web at this URL:
http://math.hws.edu/eck/cs124/index.html
This page contains links to various material related to the course. I suggest that you visit this page now by clicking on the above link. Add the page to your Bookmarks (in Netscape) or your Favorites (in Internet Explorer) so that you can easily find your way back to it in the future.
As one of the exercises for this lab, you are asked to use Alta Vista to find an interesting Web page and to submit the URL of that page.
Applets, HTML, and Web Publishing
Java was designed to be thoroughly integrated into the Internet and the World Wide Web. One of the things you can do with Java is write applets, which are programs that can run on a Web page in a Web browser such as Netscape or Internet Explorer. (Applet programming is covered starting in Chapter 5 of the on-line text for this course.) You won't be writing and applets in this lab, but you will compile one, run it, and include it on a Web page. In the process, you'll create your own "home page" on the Web. When you do write applets later in the course, I might ask you to publish them on your own Web page.
The applet that you will use is called Maze. To program this applet, I wrote a file called Maze.java. You will find a copy of this file in your lab1 directory. To use the applet you have to compile it to produced the compiled class file, Maze.class. Do so now. An applet is not an independent program. You need a Web page to run it on. A Web page is just a file written in a language called HTML. An HTML file includes all the text that appears on the Web page, plus special commands the tell the Web browser to add images or applets to the page, to display a line of text in a large type size, and so on. The file index.html in your lab1 directory is a simple HTML page that includes the Maze applet. If this page were published on the Web, you would be able to view it with Netscape. When you are developing an applet, you can view it -- without publishing it on the Web -- with the appletviewer program. You apply this program to the HTML file that includes the applet. To view the Maze applet, use the command:
appletviewer index.html
This will show you the applet in a separate window, but it will not show you any of the text on the Web page. Try this now, to see what the Maze applet does. Remember that this will only work if you have the compiled applet, Maze.class.
Now, what if you would like to publish this page on the Web so that anyone in the world can see it? To publish a page on the Web, all you have to do is put it in a place where a "Web server" program can see it. This program gets requests for pages from Web browsers on the Internet, and it sends back pages in response. There is a directory in your account on math.hws.edu where you can put Web pages that you want to publish. The name of the directory is www. To publish the index.html page, all you have to do is copy it to this directory. For the applet to work properly on the page, you also have to copy the applet class file. You can do this with the commands:
cp index.html ~/www cp Maze.class ~/wwwOnce you do this, your page will be visible on the Web at the address http://math.hws.edu/~username/index.html except that you should substitute your own user name for username. Use Netscape to view your page! Actually, you can get to this page with the abbreviated address http://math.hws.edu/~username/, since index.html is assumed if you don't give a file name.
Your page is not very interesting right now. You can add to it by editing the file index.html in the www directory. To do this, change to the www directory with the command "cd ~/www" and then use "nedit index.html to edit the file. I don't expect you to do much at this time. Just change the "headline" on the page and add some information to the text. But if you want to do more, you can read about the basics of HTML in Section 5.4 of the text. This section has no prerequisites, so you can read it now if you want.
By the way, if you want to have a permanent home page after the end of this course, you need to have an account on the Colleges' Web server computer, www.hws.edu. To get such an account, contact the Office of College Relations. Of course, if you decide to major in computer science, you can keep your account on math.hws.edu...
Using Email
In one of the exercises for this lab, I ask you to send me an email message. During the lab, you should make sure that you know how to do this. If you want to use Netscape for email, and if you have not already configured it, please get it done before you leave the lab.
Exercises to Turn in
Exercise 1.Turn in a printout of your Mississippi.java program. Make sure it follows the rules of good programming style that we discuss in class.
Exercise 2.Turn in a printout of your improved Converse.java program. Make sure it follows the rules of good programming style that we discuss in class, and that it follows the requirements given above.
Exercise 3. Publish your page on the Web, as described above. Don't forget to edit the HTML file and make the required changes. I will check that your page is available on the Web.
Exercise 4. When you worked with the Maze Applet above, you saw that there are three types of files involved in programming and running applets: .java files, .class files, and .html files. Write a short essay explaining what information each type of file contains, and how each type of file is used. At the end of the lab, when you published a page on the Web, why did you have to copy only the .class and .html files, and not the .java file, to your www directory?
Exercise5. Send me an email (to address eck@hws.edu). In your email, describe your background in computer science, if any, and discuss briefly what you expect to get out of the course. Be sure to include your name or names. I will email you a reply to your note, to confirm that I got it. Your email must be sent by the time you turn in your lab.
Exercise 6. Use the Alta Vista search facility, at http://www.altavista.digital.com/ to find an interesting Web page on a topic that interests you. (Don't settle for some obvious, generic page such as www.espn.com!) In your lab report, tell me what terms you entered in order to search for your page. Also tell me the complete URL for that page. I should be able to get to the page by typing the URL you give me into Netscape's Location box.