DEV Community

Cover image for Writing a Simple Program in Java
Paul Ngugi
Paul Ngugi

Posted on

Writing a Simple Program in Java

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:

  1. Read in the circle’s radius.
  2. Compute the area using the following formula: area = radius * radius * p
  3. Display the result.

It’s always good practice to outline your program (or its underlying problem) in the form of an algorithm before you begin coding. When you code—that is, when you write a program—you translate an algorithm into a program. You already know that every Java program begins with a class definition in which the keyword class is followed by the class name. Assume that you have chosen ComputeArea as the class name. The outline of the program would look like this:

public class ComputeArea {
// Details to be given later
}
Enter fullscreen mode Exit fullscreen mode

As you know, every Java program must have a main method where program execution begins. The program is then 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
 }
}
Enter fullscreen mode Exit fullscreen mode

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’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:

public class ComputeArea {
 public static void main(String[] args) {
  double radius;
  double area;
  // Step 1: Read in radius
  // Step 2: Compute area
  // Step 3: Display the area
 }
}
Enter fullscreen mode Exit fullscreen mode

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.

The first step is to prompt the user to designate the circle’s radius. 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.

The second step is to compute area by assigning the result of the expression radius * radius * 3.14159 to area.

In the final step, the program will display the value of area on the console by using the System.out.println method.

Image description

The plus sign (+) has two meanings: one for addition and the other for concatenating (combining) strings. The plus sign (+) is called a string concatenation operator. It combines two strings into one. If a string is combined with a number, the number is converted into a string and concatenated with the other string. Therefore, the plus signs (+) concatenate strings into a longer string, which is then displayed in the output.

A string cannot cross lines in the source code. Thus, the following statement would result in a compile error:

System.out.println("Introduction to Java Programming,
by Y. Daniel Liang");
Enter fullscreen mode Exit fullscreen mode

To fix the error, break the string into separate substrings, and use the concatenation operator (+) to combine them:

System.out.println("Introduction to Java Programming, " +
"by Y. Daniel Liang");
Enter fullscreen mode Exit fullscreen mode

Top comments (0)