CSCI 241 Labs: PreLab 1
Introduction to Linux and BlueJ
Introduction
The beginning of this document introduces you to procedures and
commands for working in
the UW-Parkside Computer Science Department lab; the end introduces
BlueJ. The Linux command section contains all commands you will need
this semester.
Another (more extensive) handout, "Getting Started in UNIX",
complements this document and is
available in the lab.
These handouts will be useful all semester, so keep them with you when
you are
working on lab machines.
Activating your Computer
Science (CS) Lab account
The Computer Science Department has established policies that all lab
users must follow. They are summarized in the account policy sheet.
Make certain you read, understand and follow these policies.
The very first time (and only
the first time) you use a computer in the lab, you need to activate
your account. Your Computer
Science (CS) Lab account will have a DIFFERENT login ID and password
than the one you use for other UWP access.
Logging In
Your lab login id and password are associated with your lab account,
not with one particular
machine. Use them at any machine in the room.
- The computer system you use
in the Computer Science Laboratory runs the Linux operating system.
Linux is a variant of UNIX, and our lab currently uses CentOS linux.
- If the screen is blank, press any key to bring it to life.
The monitor may be turned off; turn
it on if necessary (our monitors will go to sleep after a short time with no computer use).
PLEASE keep your fingers from touching the screens -
they are not easily cleanable.
You should now see a screen with the current date and time.
Press the Enter key (or roll the mouse wheel).
Now you should see a dialogue box where you can enter your "Username:".
Enter your CS department login name and press Enter. Then, one more
dialogue box
appears asking for your password. Type your password in the box and
then press the Enter key.
- If your login is successful,
there will be a short delay (needed to load your profile) followed by
the display of some startup Gnome windows (Gnome is your Desktop
Manager Program.) Close any windows that show up by clicking on the X
in the upper right corner of the window.
- Look in the upper left corner and click Applications.
You can use this menu item to reach just about anything you need to use.
In the Favorites list which appears, click Terminal.
You will now see a window whose
title includes your login, machine name (login-name@vegetable-name)
and current directory location (~,
which means your home directory, /home/student/login-name). The cursor
should appear to the right of a prompt which looks like: "[login-name@vegetable-name~]$".
Logging Out
When you are finished working on a computer you must log out. For
security reasons, we recommend you do NOT
use the "Switch User" option. Failure to log out properly will
potentially result in other students gaining access to your account.
First, close all browsers and
application programs currently running. Logging out while
certain applications are running can leave them running even after you
log out, tying up resources and leaving a potential security risk.
Click on your name (found in the upper right corner of the screen). Select the
option, "Log out". Another dialogue box will appear, from which you can
confirm that you want to log out. DO NOT
shut the machine down; others may be using it remotely.
Working in X Windows and Linux
The computer science lab machines run the Linux
operating system, a variant of UNIX. X
Windows runs on top of Linux,
giving
us windows and menus in which to work. A desktop manager named Gnome
runs on top of the X Windows server.
Various window managers also work in the background. Your account has
already been set
up to run a window manager.
Linux Commands
This section summarizes a few of the basics of working within Linux.
Linux commands are typed into an xterm
(a terminal window). To open an xterm you can click
Applications-Favorites-Terminal (as described above).
A window will open with a prompt that looks something like
[smith@cabbage~]$
where smith
is your login name and cabbage
is the name of the machine at which you are working.
Linux commands are case
sensitive. Most commands are typed in all
lower case, but make certain you type them exactly as they appear
below.
-
The Directory Structure
Almost all operating systems use the basic notions of directories
(folders) and files stored within the directories. Linux is no
exception. When you login to your account, you are in your home
directory. Your home directory has a parent directory, and as you will
see, you can also create subdirectories within your home directory. All
together the directories form a large (upside down) tree. The root of
the tree is named: /
When typing commands
several directories have special abbreviations.
The current directory is referred to as: .
The parent of the current directory is: ..
Your home directory is: ~
- pwd
prints the name of the working directory.
At the $ prompt, enter: pwd.
Assuming that your account is named 'smith' and that you have just
logged in, the machine will respond with:
/home/student/smith
This may be interpreted as:
/
contains a subdirectory named home,
home
contains a subdirectory named student,
and student
contains a subdirectory named smith,
which is your home directory.
- cd
changes the working directory.
cd
(typed by itself) returns you to your home directory.
cd ..
moves you up one level in the directory tree.
cd directory
path - changes directory to
location specified.
- ls
lists the files and subdirectories within the current directory (that's
a letter L in lowercase followed by s).
ls
lists the filenames.
ls
-l
is a long listing (the letter after the - (dash) is also
a lowercase L). It includes permissions on the files, the owner of each
file, when they were created and their names.
ls -a
lists all files, including those beginning with .
(files usually hidden from view; generally containing setup or
configuration information). The current directory is always named .
(by itself), but other files may begin with .
as well.
- mkdir
makes directories.
mkdir Lab01
creates a subdirectory named Lab01
- rmdir
removes empty directories.
rmdir Lab01
removes the directory named Lab01
-
File Manipulation
Files exist within directories. They are typically created using an
editor.
There are many editors available in the CS lab, including gedit,
vi
and emacs.
gedit
is the most intuitive, with many of the same commands you use in
Microsoft's WordPad.
There are specialized handouts available in MOLN D120 to learn about vi
and emacs.
Once files exist, they can be copied, moved, deleted and viewed
via
various
Linux commands.
Linux uses the wild card
character, *,
to match any string of characters in a file name.
Warning: Using an asterisk (*) can affect more files than you expect if
unintended files match your wild card specification.
- cp
is used to copy files.
cp Ball.java Ball.bak
copies the file Ball.java
into a new file named Ball.bak
At the beginning of each week's lab exercise, you will copy a directory
of files. For example, here is the command for the first week:
cp -r
/home/student/Classes/Cs241/Labs/Lab01 .
This command has several
parts to it, including a "from" and a "to" location.
Specifically, it copies the directory named Lab01
(and all folders and files it holds) from
its location inside of /home/student/Classes/Cs241/Labs.
The -r
option (or "flag") says to do the copy recursively (meaning take the
contents of the directory as well). It copies all of this group to
the current directory (the location from which you typed the command,
represented by the dot (.)).
As a rule of thumb, when copying a directory, use the -r
flag. When copying individual files, do not.
- more
lists files to the window, one windowful at a time.
more Ball.java
lists the file Ball.java
to the window. If the file is more than one window long, more
waits for the user to press the spacebar for the next windowful.
- rm
removes files.
rm Ball.java
removes the file Ball.java
Warning!
Use rm
with caution.
Once a file is removed, it is gone forever. For example,
rm -rf *
will remove all
the files in the current directory.
- chmod
changes the access permissions on files or directories. Security on
UNIX systems is based on granting permissions to the file owner (u),
the file's group (g) and all others (o). You are the owner of files you
create. All students belong to a group named student.
The permissions are read
(r), write (w) and execute (x). Anyone with read
permission may see the contents of the file. Anyone with write
permission may change the file. For programs, anyone with execute
permission may run the program. The permissions are a bit different for
directories. In particular, execute
permission on a directory enables one to access the contents of the
directory.
chmod g+x a.out
adds execute
permission to all members of the group to enable them to run the
program a.out.
chmod go-r Ball.java
removes read
permission from the file Ball.java,
so that group members and others can no longer read it.
Now ls
-l discussed above makes more
sense.
ls -l
/home/student/Classes/Cs241/Labs/Lab01
returns something like:
-rw-r--r-- 1 hansen staff 123 Sep 7 2001 LabOne.ctxt
-rw-r--r-- 1 hansen staff 788 Sep 7 2001 LabOne.java
-rw-r--r-- 1 eddy staff 471 Sep 7 2001 README.TXT
-rw-r--r-- 1 hansen staff 285 Sep 7 2001 bluej.pkg
-rw-r--r-- 1 hansen staff 285 Sep 7 2001 bluej.pkh
Most of the files belong to Professor Hansen, except for README.TXT
which belongs to Ms. Eddy. The group of each file is staff.
The first 10 characters of each line show the permissions on the file.
The first character is a -
for ordinary files and a d
for directories. The next three characters show the owner's
permissions, the next three the group's permissions and the last three
all other's permissions. In our example, the owner has read
and write
permissions. The group and others have only read
permissions. There are no executable files in this directory.
Linux Printing Commands
- lp
prints a copy of a file to the printer.
lp LabOne.java
prints the file LabOne.java
to the printer. It prints one logical page per physical sheet of paper.
-
To submit your assignments, you will NOT need to give a printout to your instructor.
You will upload a copy of your .java file(s) to Canvas.
However, if you wish to print for your own review, we recommend printing files through BlueJ.
BlueJ also allows you to print 2 logical pages on one physical sheet of paper
(used to save paper while work is in progress).
Most applications (like BlueJ, mentioned earlier) have a print command in their menu system.
This is generally the easiest way to print.
Warning!
Do not print binary files. In particular, do
not print files with a .class
extension.
The results are unpredictable, including possibly printing hundreds of
pages and/or hanging the printer.
- lpq
displays the jobs currently waiting in the print queue. You can see if
your file is still waiting to be printed.
- Most applications (like BlueJ, mentioned earlier) have a
print command in their menu system. This is generally the easiest way
to print.
Submitting a Programming
Assignment Electronically (two required ways)
Each programming assignment (not labs) must be submitted in two ways:
-
Submit your projects through a BlueJ menu option.
You MUST be running BlueJ in our lab to do the submission;
it does NOT work with your home computer BlueJ installation.
This process copies your BlueJ project, and all its files, into a
directory that your instructor uses for grading.
Each submission is time-stamped so that we will know it has arrived on
time. From here, your instructor can compile your program(s) as well as run it/them with test data.
-
Upload each .java (source file) you create/modify to Canvas.
Your instructor can write comments on this copy and assign points.
If, after submitting a project,
you discover that you want to change what you
have submitted, you may simply submit through the menu option again.
The earlier version(s) of your program(s) will be replaced by the
new version(s).
Using a USB Storage Device
Many students use USB storage devices to transfer
files between computers. These are also called flash memory or pen or
jump drives. In our lab, these drives must be mounted and unmounted on
the machines in order to access them underneath the root directory.
To begin:
- Insert the USB device on the front of the computer underneath the table.
- After a brief delay, a
window will appear listing the contents of this device. An icon for the
device will also appear on your desktop.
- Stored files will be found
inside a directory at:
/media
The name of the directory where the files are located depends on the
brand name of your flash drive. For example, if the brand is Kingston,
you might find the files at this location:
/media/KINGSTON/*
These files can be accessed with normal Linux commands like cd,
ls, cp, etc.
When finished, you will need to remove the device using these steps:
- Close any windows displaying
the contents of the device.
- Right-click on the device's
icon and choose Eject
Volume.
- Unplug the device from the
cable before
logging out and turning off the monitor.
Other Useful Linux Commands
Although a Linux command exists to change your password, it is easier do so by
opening a browser and
navigating to:
https://www.cs.uwp.edu/account/activate
Follow the directions on the screen.
- who
tells you who is currently logged onto the system.
- ps
-u tells you the status of all
your active processes
(i.e., the programs that are running on the machine in front of you).
Here is sample output from ps
-u:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
hansen 26902 0.0 0.1 1740 988 pts/8 S 13:00 0:00 -bash
hansen 26963 0.1 1.0 8192 5296 pts/8 S 13:01 0:05 emacs -font 10x20
hansen 28737 0.0 0.1 2324 688 pts/8 R 13:49 0:00 ps -u
Professor Hansen has three processes running: a command shell (bash),
an editor (emacs)
and the ps
command. The first two are stopped.
The status of the last
one is running. The PID
(Process ID) column tells you the unique
number assigned to each process.
- kill
-9 PID
kills processes.
kill -9 26963
will kill the emacs session that Professor Hansen is running.
Warning!
Use kill
judiciously. There is almost always a more natural way
to terminate a program. Only use kill
when a program is so hung there is no other alternative.
Sending Labs to Your Partner's
Account
During lab each week, you will be working with a partner, and programs
you
create during lab time will be saved in only one of the two accounts.
At the end of lab, the other person may also want to have a copy of
these
program files. Because account access is protected, you cannot directly
copy files from one student's account to another's.
For the following instructions,
the student currently logged in will be
referred to as "Student1", and the student who will receive a copy of
the
files will be referred to as "Student2". We will assume you want to
copy all the files in the Lab01
directory.
So that only one file need be transferred, the first part of these
instructions
explain how to create a zip file:
Student1:
- Make certain you are in the directory that contains the Lab01
directory.
You may need to cd to the proper level (to go up one level,
type cd ..).
- Enter one of these commands:
zip -r Lab01.zip Lab01
OR
zip -r Lab01.zipp Lab01
This creates a zip (compressed) file containing all of the Lab01 directory
contents, including any subdirectories that might exist.
For security reasons, our campus email does not allow .zip files to be attached to email messages.
If you wish to use RangerMail, you should name your file with the slightly different file extension (zipp).
You'll rename it later.
Here are two sets of instructions (you can use either one) to transfer
this
zipped file to Student2. The first way is by attaching the file to an
email message, and the second way is by using secure file transfer
protocol (sftp).
-
The following instructions use the campus RangerMail program as the
email client (program).
Very similar directions apply if you use a different email server such
as
yahoo or hotmail.
This method of file transfer is handy if Student2 wishes to get a copy
of the lab files on their home computer. It is also a good way to send
files
to yourself to retrieve at home.
- Using Firefox, go to the
UWP Logins page and log into RangerMail.
- Choose to send a new message.
- Enter your partner's
email address in the To
box.
- Type "Lab01 Files" in
the Subject
box.
- Look for the "Attach
files" button (with the paper clip) above the "From:" field. Choose
Browse and navigate to your Lab01.zipp
file, and click Open.
- Important!
You must click the Attach
button.
- Click the Send
button.
- Log out of RangerMail.
To receive the lab files, Student2 should follow the procedure below:
- Log in to any lab machine.
- Using Firefox, go to the
UWP Logins page and log into RangerMail.
- Select the message
containing the attachment.
- On the Parts
line you should see Lab01.zipp.
Click on it.
- A window will appear
asking if you want to save the file. Make certain "Save to Disk" is
selected, then either save to your desktop (the default) or click on
the Desktop
button to navigate to the location where you wish to save the file.
- Rename the ".zipp" file as ".zip".
- Log out of RangerMail
and close Firefox.
- The two students can also
use secure file transfer protocol to transfer the zipped file directly
into another lab account.
- Assume that the two lab
partners are working on the machine named radish.
Student1 types the following command on his/her machine:
sftp
student2@radish.cs.uwp.edu
note: this assumes student2
is the person's login name
- A prompt will appear
asking for Student2's password. Student2 should type this in, then
press Enter.
- The terminal window
doesn't look quite the same as a normal terminal window, but Student2
can still use Linux commands such as cd,
ls, etc. to go to the location
where she wants the copied file to be placed.
- Student2 then types
put Lab01.zip
- The zipped file will
then be placed in the proper location. Student2 is still logged in to
her account at this time.
- Student2 should type exit
to stop running sftp
and return the machine to Student1's account.
Student2 should log in as usual to her account, then:
- In a terminal window, cd
to the directory where you saved the Lab01.zip
file.
- Type the command unzip
Lab01.zip to unzip the file.
Beyond the Basics
The goal of CSCI-241 is to teach you the fundamentals of
object-oriented
computer programming. These fundamentals apply regardless of the type
of
machine, operating system or object-oriented language you choose. To
accomplish this we will use the Java programming language and an
integrated
development environment (IDE) named BlueJ. The remainder of this
document
introduces you to each of these.
BlueJ
BlueJ, like other IDEs, provides the user with many tools for
developing Java programs including
an editor, access to the compiler and a debugger.
There are many other IDEs available for Java including Eclipse
(originally created by IBM, but now a non-profit open source
foundation), Forte, NetBeans (both by Sun), JBuilder (by Borland) and
CodeWarrior (included with some textbooks).
BlueJ was designed particularly for students just beginning to learn
object-oriented programming using Java.
You'll use BlueJ in the very
first lab.
- Starting
BlueJ To start BlueJ, enter the
command (after the $ prompt)
bluej&
The ampersand (&)
allows you to toggle back-and-forth between BlueJ and the terminal
window.
You can also reach BlueJ through the Applications->Programming menu.
A few moments after entering this command, the main BlueJ
window will open.
- Exiting
BlueJ To exit BlueJ select Project
-> Quit from BlueJ's
menu.
- BlueJ
Projects BlueJ, like most IDEs,
collects Java files together into projects. A project
is a directory that contains one or more Java programs and other
related files. For example, when you copy files for use in a lab
exercise, you typically copy one or more BlueJ projects.
- Creating
and Opening Projects
When you start working you will either want to create a new BlueJ
project or open a pre-existing project. Both options appear in BlueJ's
Project menu.
When you select either
option the system gives you a list of what is available in your current
subdirectory. Each icon on the 'Look In:'
line represents a way to either move to a different directory level, or
change the way the directory listings are displayed in this window.
Move your mouse pointer onto each icon, one at a time, to see (after a
brief delay) a short description of the action that icon performs.
Instead of using the mouse, you can also type the file name you want in
the area labeled 'File Name:'
located below the directory listing.
To create a new project select Project
-> New Project and enter
the name of the project in the 'File Name:'
area.
To open an existing project select Project
-> Open Project. In the
Open Project window, choose the folder
with the project name, then click Open. Your BlueJ window now displays a paper icon and
a class icon for
each class
in the project. If you opened a new project, there are no classes
currently displayed.
- Adding
Classes to Projects
To add a class to a project, click on the New
Class button. A dialog box
appears asking for a class name. Enter the name of your choice.
- Editing
Java Files
BlueJ uses a text editor named moe
to edit Java source code. To open the editor, use the right
mouse button, click on the class icon for LabOne
and select "Open Editor" from the menu displayed. (Alternatively, you
can double-click the class icon to edit the implementation.) The Java
code for this class is now displayed.
The editor is fairly
intuitive. You can move around the file using the arrow keys. Text is
inserted when you type. The editor also uses syntax highlighting. Right
now we don't know much Java, but as the semester proceeds, you will see
that different language structures appear in different colors in the
editor.
To close the editor, first
choose Class -> Save,
then choose Class ->
Close.
- Compiling
Java Files
Java source code needs to be translated into byte
code before it can be run.
The Java files that we edit are called source
code. Source code is easy for
humans to read and interpret. Byte code, a.k.a. machine language, is
binary. It is very
difficult for humans to read but easy for the computer to interpret. In
Java, source
code files always have a .java
suffix. Byte code files always have a .class
suffix.
Warning!
Never
print a byte code (or any other binary) file.
To successfully compile, a
Java file must be syntactically correct. This means that it must
contain only legal Java statements. If there are syntactic errors the
compiler cannot generate
byte code, but can generate error messages. One nice feature of BlueJ's
IDE is that when
the compiler finds an error, it highlights the line where the error was
detected and displays
an error message, hopefully making the error easier to correct. The
error may occur on the
highlighted line or on one earlier in the program.
Warning!
Logical errors are mistakes in the program's logic, not its syntax. The
Java
compiler has no way of detecting these.
To compile a class while
you are editing it, click the Compile
button that appears
at the upper left of the window. If the compile is successful, you will
receive a message
saying, "Class compiled - no syntax errors."
To compile a class when not
editing, right click the class icon and choose compile.
If the compile is successful, you will see a message saying, "Compiling
... Done".
- Running
Java Code from Within BlueJ
Java is an object-oriented language. The basic idea behind
object-oriented
programming is to define classes and then make instances of those
classes
named objects. BlueJ gives us three ways to create and use objects.
- The
BlueJ Object Bench
It is possible in BlueJ to create (instantiate) an object and
manipulate it directly.
Right click on the class you wish to instantiate and choose new
classname.
A window appears with a name for the object. You may simply click okay,
or rename
the object as you wish. A red object icon appears in the object bench
at the bottom
of the BlueJ window.
An object contains methods
that may be invoked
to make the object
do things. Right click on the object icon and choose the method. If the
method
requires arguments,
i.e., data, another window will appear asking you to enter
it.
- Java
Programs
Most Java programs are made up of many objects working together to
accomplish some task.
Java programs are invoked (start running) from the class icon, not
object icon. Right click on the
class icon and choose Run
Main Method. The program begins
running.
Note: Alternatively,
you can run the main
method by choosing void
main(String []).
You will then see a second window with {
}
symbols in the textbox. Simply choose OK
to run the program.
- Java
Applets
There is a third way to manipulate objects in BlueJ. Applets are Java
programs that
are meant to run in a web browser. Applets require both extensive
knowledge
of Java and some knowledge of html. While we may explore applets at
some point
during the semester, they are not covered further here.