Skip to main content
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.
VariablesAndNames.java
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." );
    }
}
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.
Now we’ll do even more typing of variables and printing them out. Every time you put ” (double-quotes) around a piece of text you have been making a string. A string is how you make something that your program might give to a human. You print them, save them to files, send them to web servers, all sorts of things.Strings are really handy, so in this exercise you’ll learn how to make strings that have variables embedded in them.As usual, just type this in even if you don’t understand it and make it exactly the same.
VariablesAndNames.java
public class MoreVariablesAndPrinting
{
    public static void main( String[] args )
    {
        String myName, myEyes, myTeeth, myHair;
        int myAge, myHeight, myWeight;

        myName = "Zed A. Shaw";
        myAge = 35;     // not a lie
        myHeight = 74;  // inches
        myWeight = 180; // lbs
        myEyes = "Blue";
        myTeeth = "White";
        myHair = "Brown";

        System.out.println( "Let's talk about " + myName + "." );
        System.out.println( "He's " + myHeight + " inches tall." );
        System.out.println( "He's " + myWeight + " pounds heavy." );
        System.out.println( "Actually, that's not too heavy." );
        System.out.println( "He's got " + myEyes + " eyes and " + myHair + " hair." );
        System.out.println( "His teeth are usually " + myTeeth + " depending on the coffee." );

        // This line is tricky; try to get it exactly right.
        System.out.println( "If I add " + myAge + ", " + myHeight + ", and " + myWeight
            + " I get " + (myAge + myHeight + myWeight) + "." );
    }
}
Write a program that creates three variables: an int, a double, and a String.Put the value 113 into the first variable, the value 2.71828 into the second, and the value "Computer Science" into the third. It does not matter what you call the variables… this time.Then, display the values of these three variables on the screen, one per line.
This is room # 113
e is close to 2.71828
I am learning a bit about Computer Science
Your program SHOULD NOT look like this:
UsingVariables.java
System.out.println( "This is room # 113" );
System.out.println( "e is close to 2.71828" );
System.out.println( "I am learning a bit about Computer Science" );
You must use three variables. Your program will probably have nine lines of code inside the curly braces of main().
Write a program that stores your name and year of graduation into variables, and displays their values on the screen.Make sure that you use two variables, and that the variable that holds your name is the best type for such a variable, and that the variable that holds the year is the best type for that variable.Also make sure that your variable names are good: the name of a variable should always relate to its contents.
My name is Juan Valdez and I'll graduate in 2010.
Notice that in the example above, the values “Juan Valdez” and 2010 have been stored in variables before printing.You’re doing it wrong if your program looks like this:
StillUsingVariables.java
System.out.println( "My name is Juan Valdez and I'll graduate in 2010." );
You must use three variables. Your program will probably have nine lines of code inside the curly braces of main().
Use several variables to store the names of your classes and their teachers. Then, display a nice little table displaying your schedule. Just FYI, my column of courses has a width of 26 characters, and the teacher column has a width of 15. The first and last lines are a plus sign, fifty dashes (a.k.a. minus signs) and another plus sign.Your table doesn’t need to look exactly like this, or even line up. I used a total of sixteen variables (course1, course2, … course8, teacher1, teacher2, etc.). You should do the same.
+------------------------------------------------------------+
| 1 |                          English III |       Ms. Lapan |
| 2 |                          Precalculus |     Mrs. Gideon |
| 3 |                         Music Theory |       Mr. Davis |
| 4 |                        Biotechnology |      Ms. Palmer |
| 5 |           Principles of Technology I |      Ms. Garcia |
| 6 |                             Latin II |    Mrs. Barnett |
| 7 |                        AP US History | Ms. Johannessen |
| 8 | Business Computer Infomation Systems |       Mr. James |
+------------------------------------------------------------+