Lab 2 In-Class: Variables, Expressions and Types

Getting Started

As usual, start Emacs, Firefox, and an xterm window. In the browser go to the class page and open this document.

Some System Hints

Emacs settings

Unfortunately, typographical mistakes are regular occurances when programming. Needing to recompile because you capitalized 'Float' can get tedious after a while.

As we learned in the previous labs, emacs is a powerful tool in our pursuit of software development. In addition to text editing capabilities, it also knows a thing or two about JAVA syntax. Emacs is capable of color coding certain JAVA elements to alert you if something isn't quite right. For example - comments are coded as red, reserved words are purple, etc. You will quickly learn to rely on this powerful visual reminder to avoid simple mistakes.

To turn on color coding simply launch emacs and go to the Options menu. Then select the first choice - Syntax Highlighting . When you load a JAVA program (like PlusTest.java from lab1), you should see the effects of color coding

Shell Scripts

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 filename) 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 the file.

On to Java

In the xterm, change to your labs directory, create a subdirectory called lab2, and change to that directory.

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 simply a name for a value. As its name implies, its value cannot be changed. 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: 
//
// 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: Exercises

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

  1. Put your name in the header documentation!
  2. Currently each variable is declared and initialized in the same statement. However, often it is easier to read and modify a program in which the variables are declared first, and assignments and calculations are done later. Modify the program by separating the declarations of variables radius and area from their initializations. In particular, declare both variables first (after the declaration/initialization of the constant PI which should stay the same), then put the two assignment statements to assign values to each variable. Compile and run the program to make sure you did things correctly.

  3. The circumference of a circle is 2 times the product of PI and the radius. Add statements to this program so that it computes and prints the circumference for the circle. You will need to do the following:

  4. When the radius of a circle doubles, what happens to its circumference and area? Do they double as well? You can determine this by doubling the radius, then computing the area and circumference again and dividing the new values by the old. To do this you'll need new variables to hold the second area and circumference, since you need both areas and both circumferences to see how the value has changed. However, the new radius should be stored in the same variable as the old radius. Modify the program as follows: Look at the results. Is this what you expected? It is always important to verify the output of a program using some knowledge of what the answers should be. In this case you can figure out mathematically what the answers should be. Remember that the radius is multiplied by 2. Since the area formula squares the radius how much is the area multiplied by? What about the circumference? If your results aren't correct, check your program and correct it.

  5. 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 a value for the radius from the user instead of having it written into the program ("hardcoded"). Modify your program as follows:

    Print your Circle.java to turn in. Use the command

           ~/print Circle.java
    

  6. File Paint.java contains the partial program below, which when complete will calculate the amount of paint needed to paint the walls of a room of the given length and width. It assumes that the paint covers 350 square feet per gallon.
    //********************************************************************
    //  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 java.util.Scanner;
    
    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
    
            // Create a Scanner object (named scan)
    
            //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, open it in emacs, put your name in the header documentation and fill in the missing statements (the comments provide a guide) so that the program does what it is supposed to. Compile and run the program and correct any errors.

    Print your Paint.java to turn in.

  7. Integer Arithmetic and Data Conversion The file Change.java contains a program to compute the amount of change to be returned to a customer after a purchase. Save the program to your lab2 directory, open it in emacs and study it noting the following:

    Compile and run the program to see if the output matches what you would expect.

    Now add to the program as follows:

    Be sure your name is in the header documentation then print your Change.java program 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:

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

    What to turn in