2ND QUARTER - CHAPTER II
(Elementary Programming)
2.1 Introduction
The focus of this chapter is on learning elementary programming techniques to solve problems.
In Chapter 1 you learned how to create, compile, and run very basic Java programs. Now you will learn how to solve problems by writing programs. Through these problems, you will learn elementary programming using primitive data types, variables, constants, operators, expressions, and input and output.
Suppose, for example, that you need to take out a student loan. Given the loan amount, loan term, and annual interest rate, can you write a program to compute the monthly payment and total payment? This chapter shows you how to write programs like this. Along the way, you learn the basic steps that go into analyzing a problem, designing a solution, and implementing the solution by creating a program.
2.2 Writing a simple program
Writing a program involves designing a strategy for solving the problem and then using a programming language to implement that strategy.
Let’s first consider the simple problem of computing the area of a circle. How do we write a program for solving this problem? Writing a program involves designing algorithms and translating algorithms into programming instructions, or code.
An algorithm describes how a problem is solved by listing the actions that need to be taken and the order of their execution. Algorithms can help the programmer plan a program before writing it in a programming language. Algorithms can be described in natural languages or in pseudocode (natural language mixed with some programming code). The algorithm for calculating the area of a circle can be described as follows in figure 1.1
1. public class ComputeArea {
2. public static void main(String[] args) {
3. // Step 1: Read in radius
4. // Step 2: Compute area
5. // Step 3: Display the area
6. }
7. }
Note:
Two important issues that the program needs to read the radius entered by the user from the keyboard.
1. Reading the radius
2. 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. A variable represents a value stored in the computer’s memory. Rather than using x and y as variable names, choose descriptive names: in this case, radius for radius, and area for area. To let the compiler know what radius and area are, specify their data types. That is the kind of data stored in a variable, whether integer, real number, or something else. This is known as declaring variables. Java provides simple data types for representing integers, real numbers, characters, and Boolean types. These types are known as primitive data types or fundamental types.
Real numbers (i.e., numbers with a decimal point) are represented using a method known as floating-point in computers. So, the real numbers are also called floating-point numbers. In Java, you can use the keyword double to declare a floating-point variable. Declare radius and area as double. The program can be expanded as follows:
Figure 1.2
1. public class ComputeArea {
2. public static void main(String[] args) {
3. double radius;
4. double area;
5. // Step 1: Read in radius
6. // Step 2: Compute area
7. // Step 3: Display the area
}
}
The program declares radius and area as variables. The reserved word double indicates that radius and area are floating-point values stored in the computer.
1st step is to prompt the user to designate the circle’s radius.
Note: You will soon learn how to prompt the user for information. For now, to learn how variables work, you can assign a fixed value to radius in the program as you write the code; later, you’ll modify the program to prompt the user for this value.
2nd step is to compute area by assigning the result of the expression radius * radius * 3.14159 to area.
3rd step., the program will display the value of area on the console by using the System.out.println method.
Figure 2.1 shows the complete program, and a sample run of the program is shown in
1. public class ComputeArea {
2. public static void main(String[] args) {
3. double radius; // Declare radius
4. double area; // Declare area
5.
6. // Assign a radius
7. radius = 20 ; // radius is now 20
8.
9. // Compute the area
10. area = radius * radius * 3.14159 ;
11.
12. // Display results
13. System.out.println("The area for the circle
14. of radius " + radius + " is " + area) ;
15. }
16. }
No comments:
Post a Comment