Lab 2 In-Class: Variables, Expressions and Types

Getting Started

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

Some System Hints

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


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:
// 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, into your lab2 directory and modify it as follows:

  1. Separate the declarations of variables radius and area from their initializations. While this is ok as it is, sometimes it is easier to read and modify a program in which the variables are declared first, and assignments and calculations are done later.

  2. 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:

  3. 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 can (and 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? (You should be able to figure out mathematically what the answers should be. If your results aren't correct, check your program.)

  4. 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 to turn in. Use the command


  5. File 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:
    //  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 and do the following:

    Print your to turn in.

  6. Suppose your lab instructor has a somewhat complicated method of determining your grade on a lab. Each lab consists of two out-of-class activities -- a pre-lab assignment and a post-lab assignment -- plus the in-class activities. The in-class work is 60% of the lab grade and the out-of-class work is 40% of the lab grade. Each component of the grade is based on a different number of points (and this varies from lab to lab) -- for example, the pre-lab may be graded on a basis of 20 points (so a student may earn 17 out of 20 points) whereas the post-lab is graded on a basis of 30 points and the in-class 25 points. To determine the out-of-class grade the instructor takes the total points earned (pre plus post) divided by the maximum possible number of points, multiplied by 100 to convert to percent; the in-class grade is just the number of points earned divided by the maximum points, again converted to percent.

    The program (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%. Note that the constants store the out-of-class and in-class weights and that variables are used to store the values entered by the user. Do the following:

    1. First study the program. Note that after the constants and variables are declared the program prints a welcome message and then there are 6 prompts asking the user for input. Each prompt is followed by an assignment statement that takes the value read in and stores it in a variable. Suppose a user runs the program and enters the values shown below in response to the prompts.
      Welcome to the Lab Grade Calculator
      Enter the number of points you earned on the pre-lab: 17
      What was the maximum number of points you could have earned? 20
      Enter the number of points you earned on the lab: 23
      What was the maximum number of points for the lab? 25
      Enter the number of points you earned on the post-lab: 11
      What was the maximum number of points for the post-lab? 15
      • Fill in the values of each variable after the input is read in:
        preLabPts _______      preLabMax ________     labPts _______   labMax ______
        postLabPts _______     postLabMax ________
      • After the input is read in, the program has three assignment statements that perform calculations. The first of these computes the average for the out of class work.
        outClassAvg = preLabPts + postLabPts / preLabMax + postLabMax * 100;
        The expression on the right side of the assignment has 4 operators (two +'s, a /, and a *). Show the order in which the operations will be performed by numbering the operations -- that is, put a 1 above the operation that will be performed first, a 2 above the operation that will be performed next, and so on.

        Now, for the particular input above the expression in the assignment becomes

                17 + 11 / 20 + 15 * 100
        Show what the computer will get for the value of this expression (show the steps and the final answer).

      • The following assignment statement computes the in-class average. As above, number the operations and then show how the computer will evaluate the expression (and what answer it will get).

                 inClassAvg =  labPts / labMax * 100;

    2. Now run the program, typing in the same input as above. Clearly the output is incorrect! Is it what you expected? Correct the program. This involves writing the expressions to do the calculations correctly. The correct answers for the given input should be an out of class average of 80 (the student earned 28 points out of a possible 35 which is 80%), an in-class average of 92 (23 points out of 25), and a lab grade of 87.2 (40% of 80 plus 60% of 92).

    Print your corrected 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