More Variables and Printing

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.

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) + "." );
    }
}

What You Should See

U:\My Documents\CompSci\>java MoreVariablesAndPrinting
Let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.

U:\My Documents\CompSci\>

What You Should Do on Your Own

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

  1. Change all the variables so there isn't the my in front. Make sure you change the name everywhere, not just where you used = to set them.
  2. Try to write some variables that convert the inches and pounds to centimeters and kilos. Don't just type in the measurements, but work out the math inside your Java program.
Let's talk about Zed A. Shaw.
He's 74 inches (or 187.96 cm) tall.
He's 180 pounds (or 81.6466266 kg) heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.

Frequently-Asked Questions

Can I make a variable like this: 1 = "Zed Shaw"?
No, 1 is not a valid variable name. They need to start with a letter, so a1 would work, but 1 will not, and neither would 1a.
How can I round a floating point number?
You can use the round() function like this: Math.round(1.7333).
Why does this not make sense to me?
I'm not sure, but you can try changing the numbers in this program to your own information (your height and weight, your eye color, etc). It's weird, but talking about yourself will make it seem more "real".



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.)