Java Programming

Sequence Statements

Sequence Statements are statements that are executed in a sequential manner, one after another, in the order they appear in the code.

In programming, sequence statements are the most basic type of statements and are used to perform a series of operations or tasks. Each statement is executed one at a time, and the flow of execution moves from the first statement to the next until all statements have been executed.

Sequence statements are the simplest type of control structure in programming where each line of code is executed one after the other.

Think of it like a recipe: you can't eat the cake before you bake it, and you can't bake it before you mix the ingredients.

For example, the steps to calculate the area of a rectangle are:

  1. Get the length of the rectangle.
  2. Get the width of the rectangle.
  3. Calculate the area by multiplying the length and width.

When building such an interactive program to calculate the area, you must follow the following steps:

  1. Create a space on the memory to store the length.
  2. Create a space on the memory to store the width.
  3. Display information to the user to enter the length
  4. Get the length from the user and store it in the memory space created for length.
  5. Display information to the user to enter the width
  6. Get the width from the user and store it in the memory space created for width.
  7. Calculate the area by multiplying the length and width, and display the result to the user.

As a programmer, you must know the syntax to use to:

  1. Create space in memory to store variables (Variable Declaration)
  2. Display information to the user (Output)
  3. Get input from the user (Input)
  4. Perform calculations (Processing)

The syntax to accomplish these varies from one programming language to another.

We've already learned about variable declaration and initialization from our previous lessons.

Output syntax in Java

The syntax for displaying output in Java is the System.out.println() or System.out.print() method.

The text to display is specified as a string and written between double quotes (")

For example, to display the message "Hello, World!" in Java, you can use the following code:

System.out.println("Hello, World!");

To display "WELCOME TO JAVA PROGRAMMING" in Java, you can use the following code:

System.out.println("WELCOME TO JAVA PROGRAMMING");

In the above program to calculate the area of a rectangle, to display "Enter length:", you can use the following code:

System.out.print("Enter length: ");

To display "Enter width:", you can use the following code:

System.out.print("Enter width: ");

We now have the necessary syntax knowledge to write our program to calculate the area of a rectangle in Java.

Since the length and width could be decimal values, it is appropriate to store them as floats or doubles.

To handle user input in Java, we use the Scanner class from the java.util package.

Methods to call on scanner object and the data type

Data Type Scanner Method Description & Example Usage
int scanner.nextInt() Reads an integer value (e.g., age, count).
int age = scanner.nextInt();
float scanner.nextFloat() Reads a single-precision decimal number.
float length = scanner.nextFloat();
double scanner.nextDouble() Reads a double-precision decimal number.
double price = scanner.nextDouble();
boolean scanner.nextBoolean() Reads a boolean value (true or false).
boolean isActive = scanner.nextBoolean();
String (Single Word) scanner.next() Reads the next token/word up to the first space.
String firstName = scanner.next();
String (Full Line) scanner.nextLine() Reads an entire line of text until a newline character.
String fullName = scanner.nextLine();
long scanner.nextLong() Reads a large integer value.
long population = scanner.nextLong();
short scanner.nextShort() Reads a short integer value.
short smallNum = scanner.nextShort();
byte scanner.nextByte() Reads a byte value.
byte b = scanner.nextByte();

Java Program To Calculate the Area of a Rectangle

import java.util.Scanner;

public class RectangleArea {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        float length, width; /* Declare variables to store length and width */
        
        System.out.print("Enter length: "); /* Prompt the user to enter the length */
        length = scanner.nextFloat(); /* Read the length from the user and store it in the variable 'length' */
        
        System.out.print("Enter width: "); /* Prompt the user to enter the width */
        width = scanner.nextFloat(); /* Read the width from the user and store it in the variable 'width' */
        
        System.out.println("The area of a rectangle of length " + length + " cm and width " + width + " cm is " + (length * width) + " cm square");
        
        scanner.close();
    }
}

Notes:

  1. Java uses the + operator to concatenate strings and variables in output statements.
  2. Input in Java requires importing java.util.Scanner, creating a Scanner object, and calling methods like nextFloat().

Note: The executable code should be placed inside the main method.

The explanatory statements are called comments which are ignored by the compiler.

You can run the code in your IDE or use the Java online compiler below to test the program:

https://www.programiz.com/java-programming/online-compiler/

Programming Tasks

Write a program in Java to calculate the circumference and area of a circle.

Notes:

  1. The circumference of a circle is given by the formula: C = 2 * π * r
  2. The area of a circle is given by the formula: A = π * r2
  3. r2 = r * r (A square is a number multiplied by itself) in Java
  4. π = 3.142
  5. r is the radius of the circle, which can be obtained from the user using a Scanner.
  6. A constant is defined in Java using the final keyword (e.g., final float PI = 3.142;).

The sequence of steps to solve this problem is as follows:

  1. Define a constant for PI (π) using final float PI = 3.142;.
  2. Get the radius of the circle from the user.
  3. Calculate the circumference using the formula C = 2 * π * r.
  4. Calculate the area using the formula A = π * r2.
  5. Display the calculated circumference and area to the user.

Java Program To Calculate Circumference and Area of a Circle

import java.util.Scanner;

public class CircleCalculator {
    // Define a constant for PI
    static final float PI = 3.142;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        float radius, circumference, area; /* Declare variables to store radius, circumference, and area */
        
        System.out.print("Enter radius: "); /* Prompt the user to enter the radius */
        radius = scanner.nextFloat(); /* Read the radius from the user and store it in the variable 'radius' */
        
        circumference = 2 * PI * radius; /* Calculate the circumference using the formula C = 2 * π * r */
        area = PI * radius * radius; /* Calculate the area using the formula A = π * r^2 */
        
        System.out.println("The circumference of a circle with radius " + radius + " cm is " + circumference + " cm");
        System.out.println("The area of a circle with radius " + radius + " cm is " + area + " cm square");
        
        scanner.close();
    }
}

Alternatively, we could declare the constant locally inside the main method:

import java.util.Scanner;

public class CircleCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        final float PI = 3.142; /* Local constant for PI */
        
        float radius, circumference, area; /* Declare variables to store radius, circumference, and area */
        System.out.print("Enter radius: "); /* Prompt the user to enter the radius */
        radius = scanner.nextFloat(); /* Read the radius from the user and store it in the variable 'radius' */
        
        circumference = 2 * PI * radius; /* Calculate the circumference */
        area = PI * radius * radius; /* Calculate the area */
        
        System.out.println("The circumference of a circle with radius " + radius + " cm is " + circumference + " cm");
        System.out.println("The area of a circle with radius " + radius + " cm is " + area + " cm square");
        
        scanner.close();
    }
}

In the above Java program, the circumference and area are stored before being displayed to the user.

In case you decide not to store them in variables but calculate directly inside the print statement, the code will be:

import java.util.Scanner;

public class CircleCalculatorDirect {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        final float PI = 3.142;
        
        float radius; /* Declare variable to store radius */
        System.out.print("Enter radius: "); /* Prompt the user to enter the radius */
        radius = scanner.nextFloat(); /* Read the radius from the user and store it in the variable 'radius' */
        
        System.out.println("The circumference of a circle with radius " + radius + " cm is " + (2 * PI * radius) + " cm");
        System.out.println("The area of a circle with radius " + radius + " cm is " + (PI * radius * radius) + " cm square");
        
        scanner.close();
    }
}