9. Variables and Names - Link
- Task
- Output
- Question
- FAQ
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:
- Write a comment above each line explaining to yourself what it does in English.
- Read your .java file backwards.
- Read your .java file out loud, saying even the punctuation and symbols.
VariablesAndNames.java
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.10. More Variables and Printing - Link
- Task
- Output
- Question
- FAQ
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
11. Using Variables - Link
- Task
- Answer
- Output
Write a program that creates three variables: an Your program SHOULD NOT look like this:You must use three variables. Your program will probably have nine lines of code inside the curly braces of
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.UsingVariables.java
main().12. Still Using Variables - Link
- Task
- Answer
- Output
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.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:You must use three variables. Your program will probably have nine lines of code inside the curly braces of
StillUsingVariables.java
main().13. Your Schedule - Link
- Task
- Answer
- Output
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.