SlideShare a Scribd company logo
1 of 16
1
CMPS 12M
Data Structures Lab
Lab Assignment 1
The purpose of this assignment is threefold: (1) get a basic
introduction to the Andrew File System (AFS)
which is the file system used by the ITS UNIX timeshare, (2)
learn how to create an executable jar file
containing a Java program, and (3) learn to automate
compilation and other tasks using Makefiles.
Part 1: HELLO WORLD
Start by creating a directory for this course. Fire up a terminal.
(Let % denote the prompt.) Run:
% cd ~/Desktop
% mkdir CMPS12B
% cd CMPS12B
% pwd
(You should see which directory you’re in. You’re free to create
this directory anywhere, but follow the
above if you’re not sure.)
In this directory, create a file HelloWorld.java with the
following text.
//-----------------------------------------------------
// HelloWorld.java
// Prints "Hello world", the first program you should always
write
//-----------------------------------------------------
class HelloWorld {
static public void main(String[] args) {
System.out.println("Hello world");
}
}
Go to a terminal, and get into the directory with this file. Run
% javac HelloWorld.java
% java HelloWorld
You should see “Hello World” printed out. If not, you’ll need to
install Java on your machine. Or, you can
optionally work on the UNIX timeshare. Talk to the lab
instructor for more directions.
Part 2: AFS
Learning a bit about AFS is useful in terms of knowing some
typical OS/file-system operations. The UNIX
time share systems are also a good option if you are having
trouble with compiling and running Java or C
programs on your laptop. The UNIX servers have all of the tools
you need to complete all assignments for
this class, although working on your own laptop can be more
convenient and will also allow you to use a
modern, graphical IDE.
Logon to your ITS UNIX timeshare account at UNIX.ucsc.edu.
If you don't know how to do this read the
instructions at the URL below. You can also ask for help at a
lab session but you should really figure this
out on your own if possible.
https://its.ucsc.edu/unix-timeshare/tutorials/how-to-
connect.html
2
Create a directory cs12b with a subdirectory lab1. From within
lab1 create a subdirectory called private,
then set access permissions on the new directory so that other
users cannot view its contents. Do all this by
typing the lines below. The UNIX prompt is depicted here as
%, although it may look different in your login
session. Those lines without the UNIX prompt are the output of
your typed commands.
% mkdir cs12b
% cd cs12b
% mkdir lab1
% cd lab1
% mkdir private
% fs setacl private system:authuser none
% fs setacl private system:anyuser none
% fs listacl private
Access list for private is
Normal rights:
foobar rlidwka
Here foobar will be replaced by your own cruzid. The last line
of output says that your access rights to
directory private are rlidwka which means: read, list, insert,
delete, write, lock, and administer. In other
words you have all rights in this directory, while other users
have none. If you are unfamiliar with any
UNIX command, you can view its manual page by typing: man
<command name>. (Do not type the angle
brackets <>.) For instance man mkdir brings up the man pages
for mkdir. Man pages can be very cryptic,
especially for beginners, but it is best to get used to reading
them as soon as possible.
Under AFS, fs denotes a file system command, setacl sets the
access control list (ACL) for a specific
user or group of users, and listacl displays the access lists for a
given directory. The command
% fs setacl <some directory> <some user> <some subset of
rlidwka or all or none>
sets the access rights for a directory and a user. Note that setacl
can be abbreviated as sa and listacl
can be abbreviated as la. For instance do la on your home
directory:
% fs la ~
Access list for /afs/cats.ucsc.edu/users/a/foobar is
Normal rights:
system:authuser l
foobar rlidwka
The path /afs/cats.ucsc.edu/users/a/foobar will be replaced by
the full path to your home directory,
and your own username in place of foobar. Note that ~ (tilde)
always refers to your home directory, .
(dot) always refers to your current working directory (the
directory where you are currently located) and ..
(dot, dot) refers to the parent of your current working directory.
The group system:authuser refers to
anyone with an account on the ITS UNIX timeshare. Thus by
default, any user on the system can list the
contents of your home directory. No other permissions are set
for system:authuser however, so again
by default, no one else can read, insert, delete, write, lock, or
administer your files.
Do fs la ~/cs12b and verify that the access rights are the same
for the child directory cs12b. Create a
subdirectory of private, call it anything you like, and check its
access rights are the same as for its parent.
Thus we see that child directories inherit their permissions from
the parent directory when they are created.
To get a more comprehensive list of AFS commands do fs help.
For instance you will see that fs lq
shows your quota and usage statistics. For more on the Andrew
File System go to
3
http://claymore.rfmh.org/public/computer_resources/AFSinfo.ht
ml.
While your assignments are solo assignments to be done alone.
For this lab you can find another student
and as an exercise allow your partner read access to the
directory where you are working on a shared
assignment, e.g. prog1, do the following:
% cd cs12b
% mkdir prog1
% fs setacl prog1 system:anyuser none
% fs setacl prog1 system:authuser none
% fs setacl prog1 partnerID read
% fs listacl prog1
Access list for prog1 is
Normal rights:
system:operator rlidwk
system:administrators rlidwka
partnerID rl
mcdowell rlidwka
Of course, using other options with "fs setacl" you could also
give your partner write access to the directory,
although you should do that with great caution. Try and create
some files and see if your partner can access
them.
Part 3: Jar Files
Copy the following file to your lab1 directory. Part of this
exercise is to be able to use the text editor.
You want to complete Lab1 knowing that you are 100%
comfortable that you can edit and compile simple
files.
//-------------------------------------------------------------------------
----
// HelloUser.java
// This is a really simple program to get you started.
// Prints greeting to stdout, then prints out some environment
information.
//-------------------------------------------------------------------------
----
class HelloUser{
public static void main( String[] args ){
String userName = System.getProperty("user.name"); /* this
should be you! */
String os = System.getProperty("os.name");
String osVer = System.getProperty("os.version");
String jre = System.getProperty("java.runtime.name");
String jreVer =
System.getProperty("java.runtime.version");
String jvm = System.getProperty("java.vm.name");
String jvmVer = System.getProperty("java.vm.version");
String javaHome = System.getProperty("java.home");
long freemem = Runtime.getRuntime().freeMemory();/*
scary if 0! */
long time = System.currentTimeMillis();
System.out.println("Hello "+userName);
System.out.println("Operating system: "+os+" "+osVer);
System.out.println("Runtime environment: "+jre+"
"+jreVer);
System.out.println("Virtual machine: "+jvm+" "+jvmVer);
System.out.println("Java home directory: "+javaHome);
System.out.println("Free memory: "+freemem+" bytes");
System.out.printf("Time: %tc.%n", time);
}
}
4
You can compile this in the normal way by doing javac
HelloUser.java then run it by doing the
command java HelloUser. Java provides a utility called jar for
creating compressed archives of
executable .class files. This utility can also be used to create
an executable jar file that can then be run
by just typing its name at the UNIX prompt (with no need to
type java first). To do this you must first
create a manifest file that specifies the entry point for program
execution, i.e. which .class file contains
the main() method to be executed. Create a text file called
Manifest containing just one line:
Main-class: HelloUser
If you don’t feel like opening up an editor to do this you can
just type
% echo Main-class: HelloUser > Manifest
The UNIX command echo prints text to stdout, and > redirects
the output to a file. Now do
% jar cvfm HelloUser Manifest HelloUser.class
The first group of characters after jar are options. (c: create a
jar file, v: verbose output, f: second argument
gives the name of the jar file to be created, m: third argument is
a manifest file.) Consult the man pages to
see other options to jar. The second argument HelloUser is the
name of the executable jar file to be
created. The name of this file can be anything you like, i.e. it
does not have to be the same as the name of
the .class file containing function main(). For that matter, the
manifest file need not be called Manifest,
but this is the convention. Following the manifest file is the list
of .class files to be archived. In our
example this list consists of just one file: HelloUser.class. At
this point we would like to run the
executable jar file HelloUser by just typing its name, but there
is one problem. This file is not recognized
by UNIX as being executable. To remedy this do
%chmod +x HelloUser
As usual, consult the man pages to understand what chmod
does. Now type HelloUser to run the program.
The whole process can be accomplished by typing five lines:
% javac –Xlint HelloUser.java
% echo Main-class: HelloUser > Manifest
% jar cvfm HelloUser Manifest HelloUser.class
% rm Manifest
% chmod +x HelloUser
Notice we have removed the now unneeded manifest file. Note
also that the –Xlint option to javac enables
recommended warnings. The only problem with the above
procedure is that it’s a big hassle to type all
those lines. Fortunately there is a UNIX utility that can
automate this and other processes.
Part 4: Makefiles
Large programs are often distributed throughout many files that
depend on each other in complex ways.
Whenever one file changes all the files depending on it must be
recompiled. When working on such a
program it can be difficult and tedious to keep track of all the
dependencies. The UNIX make utility
automates this process. The command make looks at
dependency lines in a file named Makefile. The
dependency lines indicate relationships between source files,
indicating a target file that depends on one or
more prerequisite files. If a prerequisite has been modified
more recently than its target, make updates the
target file based on construction commands that follow the
dependency line. make will normally stop if it
encounters an error during the construction process. Each
dependency line has the following format.
5
target: prerequisite-list
construction-commands
The dependency line is composed of the target and the
prerequisite-list separated by a colon. The
construction-commands may consist of more than one line, but
each line must start with a tab character.
Start an editor and copy the following lines into a file called
Makefile.
# A simple Makefile
HelloUser: HelloUser.class
echo Main-class: HelloUser > Manifest
jar cvfm HelloUser Manifest HelloUser.class
rm Manifest
chmod +x HelloUser
HelloUser.class: HelloUser.java
javac -Xlint HelloUser.java
clean:
rm -f HelloUser HelloUser.class
Anything following # on a line is a comment and is ignored by
make. The second line says that the target
HelloUser depends on HelloUser.class. If HelloUser.class
exists and is up to date, then HelloUser
can be created by doing the construction commands that follow.
Remember that all indentation is
accomplished via the tab character. The next target is
HelloUser.class which depends on
HelloUser.java. The next target clean is what is sometimes
called a phony target since it doesn’t depend
on anything and just runs a command. Likewise the target
submit does not compile anything, but does
have some dependencies. Any target can be built (or perhaps
executed if it is a phony target) by doing
make <target name>. Just typing make creates the first target
listed in the Makefile. Try this by doing
make clean to get rid of all your previously compiled stuff, then
do make again to see it all created again.
Your output from make should look something like:
% make
javac -Xlint HelloUser.java
echo Main-class: HelloUser > Manifest
jar cvfm HelloUser Manifest HelloUser.class
added manifest
adding: HelloUser.class(in = 1577) (out= 843)(deflated 46%)
rm Manifest
chmod +x HelloUser
The make utility allows you to create and use macros within a
Makefile. The format of a macro definition
is ID = list where ID is the name of the macro (by convention
all caps) and list is a list of filenames.
Then $(list) refers to the list of files. Move your existing
Makefile to a temporary file, then start your
editor and copy the following lines to a new file called
Makefile.
#-------------------------------------------------------------------------
-----
# A Makefile with macros
#-------------------------------------------------------------------------
-----
JAVASRC = HelloUser.java
SOURCES = README Makefile $(JAVASRC)
MAINCLASS = HelloUser
CLASSES = HelloUser.class
JARFILE = HelloUser
6
all: $(JARFILE)
$(JARFILE): $(CLASSES)
echo Main-class: $(MAINCLASS) > Manifest
jar cvfm $(JARFILE) Manifest $(CLASSES)
rm Manifest
chmod +x $(JARFILE)
$(CLASSES): $(JAVASRC)
javac -Xlint $(JAVASRC)
clean:
rm $(CLASSES) $(JARFILE)
Run this new Makefile and observe that it is equivalent to the
previous one. The macros define text
substitutions that happen before make interprets the file. Study
this new Makefile until you understand
exactly what substitutions are taking place. Now create your
own Hello program and call it
HelloUser2.java. It can say anything you like, but just have it
say something different from the original.
Add HelloUser2.java to the JAVASRC list, add
HelloUser2.class to the CLASSES list and change
MAINCLASS to HelloUser2. Also change the name of
JARFILE to just Hello (emphasizing that the jar file
can have any name.)
#-------------------------------------------------------------------------
-----
# Another Makefile with macros
#-------------------------------------------------------------------------
-----
JAVASRC = HelloUser.java HelloUser2.java
SOURCES = README Makefile $(JAVASRC)
MAINCLASS = HelloUser2
CLASSES = HelloUser.class HelloUser2.class
JARFILE = Hello
all: $(JARFILE)
$(JARFILE): $(CLASSES)
echo Main-class: $(MAINCLASS) > Manifest
jar cvfm $(JARFILE) Manifest $(CLASSES)
rm Manifest
chmod +x $(JARFILE)
$(CLASSES): $(JAVASRC)
javac -Xlint $(JAVASRC)
clean:
rm $(CLASSES)
This new Makefile compiles both HelloUser classes (even
though neither one depends on the other.) Notice
however the entry point for program execution has been
changed to function main() in your program
HelloUser2.java. Macros make it easy to make changes like
this, so learn to use them. To learn more
about Makefiles follow the links posted on the class webpage.
We've discussed three Makefiles in this project. If you rename
them Makefile1, Makefile2 and
Makefile3 respectively (since you can't have three files with the
same name), you'll find that the make
command does not work since a file called Makefile no longer
exists. Instead of renaming files to run the
7
Makefile you want, you can use the -f option to the make
command, and specify the name of your Makefile.
For instance
% make -f Makefile2
runs Makefile2. If you want to specify something other than the
first target, place it after the file name on
the command line. For instance
% make –f Makefile3 clean
runs the clean target in Makefile3.
What to turn in
That was a lot of work. You should have a folder Lab1. In that
folder, the following files should be pushed
into the repo:
HelloUser.java, HelloUser2.java, Makefile1, Makefile2,
Makefile3,
README
You must follow the naming convention exactly, including the
capitalization. Do not call your file
“hellouser.java” or “helloUser.java”, etc.
For this and every lab and programming assignment this quarter,
you will submit a zip file of a subdirectory
created for that specific assignment. The subdirectory for this
lab assignment must be named lab1 and the
zip file should be named lab1.zip. The names for the others will
be similar (e.g. lab2, prog1, prog2, etc.).
All program source files you turn in for this and other
assignments (in both 12B and 12M) should begin
with a comment block giving your name, cruzid, class (12B or
12M), a short description of its role in the
project, the file name and any special instructions for compiling
and/or running it. In this next assignment
we’ll spend more time on documentation. Every submission
should also include a README file that is a
plain text file. In general README lists all the files being
submitted (including README) along with any
special notes to the grader. Use what you've learned in this
project to write a Makefile that creates and
archives both .class files (HelloUser.class and HelloUser2.class)
in an executable jar file called
Hello that runs function main() from HelloUser2.class. It
should also include a clean utility, as in the
above examples.
For this assignment your lab1 directory should contain at least:
README (a plain text file), Makefile (the
last one as specified just above), Hello (a jar file),
HelloUser.java, HelloUser2.java. It may also contain
other files you created as part of this assignment (e.g.
Makefile1). It is ok if it contains your .class files, but
they are not needed and it would be best to remove them before
creating the zip file. You can use "make –
f Makefile clean" to do that. Submit the zip file on Canvas.
There is a lot in this assignment so start early and ask questions
in lab section or office hours if anything is
unclear.
Grading:
• (10 points) Everything done correctly
• (9 points) No README, but everything else correct
• (8 points) Some error in makefiles
• (7 points) Only turning in java files
• (5 or less points) Submitting something
8
If files are named incorrectly, you get no credit.

More Related Content

Similar to 1 CMPS 12M Data Structures Lab Lab Assignment 1 .docx

Similar to 1 CMPS 12M Data Structures Lab Lab Assignment 1 .docx (20)

Basics of Linux
Basics of LinuxBasics of Linux
Basics of Linux
 
lec1.docx
lec1.docxlec1.docx
lec1.docx
 
Tutorial 2
Tutorial 2Tutorial 2
Tutorial 2
 
Chapter 2 unix system commands
Chapter 2 unix system commandsChapter 2 unix system commands
Chapter 2 unix system commands
 
Unix Security
Unix SecurityUnix Security
Unix Security
 
Edubooktraining
EdubooktrainingEdubooktraining
Edubooktraining
 
Linux filesystemhierarchy
Linux filesystemhierarchyLinux filesystemhierarchy
Linux filesystemhierarchy
 
Bc0056 unix operating system
Bc0056   unix operating systemBc0056   unix operating system
Bc0056 unix operating system
 
Common linux ubuntu commands overview
Common linux  ubuntu commands overviewCommon linux  ubuntu commands overview
Common linux ubuntu commands overview
 
Operating system lab manual
Operating system lab manualOperating system lab manual
Operating system lab manual
 
Linux advanced privilege escalation
Linux advanced privilege escalationLinux advanced privilege escalation
Linux advanced privilege escalation
 
60761 linux
60761 linux60761 linux
60761 linux
 
Linux
LinuxLinux
Linux
 
Command Line Tools
Command Line ToolsCommand Line Tools
Command Line Tools
 
UnixShells.ppt
UnixShells.pptUnixShells.ppt
UnixShells.ppt
 
OS Lab Manual.pdf
OS Lab Manual.pdfOS Lab Manual.pdf
OS Lab Manual.pdf
 
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformaticsBITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
 
50 Most Frequently Used UNIX Linux Commands -hmftj
50 Most Frequently Used UNIX  Linux Commands -hmftj50 Most Frequently Used UNIX  Linux Commands -hmftj
50 Most Frequently Used UNIX Linux Commands -hmftj
 
Linux
LinuxLinux
Linux
 
Bootcamp linux commands
Bootcamp linux commandsBootcamp linux commands
Bootcamp linux commands
 

More from joyjonna282

In 2011, John Jones, a middle school social science teacher began .docx
In 2011, John Jones, a middle school social science teacher began .docxIn 2011, John Jones, a middle school social science teacher began .docx
In 2011, John Jones, a middle school social science teacher began .docx
joyjonna282
 
In 2005, serial killer Dennis Rader, also known as BTK, was arrested.docx
In 2005, serial killer Dennis Rader, also known as BTK, was arrested.docxIn 2005, serial killer Dennis Rader, also known as BTK, was arrested.docx
In 2005, serial killer Dennis Rader, also known as BTK, was arrested.docx
joyjonna282
 
In 2014 Virginia scientist Eric Betzig won a Nobel Prize for his res.docx
In 2014 Virginia scientist Eric Betzig won a Nobel Prize for his res.docxIn 2014 Virginia scientist Eric Betzig won a Nobel Prize for his res.docx
In 2014 Virginia scientist Eric Betzig won a Nobel Prize for his res.docx
joyjonna282
 

More from joyjonna282 (20)

In a 250-300 word response, critically examine your personal level o.docx
In a 250-300 word response, critically examine your personal level o.docxIn a 250-300 word response, critically examine your personal level o.docx
In a 250-300 word response, critically examine your personal level o.docx
 
In a 10 –12 page paper, identify and analyze the benefits and challe.docx
In a 10 –12 page paper, identify and analyze the benefits and challe.docxIn a 10 –12 page paper, identify and analyze the benefits and challe.docx
In a 10 –12 page paper, identify and analyze the benefits and challe.docx
 
In a 1-2 page Microsoft Word document, discuss the following case st.docx
In a 1-2 page Microsoft Word document, discuss the following case st.docxIn a 1-2 page Microsoft Word document, discuss the following case st.docx
In a 1-2 page Microsoft Word document, discuss the following case st.docx
 
In a 16–20 slide PowerPoint presentation (excluding title and refere.docx
In a 16–20 slide PowerPoint presentation (excluding title and refere.docxIn a 16–20 slide PowerPoint presentation (excluding title and refere.docx
In a 16–20 slide PowerPoint presentation (excluding title and refere.docx
 
In a 1-2 page Microsoft Word document, using APA, discuss the follow.docx
In a 1-2 page Microsoft Word document, using APA, discuss the follow.docxIn a 1-2 page Microsoft Word document, using APA, discuss the follow.docx
In a 1-2 page Microsoft Word document, using APA, discuss the follow.docx
 
In a 1-2 page paper, discuss how the government, the media, and the .docx
In a 1-2 page paper, discuss how the government, the media, and the .docxIn a 1-2 page paper, discuss how the government, the media, and the .docx
In a 1-2 page paper, discuss how the government, the media, and the .docx
 
In 2010, plans were announced for the construction of an Islamic cul.docx
In 2010, plans were announced for the construction of an Islamic cul.docxIn 2010, plans were announced for the construction of an Islamic cul.docx
In 2010, plans were announced for the construction of an Islamic cul.docx
 
In 2011, John Jones, a middle school social science teacher began .docx
In 2011, John Jones, a middle school social science teacher began .docxIn 2011, John Jones, a middle school social science teacher began .docx
In 2011, John Jones, a middle school social science teacher began .docx
 
In 5-7 pages (double-spaced,) provide a narrative explaining the org.docx
In 5-7 pages (double-spaced,) provide a narrative explaining the org.docxIn 5-7 pages (double-spaced,) provide a narrative explaining the org.docx
In 5-7 pages (double-spaced,) provide a narrative explaining the org.docx
 
In 2004 the Bush Administration enacted changes to the FLSA and the .docx
In 2004 the Bush Administration enacted changes to the FLSA and the .docxIn 2004 the Bush Administration enacted changes to the FLSA and the .docx
In 2004 the Bush Administration enacted changes to the FLSA and the .docx
 
In 200-250 wordsGiven the rate of technological chang.docx
In 200-250 wordsGiven the rate of technological chang.docxIn 200-250 wordsGiven the rate of technological chang.docx
In 200-250 wordsGiven the rate of technological chang.docx
 
in 200 words or more..1  do you use twitter if so , how often do.docx
in 200 words or more..1  do you use twitter if so , how often do.docxin 200 words or more..1  do you use twitter if so , how often do.docx
in 200 words or more..1  do you use twitter if so , how often do.docx
 
In 200 words or more, answer the following questionsAfter reading .docx
In 200 words or more, answer the following questionsAfter reading .docxIn 200 words or more, answer the following questionsAfter reading .docx
In 200 words or more, answer the following questionsAfter reading .docx
 
In 2005, serial killer Dennis Rader, also known as BTK, was arrested.docx
In 2005, serial killer Dennis Rader, also known as BTK, was arrested.docxIn 2005, serial killer Dennis Rader, also known as BTK, was arrested.docx
In 2005, serial killer Dennis Rader, also known as BTK, was arrested.docx
 
In 2003, China sent a person into space. China became just the third.docx
In 2003, China sent a person into space. China became just the third.docxIn 2003, China sent a person into space. China became just the third.docx
In 2003, China sent a person into space. China became just the third.docx
 
In 250 words briefly describe the adverse effects caused by exposure.docx
In 250 words briefly describe the adverse effects caused by exposure.docxIn 250 words briefly describe the adverse effects caused by exposure.docx
In 250 words briefly describe the adverse effects caused by exposure.docx
 
In 2.5 pages, compare and contrast health care reform in two differe.docx
In 2.5 pages, compare and contrast health care reform in two differe.docxIn 2.5 pages, compare and contrast health care reform in two differe.docx
In 2.5 pages, compare and contrast health care reform in two differe.docx
 
In 2014 Virginia scientist Eric Betzig won a Nobel Prize for his res.docx
In 2014 Virginia scientist Eric Betzig won a Nobel Prize for his res.docxIn 2014 Virginia scientist Eric Betzig won a Nobel Prize for his res.docx
In 2014 Virginia scientist Eric Betzig won a Nobel Prize for his res.docx
 
In 200-300 words  - How is predation different from parasitism What.docx
In 200-300 words  - How is predation different from parasitism What.docxIn 200-300 words  - How is predation different from parasitism What.docx
In 200-300 words  - How is predation different from parasitism What.docx
 
In 3 and half pages, including a title page and a reference page, di.docx
In 3 and half pages, including a title page and a reference page, di.docxIn 3 and half pages, including a title page and a reference page, di.docx
In 3 and half pages, including a title page and a reference page, di.docx
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

1 CMPS 12M Data Structures Lab Lab Assignment 1 .docx

  • 1. 1 CMPS 12M Data Structures Lab Lab Assignment 1 The purpose of this assignment is threefold: (1) get a basic introduction to the Andrew File System (AFS) which is the file system used by the ITS UNIX timeshare, (2) learn how to create an executable jar file containing a Java program, and (3) learn to automate compilation and other tasks using Makefiles. Part 1: HELLO WORLD Start by creating a directory for this course. Fire up a terminal. (Let % denote the prompt.) Run: % cd ~/Desktop % mkdir CMPS12B % cd CMPS12B % pwd (You should see which directory you’re in. You’re free to create this directory anywhere, but follow the above if you’re not sure.) In this directory, create a file HelloWorld.java with the following text. //----------------------------------------------------- // HelloWorld.java
  • 2. // Prints "Hello world", the first program you should always write //----------------------------------------------------- class HelloWorld { static public void main(String[] args) { System.out.println("Hello world"); } } Go to a terminal, and get into the directory with this file. Run % javac HelloWorld.java % java HelloWorld You should see “Hello World” printed out. If not, you’ll need to install Java on your machine. Or, you can optionally work on the UNIX timeshare. Talk to the lab instructor for more directions. Part 2: AFS Learning a bit about AFS is useful in terms of knowing some typical OS/file-system operations. The UNIX time share systems are also a good option if you are having trouble with compiling and running Java or C programs on your laptop. The UNIX servers have all of the tools you need to complete all assignments for this class, although working on your own laptop can be more convenient and will also allow you to use a modern, graphical IDE. Logon to your ITS UNIX timeshare account at UNIX.ucsc.edu. If you don't know how to do this read the instructions at the URL below. You can also ask for help at a lab session but you should really figure this out on your own if possible.
  • 3. https://its.ucsc.edu/unix-timeshare/tutorials/how-to- connect.html 2 Create a directory cs12b with a subdirectory lab1. From within lab1 create a subdirectory called private, then set access permissions on the new directory so that other users cannot view its contents. Do all this by typing the lines below. The UNIX prompt is depicted here as %, although it may look different in your login session. Those lines without the UNIX prompt are the output of your typed commands. % mkdir cs12b % cd cs12b % mkdir lab1 % cd lab1 % mkdir private % fs setacl private system:authuser none % fs setacl private system:anyuser none % fs listacl private Access list for private is Normal rights: foobar rlidwka Here foobar will be replaced by your own cruzid. The last line of output says that your access rights to directory private are rlidwka which means: read, list, insert, delete, write, lock, and administer. In other words you have all rights in this directory, while other users have none. If you are unfamiliar with any
  • 4. UNIX command, you can view its manual page by typing: man <command name>. (Do not type the angle brackets <>.) For instance man mkdir brings up the man pages for mkdir. Man pages can be very cryptic, especially for beginners, but it is best to get used to reading them as soon as possible. Under AFS, fs denotes a file system command, setacl sets the access control list (ACL) for a specific user or group of users, and listacl displays the access lists for a given directory. The command % fs setacl <some directory> <some user> <some subset of rlidwka or all or none> sets the access rights for a directory and a user. Note that setacl can be abbreviated as sa and listacl can be abbreviated as la. For instance do la on your home directory: % fs la ~ Access list for /afs/cats.ucsc.edu/users/a/foobar is Normal rights: system:authuser l foobar rlidwka The path /afs/cats.ucsc.edu/users/a/foobar will be replaced by the full path to your home directory, and your own username in place of foobar. Note that ~ (tilde) always refers to your home directory, . (dot) always refers to your current working directory (the directory where you are currently located) and .. (dot, dot) refers to the parent of your current working directory. The group system:authuser refers to anyone with an account on the ITS UNIX timeshare. Thus by default, any user on the system can list the contents of your home directory. No other permissions are set
  • 5. for system:authuser however, so again by default, no one else can read, insert, delete, write, lock, or administer your files. Do fs la ~/cs12b and verify that the access rights are the same for the child directory cs12b. Create a subdirectory of private, call it anything you like, and check its access rights are the same as for its parent. Thus we see that child directories inherit their permissions from the parent directory when they are created. To get a more comprehensive list of AFS commands do fs help. For instance you will see that fs lq shows your quota and usage statistics. For more on the Andrew File System go to 3 http://claymore.rfmh.org/public/computer_resources/AFSinfo.ht ml. While your assignments are solo assignments to be done alone. For this lab you can find another student and as an exercise allow your partner read access to the directory where you are working on a shared assignment, e.g. prog1, do the following: % cd cs12b % mkdir prog1 % fs setacl prog1 system:anyuser none % fs setacl prog1 system:authuser none % fs setacl prog1 partnerID read % fs listacl prog1 Access list for prog1 is
  • 6. Normal rights: system:operator rlidwk system:administrators rlidwka partnerID rl mcdowell rlidwka Of course, using other options with "fs setacl" you could also give your partner write access to the directory, although you should do that with great caution. Try and create some files and see if your partner can access them. Part 3: Jar Files Copy the following file to your lab1 directory. Part of this exercise is to be able to use the text editor. You want to complete Lab1 knowing that you are 100% comfortable that you can edit and compile simple files. //------------------------------------------------------------------------- ---- // HelloUser.java // This is a really simple program to get you started. // Prints greeting to stdout, then prints out some environment information. //------------------------------------------------------------------------- ---- class HelloUser{ public static void main( String[] args ){ String userName = System.getProperty("user.name"); /* this should be you! */ String os = System.getProperty("os.name"); String osVer = System.getProperty("os.version"); String jre = System.getProperty("java.runtime.name"); String jreVer = System.getProperty("java.runtime.version");
  • 7. String jvm = System.getProperty("java.vm.name"); String jvmVer = System.getProperty("java.vm.version"); String javaHome = System.getProperty("java.home"); long freemem = Runtime.getRuntime().freeMemory();/* scary if 0! */ long time = System.currentTimeMillis(); System.out.println("Hello "+userName); System.out.println("Operating system: "+os+" "+osVer); System.out.println("Runtime environment: "+jre+" "+jreVer); System.out.println("Virtual machine: "+jvm+" "+jvmVer); System.out.println("Java home directory: "+javaHome); System.out.println("Free memory: "+freemem+" bytes"); System.out.printf("Time: %tc.%n", time); } } 4 You can compile this in the normal way by doing javac HelloUser.java then run it by doing the command java HelloUser. Java provides a utility called jar for creating compressed archives of executable .class files. This utility can also be used to create an executable jar file that can then be run by just typing its name at the UNIX prompt (with no need to type java first). To do this you must first create a manifest file that specifies the entry point for program execution, i.e. which .class file contains the main() method to be executed. Create a text file called Manifest containing just one line:
  • 8. Main-class: HelloUser If you don’t feel like opening up an editor to do this you can just type % echo Main-class: HelloUser > Manifest The UNIX command echo prints text to stdout, and > redirects the output to a file. Now do % jar cvfm HelloUser Manifest HelloUser.class The first group of characters after jar are options. (c: create a jar file, v: verbose output, f: second argument gives the name of the jar file to be created, m: third argument is a manifest file.) Consult the man pages to see other options to jar. The second argument HelloUser is the name of the executable jar file to be created. The name of this file can be anything you like, i.e. it does not have to be the same as the name of the .class file containing function main(). For that matter, the manifest file need not be called Manifest, but this is the convention. Following the manifest file is the list of .class files to be archived. In our example this list consists of just one file: HelloUser.class. At this point we would like to run the executable jar file HelloUser by just typing its name, but there is one problem. This file is not recognized by UNIX as being executable. To remedy this do %chmod +x HelloUser As usual, consult the man pages to understand what chmod does. Now type HelloUser to run the program. The whole process can be accomplished by typing five lines:
  • 9. % javac –Xlint HelloUser.java % echo Main-class: HelloUser > Manifest % jar cvfm HelloUser Manifest HelloUser.class % rm Manifest % chmod +x HelloUser Notice we have removed the now unneeded manifest file. Note also that the –Xlint option to javac enables recommended warnings. The only problem with the above procedure is that it’s a big hassle to type all those lines. Fortunately there is a UNIX utility that can automate this and other processes. Part 4: Makefiles Large programs are often distributed throughout many files that depend on each other in complex ways. Whenever one file changes all the files depending on it must be recompiled. When working on such a program it can be difficult and tedious to keep track of all the dependencies. The UNIX make utility automates this process. The command make looks at dependency lines in a file named Makefile. The dependency lines indicate relationships between source files, indicating a target file that depends on one or more prerequisite files. If a prerequisite has been modified more recently than its target, make updates the target file based on construction commands that follow the dependency line. make will normally stop if it encounters an error during the construction process. Each dependency line has the following format. 5
  • 10. target: prerequisite-list construction-commands The dependency line is composed of the target and the prerequisite-list separated by a colon. The construction-commands may consist of more than one line, but each line must start with a tab character. Start an editor and copy the following lines into a file called Makefile. # A simple Makefile HelloUser: HelloUser.class echo Main-class: HelloUser > Manifest jar cvfm HelloUser Manifest HelloUser.class rm Manifest chmod +x HelloUser HelloUser.class: HelloUser.java javac -Xlint HelloUser.java clean: rm -f HelloUser HelloUser.class Anything following # on a line is a comment and is ignored by make. The second line says that the target HelloUser depends on HelloUser.class. If HelloUser.class exists and is up to date, then HelloUser can be created by doing the construction commands that follow. Remember that all indentation is accomplished via the tab character. The next target is HelloUser.class which depends on HelloUser.java. The next target clean is what is sometimes called a phony target since it doesn’t depend on anything and just runs a command. Likewise the target submit does not compile anything, but does have some dependencies. Any target can be built (or perhaps
  • 11. executed if it is a phony target) by doing make <target name>. Just typing make creates the first target listed in the Makefile. Try this by doing make clean to get rid of all your previously compiled stuff, then do make again to see it all created again. Your output from make should look something like: % make javac -Xlint HelloUser.java echo Main-class: HelloUser > Manifest jar cvfm HelloUser Manifest HelloUser.class added manifest adding: HelloUser.class(in = 1577) (out= 843)(deflated 46%) rm Manifest chmod +x HelloUser The make utility allows you to create and use macros within a Makefile. The format of a macro definition is ID = list where ID is the name of the macro (by convention all caps) and list is a list of filenames. Then $(list) refers to the list of files. Move your existing Makefile to a temporary file, then start your editor and copy the following lines to a new file called Makefile. #------------------------------------------------------------------------- ----- # A Makefile with macros #------------------------------------------------------------------------- ----- JAVASRC = HelloUser.java SOURCES = README Makefile $(JAVASRC) MAINCLASS = HelloUser CLASSES = HelloUser.class JARFILE = HelloUser
  • 12. 6 all: $(JARFILE) $(JARFILE): $(CLASSES) echo Main-class: $(MAINCLASS) > Manifest jar cvfm $(JARFILE) Manifest $(CLASSES) rm Manifest chmod +x $(JARFILE) $(CLASSES): $(JAVASRC) javac -Xlint $(JAVASRC) clean: rm $(CLASSES) $(JARFILE) Run this new Makefile and observe that it is equivalent to the previous one. The macros define text substitutions that happen before make interprets the file. Study this new Makefile until you understand exactly what substitutions are taking place. Now create your own Hello program and call it HelloUser2.java. It can say anything you like, but just have it say something different from the original. Add HelloUser2.java to the JAVASRC list, add HelloUser2.class to the CLASSES list and change MAINCLASS to HelloUser2. Also change the name of JARFILE to just Hello (emphasizing that the jar file can have any name.) #------------------------------------------------------------------------- -----
  • 13. # Another Makefile with macros #------------------------------------------------------------------------- ----- JAVASRC = HelloUser.java HelloUser2.java SOURCES = README Makefile $(JAVASRC) MAINCLASS = HelloUser2 CLASSES = HelloUser.class HelloUser2.class JARFILE = Hello all: $(JARFILE) $(JARFILE): $(CLASSES) echo Main-class: $(MAINCLASS) > Manifest jar cvfm $(JARFILE) Manifest $(CLASSES) rm Manifest chmod +x $(JARFILE) $(CLASSES): $(JAVASRC) javac -Xlint $(JAVASRC) clean: rm $(CLASSES) This new Makefile compiles both HelloUser classes (even though neither one depends on the other.) Notice however the entry point for program execution has been changed to function main() in your program HelloUser2.java. Macros make it easy to make changes like this, so learn to use them. To learn more about Makefiles follow the links posted on the class webpage. We've discussed three Makefiles in this project. If you rename them Makefile1, Makefile2 and Makefile3 respectively (since you can't have three files with the same name), you'll find that the make
  • 14. command does not work since a file called Makefile no longer exists. Instead of renaming files to run the 7 Makefile you want, you can use the -f option to the make command, and specify the name of your Makefile. For instance % make -f Makefile2 runs Makefile2. If you want to specify something other than the first target, place it after the file name on the command line. For instance % make –f Makefile3 clean runs the clean target in Makefile3. What to turn in That was a lot of work. You should have a folder Lab1. In that folder, the following files should be pushed into the repo: HelloUser.java, HelloUser2.java, Makefile1, Makefile2, Makefile3, README You must follow the naming convention exactly, including the capitalization. Do not call your file “hellouser.java” or “helloUser.java”, etc.
  • 15. For this and every lab and programming assignment this quarter, you will submit a zip file of a subdirectory created for that specific assignment. The subdirectory for this lab assignment must be named lab1 and the zip file should be named lab1.zip. The names for the others will be similar (e.g. lab2, prog1, prog2, etc.). All program source files you turn in for this and other assignments (in both 12B and 12M) should begin with a comment block giving your name, cruzid, class (12B or 12M), a short description of its role in the project, the file name and any special instructions for compiling and/or running it. In this next assignment we’ll spend more time on documentation. Every submission should also include a README file that is a plain text file. In general README lists all the files being submitted (including README) along with any special notes to the grader. Use what you've learned in this project to write a Makefile that creates and archives both .class files (HelloUser.class and HelloUser2.class) in an executable jar file called Hello that runs function main() from HelloUser2.class. It should also include a clean utility, as in the above examples. For this assignment your lab1 directory should contain at least: README (a plain text file), Makefile (the last one as specified just above), Hello (a jar file), HelloUser.java, HelloUser2.java. It may also contain other files you created as part of this assignment (e.g. Makefile1). It is ok if it contains your .class files, but they are not needed and it would be best to remove them before creating the zip file. You can use "make – f Makefile clean" to do that. Submit the zip file on Canvas. There is a lot in this assignment so start early and ask questions in lab section or office hours if anything is
  • 16. unclear. Grading: • (10 points) Everything done correctly • (9 points) No README, but everything else correct • (8 points) Some error in makefiles • (7 points) Only turning in java files • (5 or less points) Submitting something 8 If files are named incorrectly, you get no credit.