Previous Section  < Day Day Up >  Next Section

Workshop: Using Expressions

When you were younger in school, as you worked on a particularly unpleasant math problem, did you ever complain to a higher power, protesting that you would never use this knowledge in your life? Sorry to break this to you, but all your teachers were right—those math skills are going to be used in your computer programming.

That's the bad news. The good news is that the computer will do any of the math you ask it to do. As mentioned earlier in this hour, any instructions you give a computer program involving math are called expressions. Expressions will be used frequently in your computer programs. You can use them for tasks such as the following:

  • Changing the value of a variable

  • Counting the number of times something has happened in a program

  • Using a mathematical formula in a program

As you write computer programs, you will find yourself drawing on your old math lessons as you use expressions. Expressions can use addition, subtraction, multiplication, division, and modulus division.

To see expressions in action, return to your word processor and close the file if it is still open. Create a new file and save it as The Elvis program creates a virtual person whose weight loss and gain can be tracked with mathematical expressions. Instead of adding statements to the program piece-by-piece, enter the full text of Listing 5.2 into the word processor. Each part of the program will be discussed in turn.

Listing 5.2. The Elvis Program

 1: class Elvis {

 2:     public static void main(String[] arguments) {

 3:         int weight = 250;

 4:         System.out.println("Elvis weighs " + weight);

 5:         System.out.println("Elvis visits all-you-can-eat rib joint.");

 6:         System.out.println("Elvis throws Thanksgiving luau.");

 7:         weight = weight + 10;

 8:         System.out.println("Elvis now weighs " + weight);

 9:         System.out.println("Elvis discovers aerobics.");

10:         weight = weight - 15;

11:         System.out.println("Elvis now weighs " + weight);

12:         System.out.println("Elvis falls into washing machine during "

13:             + "shrink cycle.");

14:         weight = weight / 3;

15:         System.out.println("Elvis now weighs " + weight);

16:         System.out.println("Oops! Elvis clones himself 12 times.");

17:         weight = weight + (weight * 12);

18:         System.out.println("The 13 Elvii now weigh " + weight);

19:     }

20: }

When you're done, save the file and compile the program. If you're using the JDK in the same folder as the file, type the following at a command line to compile the Elvis application:


If it compiles without any errors, you will not see any output; javac only responds if something goes wrong. If you do see error messages, check the line number that is listed in each error message to look for typos. Correct any typos you find and compile the program.

Next, run the program. JDK users should type the following at a command line:

java Elvis

Listing 5.3 shows the output for this program.

Listing 5.3. The Output of the Elvis Program

Elvis weighs 250

Elvis visits all-you-can-eat rib joint.

Elvis throws Thanksgiving luau.

Elvis now weighs 260

Elvis discovers aerobics.

Elvis now weighs 245

Elvis falls into washing machine during shrink cycle.

Elvis now weighs 81

Oops! Elvis clones himself 12 times.

The 13 Elvii now weigh 1053

As in the other programs you have created, the Elvis program uses a main() block statement for all its work. This statement can be broken into the following five sections:

  1. Lines 3–4: The initial weight of Elvis is set to 250.

  2. Lines 5–8: Elvis gains weight.

  3. Lines 9–11: Elvis loses weight.

  4. Lines 12–15: Elvis reduces in size dramatically.

  5. Lines 16–18: Elvis multiplies.

Line 3 creates the weight variable and designates it as an integer variable with int. The variable is given the initial value 250 and used throughout the program to monitor Elvis' weight.

The next line is similar to several other statements in the program:

System.out.println("Elvis weighs " + weight);

The System.out.println() command displays a string that is contained within its parenthesis marks. In the preceding line, the text "Elvis weighs" is displayed, followed by the value of the weight variable. There are numerous System.out.println() statements in the program. If you're still unclear about how these statements work, look at each of them in Listing 5.2 and compare them to the corresponding lines in Listing 5.3.

    Previous Section  < Day Day Up >  Next Section