Before embarking on your first lab assignment, there are some preliminaries you must learn (in addition to the basic Web and Windows skills that got you to this page): lab use policies, how our labs are set up, how to log into the ICS computer network, how our printing system (PayPrint) works, how to read and post to news groups from our lab, and (finally!) the basics of entering, compiling and running Java programs.
So, this rather lengthy orientation helps you master these preliminaries. Weve put together a series of pseudo-lab exercises that let you (with your partner, if youve formed a team already) practice and apply the material as it is presented. Read all of this orientation section; practice the parts that are new to (either of) you, or for which you need review; do the activities needed for the class (e.g., activating your computer accounts) youve not yet done.
Pay attention to the details given here, as common computing tools or activities are often somewhat different in lab than, say, on your home computer. For example: Many of you know how to use Thunderbird or Microsoft Outlook to process email. But, since lab machines are shared, we dont use these to do email; we instead have you connect to your UCInetID account through the Web using a tool called WebMail. So though you may know how to do email, you may not know how to do it from lab.
Each of you must read a few documents stored on the Web that address ethical use of computing resources and academic honesty issues. (You may also be asked to read some of these documents in the course of activating computer accounts.) We require you to read these documents because, unfortunately, a few students in the past have abused their computer privileges or acted dishonestly and claimed ignorance of the wrongness of their actions. Do note that remaining or enrolling in this course binds you to abiding by these rules.
Go to the Web page ICS Instructional Lab Rules. Read the documents it references.
Read the UCI Academic Senate Policies on Academic Honesty and the Information and Computer Science Cheating Policy.
These documents cover several important policies about academic honesty that apply to you.
Following the instructions in the Course Reference, activate your UCInetID and ICS Windows computer accounts.
Important Note: Its possible some particulars of the network will change after this lab manual has been printed. If any of those changes affect the instructions given here, well announce them either in lecture or priority lab times.
Each first-floor ICS lab room has from 30 to 45 workstations or clients forming a local area network (or LAN); Windows XP Professional is the operating system for each station. Each machine is an IBM-PC compatible that has more than enough horsepower for the work we will be doing in this class. There are also shared printers and several shared disk drives (called file servers). Each LAN connects to other LANs thoughout the ICS buildings and, through various mechanisms, to other computer networks and the Internet.
Some machine have a diskette drive that accepts 3-1/2" diskettes in IBM format only. All have a CD-ROM drive that can read both computer and music CDs, and USB ports that can accept memory sticks and portable hard drives, among other devices. Each machine also has a large local hard disk, where we store software for your use and where you can temporarily store your pairs workonce you leave lab, you must remove all your work (and only your work, nothing else) from the hard drive; more on this below.
Through your station you can also access the class file server and use the shared printers.
A word about rocking out while you compute: Yes, its true: You can bring your own music into the lab, on CDs or MP3 players or whatever, and listen to it while you computeas long as you listen through headphones or earbuds and otherwise do not disturb your neighbors. Were amazed how loud some of you play music. Loud music can permanently damage your hearing, and can be heard throughout the lab, even if you wear headphones. So, keep the volume down. If you do not, we may forbid you from listening to music (or anything else) in the lab. (See the ICS Instructional Lab Rules for more on this matter.)
The logon state: Unlike some other local area networks, Windows requires you to log on to the network, even if you dont plan to use network resources.
When you sit down at a lab station, it could be in a number of states:
The most common state is a blue screen with a box labeled Welcome to Windows. The box contains the message Press Ctrl+Alt+Delete to begin or a dialog box labeled Log On to Windows. If you see the Welcome to Windows box, press down the Ctrl key (and hold it), then the Alt key (and hold it), then the Delete key. You will get a message reminding you to save all your important work on your H: drive (more on this below). Click on OK to dismiss the message; the Log On to Windows box will appear.
If you see a black screen, or a black screen with a Windows XP logo dancing around on it, move the mouse. It may take a few seconds, but you should then see the blue screen with the Welcome to Windows box.
If the computer seems to be in a state other than described above, or you have trouble getting the Log On to Windows box, ask the Lab Attendant on duty or your TA (if present) for assistance.
Logging on: In the Log On to Windows box, at the User name: prompt, enter your user name; at Password:, enter your password; the password will echo as dots, to protect it. (You obtained your user name and passwordwhen you activated your ICS account.) If the Log on to: box does not say UCI-ICS, click on the down-pointing arrow to the right of the box. When the menu appears, click on UCI-ICS. Then click on OK. (If you do not yet have your login and password, use the guest account login name and password for now. But do activate and use your own account as soon as you can.)
If your login and password are correct, you will be asked to wait for some seconds while the computer logs you in. Eventually, you will see a scenic background, a Recycle Bin icon, and a Start button (at the lower left of the screen). Thats it; youre logged on!
Note: You will want to change your password whenever you think your password has been compromised. To do so, hold down Ctrl and Alt and hit Delete. A dialog box will appear; choose Change Password , fill in the old password and new password (twice) as prompted, and click on OK to make the change. If your password change is accepted, youll get a message to that effect; click on OK to dismiss it. If the password change was not successful, youll be told that as well; read the message, dismiss it, and try changing your password again. Keep at it until you enter an accepted password.
Remember your password! No one can look up your passwordits encrypted. So, if you forget your password, you will have to send electronic mail to helpdesk@ics.uci.edu to request that it be set to a new one (that youve given in the message), wait a day or two for the change to occur, and then log in and change your password so that (again) no one knows what it is.
When you are done using the computer:
Be sure to exit any applications you had open.
Make sure to back up any work you wish to keep, and be sure to take any diskettes with you. Keep them in a safe place, away from magnets (like speakers or telephones), intense heat (like your dashboard), and the (filled) bathtub.
Make very sure you have deleted all of your work, but only your work, that you were keeping on the computers hard disk.
Click on Start, click on Log Off. You will be asked if you are sure you want to log off; click on Log Off. This step logs you out, and leaves the machine in the logon state, ready for the next user. Logging out can take several seconds; please be patient.
Be sure to log out; if you do not, the next person who uses the machine has access to your account! Please, always leave the machine in the logon state.
We do not require print outs of any kind in this class, but you may find you wish to print some of the course materials, such as all or part of the Lab Manual, for easier reference. (After all, you dont have to have a computer handy to read printed materials.)
ICS and NACS computer labs use the PayPrint system to provide printing services. Instructions for using PayPrint in ICS labs is at Printing in the Labs; the PayPrint system in NACS labs works the same way. Note you have to purchase a PayPrint card to use this facility.
Since the PayPrint system provides you access to printers in labs across campus, be sure to print to the station you intend, so that your output will go where you expect. (Otherwise, you might be taking a very long walk to get your output!) Its usually best to send your printout to a station in the room youre in.
You should know how to back up your work to diskette, memory stick, or portable hard drive. You can also back up your work by copying it to the H: drive. H: will be mountedmade available to youwhen you log in; it corresponds to disk storage associated with your ICS Unix account. You use the H: drive the same way as you use the C: drive. Storage there is ample for this course, but if you start storing all sorts of things there, you may find yourself out of room. You can get to the H: drive by clicking on Start, then My Computer, then double-clicking on the H: drive icon (it is under the Network Drives section of the window and is the one with (H:) as the last part of its label).
It is your responsibility to safeguard your workand that includes making backup copies of it. If you lose your work, and you dont have a backup, you will get no mercy from us: in particular, the lab exam date will not be changed for you. If you have a backup, and it too fails because of circumstances beyond your control, we will consider extending your exam date a day or two. See your lab TA if you think you are entitled to an extension because of a backup failure.
Do not write to the root of the C drive, that is, to C:\. Even it the computer lets you do soand it probably wontyou may inadvertently change or delete some files important to the computers functioning. Do not store things on your desktop. The computer lets you only store a limited amount of work there. Instead, write to the C:\Temp folder.
You can get to the C:\Temp folder by clicking on Start, then My Computer, then double-clicking on the C: drive icon (it is under the Hard Disk Drives section of the window and is the one with (C:) as the last part of its label). You may get a message warning you that the contents of the drive are hidden (for your protection); if this message appears, click on the line at the bottom of the window that says you want to show the drive's contents. The contents of the root level of the C: drive will appear. You will see the Temp folder there.
When you are done using the computer, you must delete your work and only your workfrom the hard drive. This includes any files or folders you have created (all of which should be in Temp). You probably first want to back up your work; just copy the folders and files to whatever drive letter corresponds to your backup device (A:, E: or H:). Be sure to also empty the Recycle Bin, if it isnt already empty. (The easiest way to do this is to select the Recycle Bin, right-click on the mouse, and select Empty Recycle Bin.) You can also rapidly delete files by clicking on them and then holding down the Shift key and then hitting the Delete key; when asked if you are sure you want to delete the files, click on Yes. This approach deletes the files immediately. Never, ever delete files other than your own; you could make the computer nonfunctional!
Do note: the machines in lab are remotely reinitialized as required; among other things, that means any work you leave on the C drive overnight may be irrevocably destroyed. So be sure to back up any of your work on the hard drive whenever you leave lab.
Further, if you leave your work on the hard drive, someone could copy it and turn it in as her or his worka cheating situation in which you would be (inadvertently) involved. This has happened! So protect yourself; remember to delete your work from the hard drive.
And one last, important note: It is surprisingly easy to copy, by mistake, a shortcut for a file or folder to your backup device or the H: drive instead of the actual file or folder. If you do, you will not have saved your work! Remember, a shortcut just points to your work; it is not a copy of it. If you save the shortcut, youve saved the files (or folders) location on the hard drive of the particular computer you used. Even if you return to that computer, the information will probably be gone! Be careful to copy your work, not shortcuts to it.
One feature of the Internet is its electronic newsgroups. There are thousands of them. Each is a repository of related information of interest to a group of people. Since each has an Internet address, one can post messages to a news group by sending email to it (although there are easier ways). Reading a news group requires the use of a news reader, which lets you read news groups, write messages to them, make a copy of postings to your printer or disk, and follow threads, messages that are linked because they are responses to the same initial message.
Acessing newgroups is most easily done via the Internet. In particular, Google has made a major effort to store and allow access to virtually all Usenet newsgroup postings from 1981 to the present; Usenet is the major public newgroup respository. Google Groups allows you to search these newsgroups the same way you search the Web; see Groups Getting Started Web page to, well, get started!
Of course, there are other ways to access news groups and you can access news groups using any tool you like. But whatever tool you use, be sure you know at minimum how to read newsgroups; it is a basic Internet skill.
Etiquette
There are several conventions and etiquette to using the Net, especially when sending messages, be they to news groups or people so much of the etiquette also applies to email messages.
The most famous news group etiquette document is Usenet Organization and Etiquette, part of a manual for an old news reader called NewsWatcher. Read the organization section to get a good feel for how news groups are organized and for their history and traditions. Then, read the etiquette section; following its suggestions will make your news group experience much more pleasant.
Doing work on one computer by connecting to it via another computer is called remote computing. In lab, there is a remote computing tool called SecureCRT; we use it to enable you connect to your UCInetID account from the lab, so you can use a tool called Pine to access your electronic mail.
You can also connect to the UCInetID machines from any of the NACS labs on campus, and from any off-campus computer with the appropriate communications hardware and software. Here, we tell you how to access the UCInetID machines from our lab; youre own your own to learn what you need to do to access those machines from other locations.
Pairs should not share UCInetID access; each member should use her or his own account.
To use your UCInetID from the lab, open the Start menu and select All Progams then SecureCRT, then (on the submenu that appears) SecureCRT (again). A window will appear; in front of that window will appear the Connect window, which lists the computers accessible from the lab will come forward; select Ea.uci.edu and click on Connect.
Now a New Host Key warning may appear. If it does, click on Accept & Save. Now a dialog box will come up asking you for your user name; enter it. Do not check the Remember my username box! Then a dialog box asking for your password will come up; enter your password. Do not check the Remember my password box! (If you check these boxes, the next user at this machine could have access to your UCInetID account.) The dialog box will disappear, and the screen behind it will announce that you are connected to the UCInetID computer. You are now logged in to the UCI system.
When youre done, type logout to leave the system, then select Exit from the File menu to leave SecureCRT.
(SecureCRT also has several other features; feel free to explore. You can find help with SecureCRT by clicking on SecureCRT s Help menu.)
The easiest way to access your UCI email from lab is via a Web interface called WebMail. Just go to the Webmail page, log in with your UCInetID and password, and your mail appears; it allows you to read, write, delete and search for messages, among other functions. To learn how to use it, just click on the Help link near the top center of the main mail page, then the Table of Contents link in the window that appears. That page will take you to WebMails user documentation.
This section helps you become familiar with the tools provided in lab to enter, compile and run Java programs for this course; we strongly recommend you use pair programming when completing these activities.
First, you enter a run a very simple Java program. Next, you run an incorrect programone in which we have deliberately inserted errorsand fix it so that you have a correctly running program. Third, you type in a program yourself and fix any errors you introduced in the typing until it runs correctly.
We tell you the basic steps you need to know to complete this exercise, but we leave other things for you to figure out for yourself experimentation and some trial and error are probably the most effective way to learn and remember what works and what doesnt. But dont waste a lot of time: it can sometimes be difficult to spot an error or figure out the exact command or syntax to use, so ask your TA for assistance if you get stuck.
Preparation: Bring your backup media with you or use the H: drive to save your work. Remember to back up your work!
About programming environments and command line interfaces: These days, there are two major machine contexts in which one develops programs. The newer approach is to use a programming environment. One creates a project, which keeps track of everything a program needs to have in order to compile and run. For simple programs, it typically will keep track of the files containing source code, source and object libraries (and where they are stored) and various editing, compiling and linking options. (For complicated programs, much more information will be present.) Environments provide powerful tools and features to help programmers write complex code for large commercial programs, but they can be quite confusing to newcomers. Setting up projects can be complicated and tricky, even for seasoned programmers.
An older but much simpler way to compile and run programs is through a command line interface. The steps vary somewhat depending on the programming language; with Java, there are typically three steps:
The advantage to the command-line approach is that it simple to master, but it does not have the rich and powerful features of an environment.
Either a command line or IDE approach will produce an executing program. If you prefer to use an IDE to write your programs, feel free to do so. In particular, many folks like the Eclipse IDE; you can learn about it, and how to set it up for ICS21 programming, by following the instructions provided by my colleague Rich Pattis on his Tutorial on the Windows Operating System and Eclipse IDE web page. Just go to that page and click on the Start/Stop Eclipse link in the Lecture Index column and read the information from there through the Some Useful Preferences section.
Since programs for this class are quite simple (compared to commercial software, anyway) weve found that the simplicity of a command-line interface trumps the richness and power of an IDE, in terms of getting good work done with the minimum of time and hassle. So, in the rest of this manual, we take the command-line approach.
Note that, for lab exams, you turn in only the Java codeso if you use Eclipse or another IDE, be sure you submit the Java code, and only the Java code, the exam requires, and not, say, the entire Eclipse project file. Students who use an IDE to write their programs often find it easier to use the command-line approach when taking the lab exams.
To enter a program, you may use any text editor you likeyou can even use Word and save your file as Text Only With Line Breaks. But we strongly recommend you use TextPad, a text editor we provide that is easy to use and has features to help you enter neatly formatted Java code. You can also download your own copy from www.textpad.com. It is free to evaluate and only a nominal fee to licenseand you should license it if you decide to keep a copy. In this manual, we focus on TextPad.
In this introduction, we dont expect you to understand completely how each program works. Our purpose is to familiarize you with the look of Java code and have you become comfortable using the basic features of TextPad and the commands for compiling and executing Java programs.
Executing a very simple program: We begin your introduction to Java and TextPad by having you enter, compile and run a traditional first program, one that causes the computer to print out Hello, world! on the screen.
Open TextPad: Go to Start, then All Programs, then TextPad.
Save the file as Hello.java in C:\Temp.
Yes, the file is empty, but saving the file with a .Java extension tells TextPad that you are entering a Java program. TextPad will now apply Java formatting to your work. For instance, symbols and words will be color-coded and lines of Java code will be formatted using a standard style.
Enter the following program:
// Hello, world!
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello world!");
}
}
Save it. Leave TextPad open, though.
Call up a Command Prompt window by selecting Start, then All Programs, then Accessories, then Command Prompt. This gives us the interface to Windows XP we need to use command lines to compile and run Java programs. You'll see a blinking cursor next to the command line prompt. The prompt tells you the interface is ready to receive your commands. It also tells you where you are within the computers directory (folder) structure; this is known as the directory path, or just path, for short. For instance, the window usually shows C:\>Temp as the prompt. This means you are in the C drive, inside the Temp folder. When you issue commands, they will affect the files in the current directory (unless the command specifically states otherwise).
If the command line shows you are at a drive other than C:\>Temp, change to the C drive by typing in C: (and then the Return or Enter key) then enter cd \Temp (and then Return or Enter).
Now that we are in the same directory as the Java program, we can easily compile it: type javac Hello.java (followed by Return or Enter). javac is the command that invokes the Java compiler; Hello.java is the name of the file to compile.
The name of the public class inside the Java file must be the same as the name of the file itselfand that name is case sensitive; for instance, typing in javac *.java will cause an error message. (The message will be something like hello is an invalid option or argument or hello.java:5: Public class Hello must be defined in a file called "Hello.java".).
If you do get an error, go back into TextPad (the program is still active, in the open window) and look it over. Fix any differences from the example given here, save the file, go back to the Command Prompt window and compile the program again. Its critical that you save the file: javac works off of the disk copy of the file, not the copy in memory. Making the change to the copy in TextPad is not good enough; those changes also must be saved onto disk.
javac error messages that refer to errors in a program start with the name of the file and the line number on which the error was found. In addition to the message, it often prints the offending line and has an arrow pointing to the place where Java caught the error. A hint: In TextPad, you can get to a line of your Java program quickly by using the Go To command (in the Search menu).
When Hello.java compiles without error, you will get no messages from Java, just another command line prompt. But you will get a so-called class file called Hello.class in your folder. This is the program that Java actually executes.
Run the (successfully) compiled program by entering Java Hello. Note you enter java to run the program, javac to compile it. If you try to run a program before it is compiled, or enter Hello misspelled or in the wrong case, you get an error message that reads something like (Exception in thread "main" java.lang.NoClassDefFoundError: Hello).
A caution: When one compiles a Java program that relies on code contained in another Java file, javac sometimes does not recompile that second file even if it has been changed since the last time it was compiled. This behavior can cause your program to use an outdated class file (which, obviously, can cause problems).
To avoid the problem, use javac *.java at all times, especially during a lab exam. This approach will force Java to recompile every Java file in the folder, so you will not run the risk of using an outdated class file.
Another hint: You may have to recompile and re-run a program several times before it is correct, which means you need to execute the same javac and java commands over and over. However, when in the command prompt mode, you can hit the up arrow and down arrow keys to scroll though already-issued commands. When you find the one you want, you just hit return or Enter to execute that command. So after you have entered the javac and java commands for your (current) program, just use the arrows to go back to them. This is much faster (and less boring!) than retyping them.
Only the compiling and running of the Java programs is done from the command line prompt; all other work (such as editing the program) is typically done using the usual window-based interface.
Fixing a small, bug-ridden program: For our lab assignments and exams, we provide you with a starting (set of) Java files you access via links from the Lab Manual web pages. To obtain practice in modifying programs we provide, and to become more familiar with how Java programs and its error messages look, we have provided an error-ridden program called BuggyNetBill for you to fix and run.
Download BuggyNetBill.java. Store it in its own folder in the Temp directory.
(The easiest way to do this is to first create an empty folder with an appropriate name (well call it JavaPractice) in the Temp directory. Then download the file by right-clicking on its link and choosing Save Target As... or Save Target To... or Download Link To Diskthe exact phrase will depend upon the browser you are using. A file save dialog box will appear; save the file in JavaPractice.)
Always place the file or files that make up a lab into the same folder. That way, Java can find all the pieces that make up the program.
In the command prompt window, go to C: (if needed), then cd \Temp (if needed), then to the BuggyNetBill folder (by entering cd xxx at the prompt, where xxx is the name of the folder containing BuggyNetBill.java).
Compile BuggyNetBill.java; youll note lots of errors.
Open BuggyNetBill.java in TextPad. (An easy way to do this is to right click on BuggyNetBill.java and then select TextPad from the menu that appears. Of course, you can also open it with TextPads Open command.)
Download NetBill.java; put it in its own folder in the \Temp directory. Open that file in Textpad.
TextPad can have more than one file open at once; click on a files name in the upper left corner window to view and edit that file. Or, you can see both files at once by tiling the windows; experiment with the choices under the Window menu to find the tiling option you prefer.
Fix BuggyNetBill.java by looking the error messages and NetBill.java. (You could, of course, just cut and paste NetBill code into BuggyNetBill, but that would defeat the purpose of learning the kinds of messages various errors cause.)
As you fix errors, save BuggyNetBill.java and recompile it; continue in this way until there are no compile errors.
Run the code and compare the output to running NetBill. The idea is to check that your fixed program actually does what is supposed to be done. Just because a program compiles does not mean it is correct! In fact, weve put some errors in BuggyNetBill that the compiler will probably not catchbut if they are left there, you will not get a running program, or one printing the correct results.
Writing a more complicated program:
Enter, compile and run the following program. It takes an object-oriented approach and comes near to the complexity of the first lab assignment. Again, work to understand what this program is doing; it will help you understand Java and programming more generally.
Remember to make a folder in C:\Temp to hold the program and to change drives and directories in the command window as needed to be in that folder. If you are not in that directory, then javac and java will not find your program.
// Written by: Norman Jacobson on April 5, 1996
// Rewritten: by Norman Jacobson, Sept. 2001
// Revised: by Norman Jacobson, Sept. 2004 to reflect Java 5.0
// Purpose: To print a calendar for one month
import java.io.*; // make input/output functions available
import java.util.*; // make scanners available
public class RevdCalendar
{
private String month;
private String year;
private int firstDay;
private int numOfDays;
private Scanner console;
public static void main(String[] args)
{
RevdCalendar myCalendar = new RevdCalendar(); // make a calendar
myCalendar.printCalendar(); // and print it!
}
// The constructor allocates memory and gathers the data necessary
// to make a calendar.
public RevdCalendar()
{
// make a string-handling input-from-keyboard object
console = new Scanner(System.in);
// get the month, year, day of week month starts,
// number of days in month
System.out.print("Enter the month (e.g., September) ");
month = console.next();
System.out.print("Enter the year (e.g., 2001) ");
year = console.next();
System.out.println("On which day of the week does " +
month + ", " + year + " start?");
System.out.print("Enter 1 for Sunday, 2 for Monday, etc.: ");
firstDay = console.nextInt();
System.out.print("And how many days are in this month? ");
numOfDays = console.nextInt();
}
// printCalendar() prints out the calendar page.
public void printCalendar()
{
// print heading
System.out.println();
System.out.println("Calendar for " + month + ", " + year);
System.out.println(" S M T W T F S");
System.out.println();
// position first day under correct day of week
for (int pos = 1; pos < firstday; pos++)
{
System.out.print(" "); // print 3 spaces as leaders
}
// write out the days of the month
int day = firstDay;
for (int date = 1; date <= numofdays; date++) //for each date...
{
if (date <= 9)
System.out.print(" " + date); // write out dates
else // right justified
System.out.print(" " + date);
if (day == 7) // if end of week,
{
System.out.println(); // start a new line
day = 1; // and new week
}
else // otherwise
{
day++; // move to next day (same line)
}
}
System.out.println(); //finish off last line
}
}
Save your program.
You will probably have to edit, compile and run your program several times before it is entirely correct. There should be no error messages of any sort.
The output your program produces should look like the calendar shown below if you supplied input data for January 1994, which started on a Saturday and had 31 days:
Calendar for January, 1994
S M T W T F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
When you (think you) have this program running correctly, try various months, years and starting dates to be sure you know it is working properly.
Now experimentnot with correct input, but with incorrect or unexpected-but-legal input. Especially note what happens with the input and output when you enter text where integers are expected.
Back up your work.
Important note: When you compile or execute your program, several additional files are created in your folder. Do not try to back up all the files (together, they are sometimes too large to fit). All you need are the .java files and any data files that we provided or you created. Leave the other files behind: You can quickly recreate them simply by compiling and executing your program.