Lab 2 In-Class: Variables, Expressions and Types

Getting Started

As usual, start Emacs, Netscape, and an xterm. Go to the class page and open this document in Netscape. In the xterm, change to your Labs directory, create a subdirectory called lab2, and change to that directory.

Some System Hints

Color Emacs

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:

You should now have a color Emacs window.This is particularly handy when you forget to close a quote or a comment, as you are likely to notice that the colors aren't right before you even compile it.

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:

You can now use this script to print from any directory with the following command:
           ~/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:

On to Java

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: Exercises

Save this program, which is in file Circle.java, into your lab2 directory and modify it as follows:

  1. The circumference of a circle is 2 times the product of Pi and the radius. Add statements to this program so that it computes the circumference in addition to the area for both circles. You will need to do the following: Be sure your results are clearly labeled.
  2. When the radius of a circle doubles, what happens to its circumference and area? Do they double as well? You can determine this by dividing the second area by the first area. Unfortunately, as it is now the program overwrites the first area with the second area (same for the circumference). You need to save the first area and circumference you compute instead of overwriting them with the second set of computations. So you'll need two area variables and two circumference variables, which means they'll have to have different names (e.g., area1 and area2). Remember that each variable will have to be declared. Modify the program as follows: Look at the results. Is this what you expected?

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:

Print this program to turn in. Be sure to change the documentation header to contain your name and to reflect what the program actually does.

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:

When you're done, do an ls and you should see lab2.tgz in your directory.

What to turn in