CS 151 - Introduction to Data Structures
Lab 2
Unix
NOTE: This lab assumes that you at least made CS151 directories in lab last week (and Homework 1). If you have not done so, do that now.
NOTE: Feel free to work in pairs.
Using Unix (actually Ubuntu Linux)
Some important UNIX commands:
- If you are working from a personal laptop:
- Open a connection onto goldengate with the following command (from a Mac Terminal or Windows Powershell). Note you can connect to goldengate from anywhere in the world using the exact command:
ssh YOURUNIXNAME@goldengate.cs.brynmawr.edu
-
If you are working at a lab computer:
- Open a terminal. To do so: click on the 3x3 grid of dots in the lower left, then scroll through screens until you find Terminal.
As the commands are explained below, try them out ... (below I use capitalization to try to show where the command name came from. The unix command themselves are always all in lower case.)
- Special names and conventions.
Unix has several names that are used with directories. Some are merely by convention, some are built in to UNIX. Here is a partial list with definitions
- root
- the start of the directory structure. The "root" directory is thus the place where everything begins. (In Unix parlance, root can also be used to refer to a person, usually someone who has the power to write in the root directory (ie the power to change anything). In pop culture, this is why the Amy Acker character in the show "person of interest" took the the name "root".)
- home
- the home directory. Each user has a home directory. It is where you are immediately after logging in.
- path
- the way to navigate through directories from the root directory to a given directory (or file)
- ~
- a shortcut. When seen in a path, this denotes the users home directory
- .
- literally a single period. Another shortcut. This indicates the current directory
- ..
- two periods. Another shortcut. This indicates the directory one up from the current directory.
- *
- asterisk. Can be used to substitute for part -- or all - of a file name.
- cd
Change Directory
Example: cd CS151 (change to the directory CS151) (Note that this will not work if you
do not have a CS151 directory (or you are not in your home directory).)
Example: cd .. change to the directory one above where you are. For instance if you are in
/home/xena/CS151, then cd .. would put you in /home/xena
Example: cd . change from the current directory to the current directory. This does nothing as you might expect.
Example: cd with no argument. This returns you to your home directory. (I am not a big fan of this behavior.)
Example: cd ~ This also returns you to your home directory
Example: cd ~/CS151 Go to the CS151 directory that is directly under your home directory
Example: cd / Go to the root directory. (You will almost never have a reason to go to the root directory in this class (other than simple curiosity)).
- ls
LiSt files
Display a list of all the files in the directory. It is very common to use the ls command immediately after
cd.
Example: ls list the files in the current directory
Example: ls CS151 list the files in the directory CS151 (if it exists). Note that CS151 must be contained in the current directory.
Example: ls ../CS151List the contents of the CS151 directory, that is contained in the directory that contains the current directory.
Example: ls *.java Within the current directory show all files that end with .java
Example: ls A*.txt Within the current directory show all files that begin with the letter "A" and end with .txt
- mkdir
MaKe DIRectory
Create a directory
Example: mkdir CS151 make a directory named CS151 You probably already did this in your home directory.
- pwd
Displays the "current working directory". In other words this command shows you where you are. (I have no idea why pwd is so named)
- cp
CoPy a file
Example cp a b make a copy of the file named "a" in the current directory into a file named "b"
in the current directory. Note that if "b" existed before the cp command, then it will be overwritten
Example: cp ~/a a Copy the file named a that is in your home directory to the current directory, keeping the same file name. Note you could also get the same effect from this command cp ~/a .
- mv
MoVe a file
Example mv a b change the name of the file a to b.
in the current directory. When in the same directory mv can be used to rename files
Example: mv ~/a a Move the file named a that is in your home directory to the current directory, keeping the same file name. Note you could also get the same effect from this command mv ~/a .
- ssh
Secure SHell
From the current machine, open a "shell" on a remote machine and interact with that remote machine almost exactly as if you were sitting at it.
Example: ssh YOURUNIXNAME@goldengate.cs.brynmawr.edu Through tis semester, if you like to work on your laptop, I expect that you will use this command frequently. ("goldengate.cs.brynmawr.edu" is the name of a machine accessible via ssh; you cannot directly access the lab machines using ssh.)
- scp
Secure CoPy a file
The important difference between cp and scp is that you can use scp to copy between
machines. Using scp you can copy either from local to remote (upload) or from reote to local (download).
Example scp YOURUNIXLOGIN@MACHINEiD:a b make a copy of the file named "a" that is available on the
machine MACHINEiDin the home directory of user UNIXLOGIN into a file named "b"
in the current directory of the current machine. Note that if "b" existed before the scp command, then it will be overwritten
Example scp a UNIXLOGIN@MACHINEiD:b Make a copy of the file a in the current directory
and put it in the home directory of UNIXLOGIN on MACHINEiD in the name b. It is almost always a good idea in this course for MACHINEiD to be goldengate.cs.brynmawr.edu.
- rm
ReMove a file
Example: rm aa Permanently delete the file "aa"
Example: rm *.class Permanently delete all files that end in .class.
Danger. Using * with remove can result in you deleting a lot of files.be very cautious. I usually test using ls before rm. For instance "ls *.class" then "rm *.class"
- cat
Show the entire contents of a file to the terminal. ("cat" was originally an acronym for CAthode ray
Tube, which might give you a feel for how long this command has existed.)
Example: cat aa the entire contents of aa will show on the screen
- javac
JAVA Compile
Compile a java program.
Example: javac HW.java
This will compile the java code in the file HW.java. If there are no
errors, it will create a file HW.class.
- java
Run a java program.
Example:java HW
Run the java program named HW. This requires that the file HW.class is present in the current directory.
Execute the above unix commands, put answers to the questions to their right
cd |
What directory are you in?
| |
| What is the contents of this directory? (You need not list everything; 10 items is more than enough)
| |
cd / |
What directory are you in? (It has a special name.)
| |
| What is the contents of this directory (again, 10 names is enough)?
| |
cd ~ |
What directory are you in?
| |
| What is the contents of this directory?
| |
cd /home/YOU |
(Replace YOU with your login name) What directory are you in?
| |
| What is the difference between this command and the previous one?
| |
ls /home/gtowell/Public/151 |
What did this command do?
| |
cat /home/gtowell/Public/151/L02/HW.java
cat /home/gtowell/Public/151/L02/HW.class
|
What did the second command do? Is the file HW.class readable? One sentence: explain.
| |
A good understanding of these few UNIX commands is about all you need to this course. (Many long-time Unix users know little
more than these.)
Java Practice with inheritance
Write two classes "Older" and "Younger" such that Younger extends Older. (Feel free to rename.) Give Older two protected instance variables. Create a constructor in Older that sets the value of these two variables. Add a toString() method to Older. Finally, at least for Older, write a very short Main method that demonstrates all of what you just wrote.
Now write Younger. Give it two new protected instance variables. Write a constructor that initializes all 4 variables that are a part of Younger. Write a toString that shows all 4 instance variables in Younger. In your Younger toString method, use super.toString(). Write a main method to illustrate what you have done.
After completing all of the above, in your main within Younger add the line
Older yo = new Younger(1,2,3,4);
System.out.println(yo);
(Change the parameters to the Younger constructor as needed for your implementation.)
What gets printed in the added println? Explain.
What to hand in
Send email to gtowell@brynmawr.edu with the following:
- A completed table for the questions in the UNIX section
- If you worked with someone else, put both names in the subject line.
- Your Older and Younger implementations.
- Answers to the questions about Younger.
The easiest way to submit the Unix table might be to use your phone and take pictures of the completed hand-written, table. Alternately, if you typed your answers, just send them. These are just suggestions for how you
could turn things in. Feel free to be creative. All I require is legibility.