Most of you are using Emacs in black-and-white, but it has a color feature as well. When you open a .java file Emacs goes into "CC mode", which is what it uses for editing C, C++, and Java files. In CC mode Emacs knows about Java syntax, including appropriate indentation, comments, strings, and so on. When you use colors, Emacs will color different parts of a program differently can you can tell at a glance what's what -- reserved words are purple, comments are red, types are green, and so on. To use color, do the following:
Shell Scripts
Another 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 -h
This looks just like the print command you have been using, but it has a $1
instead of the filename. This 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 print
Look at the permissions; you should see that no one has execute permission.
To change this, issue the following command:
chmod a+x print
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 filename
This tells it to use the print command in your home directory to print
filename.
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 command
It 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. 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);
radius = 20;
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:
Input
In the program above, you showed what happened to the circumference and area of a circle when the radius went from 10 to 20. Does the same thing happen whenever the radius doubles, or were those answers just for those particular values? To figure this out, you can write a program that reads in values for the radii from the user instead of having them written into the program ("hardcoded"). Modify your program as follows:
import cs1.Keyboard;This tells the compiler that you will be using methods from the Keyboard class.
Making a tar file
An easy way to send someone several files is to make a tar file. (Tar stands for tape archive, a rather archaic name for this function.) Although you only have two files of interest to send for this lab (Circle.java and Circle.class), this is a good time to get used to using tar files. 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