Writing A Simple Programs
To begin, lets look at a simple program that computes the areas 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 the program involves designing simple program and data structures, as well as translating algorithms into programming code. An algrorithm 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 befrore writting it in a programming language. The algorithm for this program can be described as follows:
Writing the program involves designing simple program and data structures, as well as translating algorithms into programming code. An algrorithm 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 befrore writting 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.
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 intoductory 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 an 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 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 programers, 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 keyboard class is followed by the class name. Assume that you have chosen ComputeArea as the the class name. The outline of program would look like this:
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 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 programers, 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 keyboard class is followed by the class name. Assume that you have chosen ComputeArea as the the class name. The outline of 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 {
// 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
}
}
// Step 1: Read in radius
// Step 2: Compute area
// Step 3: Display the area
}
}
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 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 computer 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 expressed as follows:
public class ComputeArea {
public static void main (String[] args {
double radius;
double area;
// Step 1: Radius in radius
// Step 2: Compute area
// Step 3: Display the area
}
}
The program declares radius and radius as variables. The reserved word double indicates that radius and area 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.
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.printin method
The complete program is shown below
- public class ComputeArea {
- public static void main(String[] args {
- double radius; radius [no value]
- double area; area [no value]
- radius = ; radius [20]
- area - radius * radius * 3.14159; area [1256.636]
- System.out,printin(
- radius + +area)
- }
- }
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 concatenating operator. The string concatenating operator connects two strings if two operands are strings. If one of the operands is a none-string (e.g., a number), the non-string value is converted into a string and concatenated with the other string. So the plus sisgns (+) in lines 14-15 concatenate strings into a longer string, which is then displayed in the output.
More on strings and strings concatenation will be discussed in my next post
0 comments:
Post a Comment