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) + "." ); } }
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\>
Assignments turned in without these things will not receive any points.
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.
1 = "Zed Shaw"
?a1
would work, but 1 will not, and neither
would 1a.round()
function like this: Math.round(1.7333)
.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.)