How to Create Basic Calculator using Java

This tutorial will teach you how to create a basic calculator using if statement in Java. This calculator can calculate using the four operations of mathematics.

I am using “if Statement” to identify what operation should use in calculating two numbers. And also a “Boolean” condition if statement, it means that only two results of the condition should display if it is the condition is true or false.

I am also using a “Scanner” Function so that the user can input the values when the program is running. Please follow all the steps to complete this tutorial.

Basic Calculator Using If Statements Steps

  1. Create a new project.

2. Go to your source view and insert the code below above your class.

[java]import java.util.Scanner;[/java]

3. Inside your main method, declare five variables. Your code should look like below.

[java]Scanner input = new Scanner(System.in);
int num1=0,num2=0,total=0;
String op="";[/java]

Input – scanner variable

Num1 – handle the first value

Num2 – handle the second value

Total – display the result

Op – handle the input operation

4. Insert the statement below after step 3. This statement will ask the user input of first value, the second value, and operation.

[java]System.out.print("Enter first value: ");
num1 = input.nextInt();
System.out.print("Enter second value: ");
num2 = input.nextInt();
System.out.print("Enter operation: ");
op = input.next();[/java]

5. Insert the statement below after step 4. The following statement will execute the algorithm and display the result.

[java]if (op.equals("a")){
total = num1 + num2;
System.out.println("The sum is " + total);
}else if(op.equals("s")){
total = num1 - num2;
System.out.println("The difference is " + total);
}else if(op.equals("m")){
total = num1 * num2;
System.out.println("The product is " + total);
}else if(op.equals("d")){
total = num1 / num2;
System.out.println("The quotient is " + total);
}else{
System.out.println("Wrong operation!");
}[/java]

6. Run your program and the result should look like the image below.

7. Complete Source Code.

[java]import java.util.Scanner;
public class BasicCalculator {
public static void main(String args[]){
//declaring variables
Scanner input = new Scanner(System.in);
int num1=0,num2=0,total=0;
String op="";
//initializing the variables
System.out.print("Enter first value: ");
num1 = input.nextInt();
System.out.print("Enter second value: ");
num2 = input.nextInt();
System.out.print("Enter operation: ");
op = input.next();
//runing the algo and display result
if (op.equals("a")){
total = num1 + num2;
System.out.println("The sum is " + total);
}else if(op.equals("s")){
total = num1 - num2;
System.out.println("The difference is " + total);
}else if(op.equals("m")){
total = num1 * num2;
System.out.println("The product is " + total);
}else if(op.equals("d")){
total = num1 / num2;
System.out.println("The quotient is " + total);
}else{
System.out.println("Wrong operation!");
}
}
}[/java]

REMEMBER:
Operation “a” for addition, “s” for subtraction, “m” for multiplication, “d” for division. If you input operation in another format, the program will display message “Wrong operation”.

About How to Create Basic Calculator In Java

<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><strong>Project Name:</strong></td>
<td>How to Create Basic Calculator</td>
</tr>
<tr>
<td><strong>Language/s Used:</strong></td>
<td>JAVA</td>
</tr>
<tr>
<td><strong>Database:</strong></td>
<td>None</td>
</tr>
<tr>
<td><strong>Type:</strong></td>
<td>Desktop Application</td>
</tr>
<tr>
<td><strong>Developer:</strong></td>
<td>IT SOURCECODE</td>
</tr>
<tr>
<td><strong>Updates:</strong></td>
<td>0</td>
</tr>
</tbody>
</table><figcaption><em><strong>How to Create Basic Calculator In Java</strong>- Project Information</em></figcaption></figure>

If you have any comments or suggestions about on How to Create Basic Calculator using Java, feel free to leave your comment below, use the contact page of this website or use my contact information.

Email: [email protected]  |  Cell. No.: 09468362375

Readers might check this:

Sum Two Integers Value using Java

Frequently Asked Questions

What does this Java code snippet demonstrate?

Focused Java code pattern: how to compute, format, validate, or interact with MySQL/file/network with minimum code. Drop-in pattern you can adapt for your own capstone module.

What Java JDK and MySQL versions does this project require?

Most projects in this batch use Java JDK 8 or 11 with MySQL 5.7+ or MariaDB 10+. To run: install JDK (Adoptium / Oracle), install MySQL Server + MySQL Workbench, install NetBeans IDE (15+ supports modern JDK), open the project (.zip extracted folder), right-click + Open Project, add MySQL JDBC driver to Project Libraries, run.

How do I set up the database for this Java project?

Open MySQL Workbench (or phpMyAdmin if you have XAMPP), create a new empty database with the name specified in the project. Import the included .sql file via Server, Data Import in Workbench (or Import tab in phpMyAdmin). Update the connection class (usually DBConnection.java or DatabaseConnection.java) with your MySQL host, port, username, password, and database name.

Can I use this Java project for a BSIT capstone or thesis?

Yes, Java is one of the most accepted languages by Philippine BSIT panels. Extend it: add role-based access (admin/staff/customer login redirect), JasperReports printable reports, dashboards with JFreeChart, audit log, multi-branch support. Pair with Chapter 1-5 documentation matching your panel’s rubric.

Why am I getting ‘ClassNotFoundException: com.mysql.jdbc.Driver’ or ‘No suitable driver’?

Three common Java JDBC issues: (1) MySQL JDBC driver JAR not added to project Libraries. Right-click Project, Properties, Libraries, Add JAR/Folder, select mysql-connector-java-X.X.X.jar. (2) Wrong driver class name. Modern (8.0+) uses com.mysql.cj.jdbc.Driver, legacy (5.x) uses com.mysql.jdbc.Driver. (3) Connection URL missing serverTimezone parameter, add ?serverTimezone=UTC to the URL.

Where can I find more Java projects with source code?

Browse the Java Projects hub for the full library (120+ Java desktop systems). For modern Java web alternatives consider Spring Boot. For other desktop stacks see VB.NET Projects or C# Projects. For BSIT capstone idea lists see 150 Best Capstone Project Ideas.

Leave a Comment