Variables and Names

You can print things out with System.out.println and you can do math. The next step is to learn about variables. In programming a variable is nothing more than a name for something so you can use the name rather than the something as you code. Programmers use these variable names to make their code read more like English, and because programmers have a lousy ability to remember things. If they didn't use good names for things in their software they'd get lost when they came back and tried to read their code again.

If you get stuck with this exercise, remember the tricks you've been taught so far for finding differences and focusing on details:

  1. Write a comment above each line explaining to yourself what it does in English.
  2. Read your .java file backwards.
  3. Read your .java file out loud, saying even the punctuation and symbols.
public class VariablesAndNames
{
    public static void main( String[] args )
    {
        int cars, drivers, passengers, cars_not_driven, cars_driven;
        double space_in_a_car, carpool_capacity, average_passengers_per_car;

        cars = 100;
        space_in_a_car = 4.0;
        drivers = 30;
        passengers = 90;
        cars_not_driven = cars - drivers;
        cars_driven = drivers;
        carpool_capacity = cars_driven * space_in_a_car;
        average_passengers_per_car = passengers / cars_driven;


        System.out.println( "There are " + cars + " cars available." );
        System.out.println( "There are only " + drivers + " drivers available." );
        System.out.println( "There will be " + cars_not_driven + " empty cars today." );
        System.out.println( "We can transport " + carpool_capacity + " people today." );
        System.out.println( "We have " + passengers + " to carpool today." );
        System.out.println( "We need to put about " + average_passengers_per_car + " in each car." );
    }
}

Note: The _ in space_in_a_car is called an underscore character. Find out how to type it if you do not already know. We use this character a lot to put an imaginary space between words in variable names.

What You Should See

U:\My Documents\CompSci\>java VariablesAndNames
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3.0 in each car.

U:\My Documents\CompSci\>

What You Should Do on Your Own

Assignments turned in without these things will not receive any points.

  1. I used 4.0 for space_in_a_car, but is that necessary? What happens if it's just 4?
  2. Remember that 4.0 is a "floating point" number. Find out what that means.
  3. Write comments above each of the variable assignments.
  4. Make sure you know what = is called (equals) and that it's making names for things.
  5. Remember _ is an underscore character.

Frequently-Asked Questions

What is the difference between = (single-equal) and == (double-equal)?
The = (single-equal) assigns the value on the right to a variable on the left. The == (double-equal) tests if two things have the same value. You'll learn more about comparing things in a later assignment.
What do you mean by "read the file backwards"?
Very simple. Imagine you have a file with 16 lines of code in it. Start at line 16, and compare it to my file at line 16. Then do it again for 15, and so on until you've read the whole file backwards.
Why did you use 4.0 for space_in_a_car? Changing it to 4 doesn't seem to do anything.
That is because space_in_a_car was previously defined as a double variable. If it had been defined as an int variable, putting 4 into it would have made a difference.



Copyright © 2010 Zed A. Shaw. Used by permission.

(The original Python version of this assignment is part of Zed Shaw's excellent Learn Python the Hard Way course and was translated to/reinterpreted for Java by Graham Mitchell.)