Mastering Java Basics: Unveiling the Power of a Simple Calculator | Java Education Series

Published on Jan 11, 2024

Today, we dive into a Java program that moonlights as a basic calculator. It is time to dissect the code, line by line.

We will begin by declaring a class named Calculator. This is where all the magic happens.

The spotlight is on the main method. It is the entry point for our program, where the show begins.

We are setting the stage with some Character operators for our operation, and number1, number2, and result to store numeric values.

As we will receive Input from the user in this program, let’s import the Scanner class from the Java—util package. The Scanner class is like our trusty sidekick, helping us grab user input.

Now, meet our co-star, the Scanner named Input. It is ready to take in user input from the console.

Cue the audience interaction! Users get to pick an operator. The first character of their choice is stored in the operator.

It is the number of times! Users input two numbers, and we store them in numbers 1 and 2.

The plot thickens with a switch statement. It is like our script, directing the flow based on the chosen operator.

We start with a switch statement that evaluates the value of the operator variable. The switch statement allows us to perform different actions based on the value of the operator.

If the operator is ‘plus’, we will add number1 and number2, store the result in the result variable, and then print a message showing the addition operation and the outcome.

If the operator is ‘minus’, there is a mistake in the code. It subtracts the number 1 from itself instead of removing the number 2 from the number 1. The corrected line should result = number1 – number2. This subtraction result is then printed.

If the operator is ‘multiply’, it multiplies number 1 and number 2, stores the result in the result variable, and then prints a message showing the multiplication operation and the outcome.

If the operator is ‘Division’, it divides number 1 by number 2, stores the result in the result variable, and then prints a message showing the division operation and the outcome.

The default case is triggered if the entered operator does not match any cases (‘plus’, ‘minus’ ‘multiply’, ‘Division’). It prints a message informing the user to enter a valid operator.

The curtain falls. We gracefully exited by closing the Scanner and wrapping up our performance.

And that’s a wrap! Run the code, play with it, and enjoy the interactive experience of our little Calculator. Happy coding!

import java.util.Scanner;

class Calculator{
public static void main(String[] args){
char operator;
Double number1, number2, result;

Scanner input = new Scanner(System.in);

System.out.println("Choose an operator: +, -, *, or /");
operator = input.next().charAt(0);

System.out.println("Enter first number");
number1 = input.nextDouble();

System.out.println("Enter second number");
number2 = input.nextDouble();

switch (operator){
case '+':
result = number1 + number2;
System.out.println(number1 + "+" + number2 + "=" + result);
break;
case '-':
result = number1 - number1;
System.out.println(number1 + "-" + number2 + "=" + result);
break;
case '*':
result = number1 * number2;
System.out.println(number1 + "*" + number1 + "=" + result);
break;
case '/':
result = number1 / number2;
System.out.println(number1 + "/" + number2 + "=" + result);
break;
default:
System.out.println("Please Enter Valid Operator");
break;
}
input.close();
}
}

Our Latest Blog

Fundamentals of Scala Programming

Mastering the Fundamentals of Scala Programming: A Complete Guide to Functional Programming

Introduction to Scala Programming Scala programming has gained immense popularity in recent years thanks to...
Read More
Java Programming

Java Basics for Beginners: A Step-by-Step Guide to Mastering Java Programming

Introduction to Java programming Java, a versatile and extensively utilised programming language, is popular among...
Read More
Entity Relationship Diagrams (ERD)

Entity Relationship Diagrams (ERD): A Comprehensive Guide

Introduction to Entity Relationship Diagrams (ERD) Entity Relationship Diagrams (ERD) are powerful tools used in...
Read More