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);
}
}
import java.util.Scanner;
This line “imports” a tool calledScanner
into our program. This tool was created by Java developers to allow programs to receive input from the keyboard.public class SimpleCalculator {
This is the start of the program, which is calledSimpleCalculator
. A “class” is like a container where all the program code is placed.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.Scanner scanner = new Scanner(System.in);
Here, we create ascanner
object, which will “listen” to what the user types on the keyboard.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.double x = scanner.nextDouble();
We create a variablex
to store the first number the user types. The typedouble
means it can hold decimal numbers (e.g., 5.5).double y = scanner.nextDouble();
anddouble 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 variablesy
andz
.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:- Show an error message:
Error: Division by zero is not allowed.
- Stop running (
return
).
- Show an error message:
- Here, the program checks if
double result = x + y / z;
This line calculates the result ofx
plusy
divided byz
and stores it in a variable calledresult
.System.out.println("Result: " + result);
This displays the result of the calculation on the screen.}
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
, andresult
are used to temporarily store numbers. - The
if
statement (conditional operator) checks ifz
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”.