Lesson 7: Java Program Structure: A Beginner’s Guide

We have a piece of code that we saw earlier in Lesson 2. Let’s use it as an example to explore the structure of a simple Java program. This will help us understand how different parts of a program work together and what their roles are.

import java.util.Scanner;

public class SimpleCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Input the first number
        System.out.println("Enter the first number (x):");
        double x = scanner.nextDouble();

        // Input the second number
        System.out.println("Enter the second number (y):");
        double y = scanner.nextDouble();

        // Input the third number
        System.out.println("Enter the third number (z):");
        double z = scanner.nextDouble();

        // Perform the calculation
        if (z == 0) {
            System.out.println("Error: Division by zero is not allowed.");
            return;
        }
        double result = x + y / z;

        // Output the result
        System.out.println("Result: " + result);
    }
}
  1. import java.util.Scanner;
    This line “imports” a tool called Scanner into our program. This tool was created by Java developers to allow programs to receive input from the keyboard.
  2. public class SimpleCalculator {
    This is the start of the program, which is called SimpleCalculator. A “class” is like a container where all the program code is placed.
  3. public static void main(String[] args) {
    This is the main method, or the “entry point” of the program. When you run the program, it starts executing commands from here.
  4. Scanner scanner = new Scanner(System.in);
    Here, we create a scanner object, which will “listen” to what the user types on the keyboard.
  5. System.out.println("Enter the first number (x):");
    This is a command to display text on the screen. It tells the user to input the first number.
  6. double x = scanner.nextDouble();
    We create a variable x to store the first number the user types. The type double means it can hold decimal numbers (e.g., 5.5).
  7. double y = scanner.nextDouble(); and double z = scanner.nextDouble();
    These lines do the same thing as the previous one, but they store the second and third numbers the user types into variables y and z.
  8. if (z == 0) { ... }
    This is called a conditional statement. It allows the program to make decisions based on certain rules:
    • Here, the program checks if z is equal to 0.
    • If the condition is true (z == 0), the program will:
      1. Show an error message: Error: Division by zero is not allowed.
      2. Stop running (return).
    Note: We will talk more about conditional statements later in the course. For now, just understand that this part of the code sets rules for what the program should do in specific situations.
  9. double result = x + y / z;
    This line calculates the result of x plus y divided by z and stores it in a variable called result.
  10. System.out.println("Result: " + result);
    This displays the result of the calculation on the screen.
  11. }
    Every opening { must have a closing }. This marks where different parts of the program begin and end.

Summary

  • Importing Scanner lets the program read user input.
  • The class SimpleCalculator is a container for the program code.
  • The main method is where the program starts.
  • Variables like x, y, z, and result are used to temporarily store numbers.
  • The if statement (conditional operator) checks if z is zero and stops the program if needed.
  • The program calculates and shows the result unless there is an error (like dividing by zero).

In short, this program asks the user for three numbers, performs a calculation, and displays the result. If dividing by zero is attempted, it warns the user and stops.

Task:

Open an Online IDE.
Modify the basic code so that:
Your class is named Calculator.
The program reads two integer (int) values x and y from the console.
It calculates the sum of these variables and stores the result in a variable called sum.
Finally, it prints the result to the console with a message, for example:
“The sum of variables x and y: 6”.

Leave a Reply