celestial snow bridge with blue cloudy nebula in the sky

1.3 Java | Developing a Java Program from Algorithm to Code

Table of Contents

Problem to Algorithm

Problem: How do you compute the area of a circle?

Algorithm: Solving a problem always requires steps. Outlining those steps is called an Algorithm. … Algorithm describes how a problem is solved by listing the actions that need to be taken and the order of their execution.

Algorithm for calculating the area of circle

  1. Find Circle’s radius
  2. Compute using: area = radius * radius * π
  3. Display results

Takeaway: Before coding your Java applet, outline the algorithm (what the program is going to do) first.

Algorithm to Code

Skeleton

Now let’s transform the algorithm for computing the area of a circle into Java code.

Every Java program starts with the keyword class followed by class name. Let’s choose ComputeArea as the class name.

public class ComputeArea {}

For every Java program there must exist a main method where the program execution starts. To reiterate, the main method tells the Java program where to start.

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

Declaring Variables

Note that you need to tell the Java program to store the radius value after it reads it. You can store the radius value in a variable, where a variable represents a stored value in the computer’s memory. Remember to use descriptive names for variables for readability. So let’s use as variables radius for radius and area for area.

Know that you need to declare the variables so that the compiler knows that radius  and area are variables. You can do this by specifying the data types for radius and area. Data types is literally the types of data like integers, real numbers, characters, Boolean types, etc.

Let’s declare the radius  and area as real number (floating-point number) variables. You can declare a floating-point variable using the keyword double or float. Note that float & double are the same, except double is twice as precise as float.

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

Following the Steps

Following the comments that outline the program’s algorithm, the first step is to assign a value to the radius variable. You can either prompt the user for this information, but for now let’s assign a fixed value directly in the code.

The second step is to calculate the area using the expression radius * radius * 3.14, and assigning the resulting value to the area.

The third step is to display the value of area on the console using the System.out.println method.

public class ComputeArea {
    public static void main(String[] args) {
        double radius; //Making variable called radius
        double area; //Making variable called area
        
        //assign a radius
        radius = 20; //radius is now 20
        
        //calculate area of a circle
        area = radius * radius * 3.14;
        
        //display results
        System.out.println("The area for the circle of radius " + radius + " is " + area);
    }
}

Notes:

  • Notice for System.out.println that what is in quotations "" is printed out verbatim. Without quotations, the variables radius and area print the values that they contain/that they are assigned with.
  • When variables are first declared (made), they contain no value. After declaration, variables can then be assigned to carry a value.
  • The plus sign + has two meanings:
    • Numerical Addition
    • Concatenating, or combining strings. So for the display results portion of our code, the plus sign + acts as a string concatenation operator by combines the strings and the variable values into one long string.


What's Your Opinion?