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

Find Circle’s radius Compute using: area = radius * radius * π 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: