Welcome to the blog !!
There is no better way to learn any programming language than building projects. We must able to apply whatever we learn theoretically. So let's learn java by building projects .
Today we will learn to build a Simple Calculator using some simple concepts of Java.
✅ Problem Statement:
Build a system which will perform some basic mathematical operations like addition, subtraction, multiplication, division, etc.
✅ Concepts of Java covered in this Project:
- Take Input and Display Output.
- Write methods to perform operations.
- Write Getters and Setters.
- Create object of class.
- Call methods using object.
📑 Let's Code :)
*Note : I will recommend you to pre-install Eclipse IDE on your machine.
We will create Two Classes: One class for all the functionality , methods and Other class will contain Main method for accessing all the methods.
Follow the Steps ....
Step 1: Open Eclipse IDE and Right click on File tab to create new Java Project.
Step 2: Once you click on Java Project , it will open a new prompt box . Give some name to your project and then click on Finish Button.
Step 3: Now , it will create two folders under your project name (here I give project name as java_project) JRE and SRC. Now right click on SRC → New → Package .
*All classes which we are going to create for our project must be in the same package so we have created package under project.
Give name to your package and hit Finish Button. This will create package for your project.
Step 4: Again right click on package you have created to create classes. Refer the below screenshot to create classes . Remember to repeat the same process twice as we need to create 2 classes for this project.
Note* :
- In Java , Package Names are written in lower case to avoid conflict with class names and interface names for ex. calculator
- Class Name must be start with Upper case letter for ex. Calculator, Main etc.
Step 5: Now we will complete Calculator class.
Calculator class will contain -
- Declaration of private members / variables.
- Getters and Setters for accessing those members.
- Public Methods to perform addition, subtraction, multiplication, division.
Calculator.java 1.1 |
Calculator.java 1.2 |
🔴 Concept to Learn :
Que- Why do we write getters and setters ?
⇒ As we have declare variables as Private we can not access those variables outside the class. As we declare all the methods as Public we can access those methods outside the class. So we declare getters and setters using which we can access private variable outside the class.
# Getters are used to retrieve the value of private member variable.
# Setters are used to update or set the value to private member variable.
Step 6: Now lets complete Main class which is having main method.
Main class will contain -
- Take Input of 2 numbers from users
- Create object of Calculator class
- Call methods written in Calculator class using object.
- Display output
Main.java 1.1 |
Main.java 1.2 |
🔴 Concept to Learn :
Que- How to take input in Java ?
⇒ Scanner Class from java.util package is used to take input from user (refer Main.java screenshot1).
Scanner class has many method to take
Here we want to take integer input so we call nextInt() method using object of Scanner class.
Refer this to know more - https://www.w3schools.com/java/java_user_input.asp
Que- How to print anything on the console ?
⇒ In Java, System.out.println() is a statement which prints the argument passed to it.
Que- What is Object in Java ? How to create an object ?
⇒ Object is an instance of class. One can create many objects of a class.
Object is created using NEW keyword. for ex: Calculator cal = new Calculator();
Here cal is an object.
- We can access variable or methods using object and dot ( . ) operator .
Ex. cal.addition(no1,no2);
Step 7: Save and Run the project .
To run the project in Eclipse press ctrl+ F11 .
It will open new console. Give the required input and check the displayed output.
Here is the snapshot of the output of above code:
We are done with our First Project in Java 😇😇 .
You can refer source code of project here.
Happy Learning 😀😀
All The Best 👍👍👍.