Writing a program involves designing algorithms and translating algorithms into programming code.
An algorithm describes how a problem is solved in terms of the actions to be executed and the order of their execution. Algorithms can help the programmer plan a program before writing it in a programming language.
Generally, a program has three parts; input, process and output. The input is where we describe the required data. The process part will process the data to get the output. The output will then be displayed in output part. So, the structure should be looks like the following:
Write a program to compute the area of a circle.
In this exercise, we will look at a simple problem for computing the area of a circle. The algorithm for this program is as follows:
1. Input: Read in the radius.
2. Process: Compute the area using the following formula:
area = radius x radius x PI
3. Output: Display the area.
We then need to translates the above algorithm into Java program. We can start with basic structure of Java program as the following. Let say we named our program "AreaCircle1".
public class AreaCircle1 {
public static void main(String[] args) {
// Input: Read in radius
// Process: Compute area
// Output: Display the area
}
}
In this problem, we know that we want to output/display the area of a circle.
public class AreaCircle1 {
public static void main(String[] args) {
// Input: Read in radius
// Process: Compute area
System.out.println(area);
}
}
To get the value for an area, we need to use a formula.
The formula for area is radius*radius*PI.
So, we modified our program again as the following, which is we add the formula for area.
public class ComputeArea {
public static void main(String[] args) {
// Input: Read in radius
area = radius * radius * PI;
System.out.println(area);
}
In the above program, variable radius and PI have no value. Thus we cannot get the answer (value for the area).
Let say the value for radius is 3.0 and we know that the value for PI is 3.14. So, if we calculate manually, we will get the value for area is 28.26.
area = radius * radius * PI
= 3.0 * 3.0 * 3.14 = 28.26
However, at this stage, when we compile this program, we will get an error because the variable area is not declared yet. The variables need to be declared before it can be used.
// Input: Read in radius
double area; // declare the variable area
area = radius * radius * PI;
System.out.println(area);
or, it can be like this;
// Input: Read in radius
double area = radius * radius * PI;
System.out.println(area);
Why do we declare variable area as a type of double? It is because, the value of PI is a floating number, then we need to declare variable area as a double data type to get the answer as a floating number. And the data type for radius is also double.
Run the following program.
public class AreaCircle1 {
public static void main(String[] args) {
int radius = 3;
double area = radius * radius * 3.14;
System.out.println("Area = " + area);
}
}
The output will be:
Area = 28.26
Let say, we declare radius as integer as the following.
int radius = 3;
If we run the program, we will get the answer as the following.
Area = 28.26
but, if we declare area as an integer, we will get an error, because the radius is multiply with a double value 3.14.
int area = radius * radius * 3.14;// error!
Compile the above program and see the output.