A handy system feature is the ability to put one or more commands into a file and then execute the file, instead of giving all of the commands directly to the shell. This is called a shell script. For example, we can use a shell script to keep from having to type that long print command (nenscript -2rG -p- filename | lpr -h) by putting the command, slightly modifed (see below), into a file called print (or something else if you prefer) in your home directory. After changing the permissions on the file to make it executable (text files aren't executable by default), you can refer to it instead of the whole command. Here are the steps:
nenscript -2rG -p- $1 | lpr -hThis looks just like the print command you have been using, but it has a $1 instead of the filename.(Another difference is that the
-P lab-mcsp
is gone, but this is because Brad has changed
the settings so that it now
prints to the lab printer by default.) The $1
is an argument, or parameter;
it tells the command to use whatever you type after
the name of this script file in the place of the $1.
ls -l printLook at the permissions; you should see that no one has execute permission. To change this, issue the following command:
chmod a+x printThe Unix command chmod changes the permissions, or mode, of a file. The "a+x" part means for all(a) users, add(+) the execute(x) privilege. The last part (print) is just the name of the file. If you look at the permissions again, you'll see that the file is now executable.
~/print filenameThis tells it to use the print command in your home directory to print the file.
The man pages
Unix has an online manual called the man pages. If there is any Unix command you would like to know more about, you can look it up in the man pages just by typing the following to the shell:
man commandIt will then display the documentation on command in "more" style, that is, one page at a time -- press space for the next page, q to get out, b to go back. For example, man ls will give information on the ls command. Note that commands that aren't standard in Unix may or may not have man pages, depending on whether documentation was installed with the command. Our system, for example, does not have man pages for nenscript. Use the man pages to answer the following questions:
Variables
A variable is a name for a memory location that holds a value. The value that is stored in this location can be changed, hence the name variable. In Java, a variable must be declared before it can be used. The declaration gives the type of value that will be stored so that the compiler knows how much space to allocate for it. By convention, Java variables start with a lower case letter.
A constant is a name for a memory location that holds a value that cannot be changed, so we can think of it simply as a name for the value itself. In Java, a constant declaration looks just like a variable declaration except it has the reserved word final in front. By convention, constants are written in all capitals so that they are easily distinguished from variables.
Study the program below, which uses both variables and constants:
//********************************************************** //File: Circle.java //Name: Adrienne Bloss //Purpose: Print the area of a circle with two different radii //********************************************************** public class Circle { public static void main(String[] args) { final double PI = 3.14159; int radius = 10; double area = PI * radius * radius; System.out.println("The area of a circle with radius " + radius + " is " + area); } }Some things to notice:
Save this program, which is in file Circle.java, into your lab2 directory and modify it as follows:
import cs1.Keyboard;This tells the compiler that you will be using methods from the Keyboard class.
Print your Circle.java to turn in.
//********************************************************** //File: Paint.java //Name: //Purpose: Determine how much paint is needed to paint the walls //(not including the floor or ceiling) of a room given its length, //width, and height //********************************************************** import cs1.Keyboard; public class Paint { public static void main(String[] args) { final double COVERAGE = 350.0; //paint covers 350 sq ft/gal //declare integers length, width, and height //declare integers sideWallArea, endWallArea, and totalArea //declare double paintNeeded //Prompt for and read in the length of the room //Prompt for and read in the width of the room //Prompt for and read in the height of the room //Compute the area of a side wall (running the length //of the room) in square feet. //Compute the area of an end wall (running the width //of the room) in square feet. //Compute the total square feet to be painted (4 walls!) //Compute the amount of paint needed //Print the length, width, and height of the room, the total //area, and the number of gallons of paint needed. } }Save this file to your lab2 directory and do the following:
Print your Paint.java to turn in.
The program LabGrade.java (printout attached) is supposed to compute the lab grade for a student. To do this it gets as input the number of points the student earned on the prelab assignment and the maximum number of points the student could have earned; the number of points earned on the lab itself and the maximum number of points; the number of points earned on the postlab assignment and the maximum number of points. The lab grade is computed as described above: the in-class and out-of-class grades (in percent) are computed separately then a weighted average of these is computed. The program currently assumes the out-of-class work counts 40% and the in-class counts 60%. Do the following:
preLabPts _______ preLabMax ________ labPts _______ labMax ______ postLabPts _______ postLabMax ________
First show the order in which the operations will be performed by drawing parentheses and numbering the expressions -- that is draw parentheses around the first expression that will be evaluated and mark it 1, then around the second expression and mark it 2, etc.).
outClassAvg = preLabPts + postLabPts / preLabMax + postLabMax * 100;Now, for the particular input the expression in the assignment becomes
17 + 12 / 20 + 15 * 100What is the value of this expression? ____________
inClassAvg = labPts / labMax * 100;
Print your corrected LabGrade.java to turn in.
Making a tar file
An easy way to send someone several files at once is to make a tar file. (Tar stands for tape archive, a rather archaic name for this function.) To "tar up" everything in your lab2 directory, do the following:
tar czf lab2.tgz .This strange looking command can be understood as follows:
c -- "create" a file containing the tarred up files z -- "zip" this file, that is, compress it so it takes less space f -- use the next argument to name this file lab2.tgz -- the name to be used for the tar file . -- tar up everything in the current directory
What to turn in