Page 91 - Introduction to Programming with Java: A Problem Solving Approach
P. 91
3.18 Tracing
3.19 TypeCasting
3.20 charTypeandEscapeSequences
3.21 PrimitiveVariablesVersusReferenceVariables
3.22 Strings
3.23 Input—theScannerclass
3.24 GUITrack:InputandOutputwithJOptionPane(Optional)
3.1 Introduction
In solving a problem, it’s best to spend time first thinking about what you want to do and organizing your thoughts. In Chapter 2, you focused on the thinking and organizing by writing pseudocode algorithm solu- tions for given problem descriptions. In this chapter, you’ll take the next step—you’ll focus on writing solu- tions using a real programming language, Java. By using a real programming language, you’ll be able to run your program on a computer and produce results on a computer screen.
As you progress through this chapter, you’ll find that much of Java’s code parallels pseudocode. The primary difference is the precise syntax required for Java. Pseudocode syntax is lenient: Pseudocode must be clear enough so that humans can understand it, but the spelling and grammar need not be perfect. Pro- gramming-code syntax is stringent: It must be perfect in terms of spelling and grammar. Why? Because regular programming code is read by computers, and computers are not able to understand instructions un- less they’re perfect.
3.2 “I Have a Dream” Program
In this section, we present a simple program that prints a single line of text. In the next several sections, we’ll analyze the different components of the program. The analysis may be a bit dry, but bear with us. It’s impor- tant to understand the program’s components because all future programs will use those same components. In the rest of the chapter, we’ll introduce new concepts that enable us to present more substantial programs.
See Figure 3.1. It shows a program that prints “I have a dream!”1 In the upcoming sections, we’ll re- fer to it as the Dream program. The program contains comments for human readers and instructions for the computer to execute. We’ll analyze the comments first, and then we’ll move on to the
instructions. You can use this tiny program as a common starting point for all other Java
programs. Enter it, run it, and see what it does. Modify it, run it again, and so on, until you
have what you need.
1 Dr. Martin Luther King presented his famous “I have a dream” speech on the steps of the Lincoln Memorial as part of an August 28, 1963 civil rights march on Washington D.C. The speech supported desegregation and helped spur passage of the 1964 Civil Rights Act.
3.2 “I Have a Dream” Program 57
Apago PDF Enhancer
Since this chapter is your first real taste of Java, we’ll stick to the basics. We’ll present Java syntax that’s needed for simple sequential-execution programs. A sequential-execution program is one in which all the program’s statements are executed in the order in which they are written. As we write such pro- grams, we’ll show you output, assignment, and input statements. In addition, we’ll describe data types and arithmetic operations. Toward the end of the chapter, we’ll present a few slightly more advanced topics—type casting and string methods—that will add important functionality without adding much com- plexity. Let us begin the Java journey.
Start every program with this code’s structure.