Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Workshop: Using Expressions
As you worked on a particularly unpleasant math problem in school, 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 Variable.java file if it is still open. Create a new file and save it as Elvis.java. The Elvis program creates a fictional 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.
Example 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 SDK, in the same folder as the Elvis.java file, type the following at a command line to compile the Elvis application:
javac Elvis.java
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. SDK users should type the following at a command line:
java Elvis
Listing 5.3 shows the output for this program.
Example 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:
- Lines 3–4: The initial weight of Elvis is set to 250.
- Lines 5–8: Elvis gains weight.
- Lines 9–11: Elvis loses weight.
- Lines 12–15: Elvis reduces in size dramatically.
- 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.
All About Operators | Next Section

Account Sign In
View your cart