2.3. Identifiers

 
[Page 28 ( continued )]

2.2. Writing Simple Programs

To begin, let's look at a simple program that computes the area of a circle. The program reads in the radius of a circle and displays its area. The program will use variables to store the radius and the area, and will use an expression to compute the area.

Writing this program involves designing simple program and data structures, as well as translating algorithms into programming code. An algorithm describes how a problem is solved in terms of the actions to be executed, and it specifies the order in which the actions should be executed. Algorithms can help the programmer plan a program before writing it in a programming language. The algorithm for this program can be described as follows :

  1. Read in the radius.

  2. Compute the area using the following formula:

    area = radius x radius x p

  3. Display the area.

Many of the problems you will meet when taking an introductory course in programming using this text can be described with simple, straightforward algorithms. As your education progresses, and you take courses on data structures or on algorithm design and analysis, you will encounter complex problems that require sophisticated solutions. You will need to design accurate, efficient algorithms with appropriate data structures in order to solve such problems.

Data structures involve data representation and manipulation. Java provides data types for representing integers, floating-point numbers (i.e., numbers with a decimal point), characters , and Boolean types. These types are known as primitive data types . Java also supports array and string types as objects. Some advanced data structures, such as stacks, sets, and lists, have built-in implementation in Java.

To novice programmers, coding is a daunting task. When you code , you translate an algorithm into a programming language understood by the computer. You already know that every Java program begins with a class declaration in which the keyword class is followed by the class name . Assume that you have chosen ComputeArea as the class name. The outline of the program would look like this:

   public class   ComputeArea {  // Data and methods to be given later   }  

As you know, every application must have a main method where program execution begins. So the program is expanded as follows:

   public class   ComputeArea {   public static void   main(String[] args) {  // Step 1: Read in radius   // Step 2: Compute area   // Step 3: Display the area  } } 


[Page 29]

The program needs to read the radius entered by the user from the keyboard. This raises two important issues:

  • Reading the radius.

  • Storing the radius in the program.

Let's address the second issue first. In order to store the radius, the program needs to declare a symbol called a variable that will represent the radius. Variables are used to store data and computational results in the program.

Rather than using x and y , choose descriptive names : in this case, radius for radius, and area for area. Specify their data types to let the compiler know what radius and area are, indicating whether they are integer, float, or something else. Declare radius and area as double-precision floating-point numbers. The program can be expanded as follows:

   public class   ComputeArea {   public static void   main(String[] args) {   double   radius;   double   area;  // Step 1: Read in radius   // Step 2: Compute area   // Step 3: Display the area  } } 

The program declares radius and area as variables. The reserved word double indicates that radius and area are double-precision floating-point values stored in the computer.

The first step is to read in radius . Reading a number is not a simple matter. For the time being, let us assign a fixed number to radius in the program. In §2.11, "Getting Input from Input Dialogs," and §2.13, "Getting Input from the Console," you will learn how to obtain a numeric value from an input dialog and from the console.

The second step is to compute area by assigning the expression radius * radius * 3.14159 to area .

In the final step, print area on the console by using the System.out.println method.

The complete program is shown in Listing 2.1. A sample run of the program is shown in Figure 2.1.

Listing 2.1. ComputeArea.java


[Page 30]
Figure 2.1. The program displays the area of a circle.

Variables such as radius and area correspond to memory locations. Every variable has a name, a type, a size , and a value. Line 4 declares that radius can store a double value. The value is not defined until you assign a value. Line 8 assigns 20 into radius . Similarly, line 5 declares variable area , and line 11 assigns a value into area .

The plus sign ( + ) has two meanings: one for addition and the other for concatenating strings . The plus sign ( + ) in lines 14 “15 is called a string concatenation operator . The string concatenation operator connects two strings if two operands are strings. If one of the operands is a non-string (e.g., a number), the non-string value is converted into a string and concatenated with the other string. So the plus signs ( + ) in lines 14 “15 concatenate strings into a longer string, which is then displayed in the output. More on strings and string concatenation will be discussed in §2.10, "The String Type."

Caution

A string constant cannot cross lines in the source code. Thus the following statement would result in a compilation error:

 System.out.println(   "Introduction to Java Programming,     by Y. Daniel Liang"   x); 

To fix the error, break the string into substrings, and use the concatenation operator (x) to combine them:

 System.out.println(   "Introduction to Java Programming, " +     "by Y. Daniel Liang"   ); 


Tip

This example consists of three steps. It is a good approach to develop and test these steps incrementally by adding one step at a time. You should apply this approach to all the programs, although the problem-solving steps are not explicitly stated in many programs in this book.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net