In this post we will develop Unit Converter using some simple java concepts.
Here is the sample code for Main.java :
Step 4 : Let's Run the project and check the Output :)
✅ Problem Statement :
Mr. Groff works in a shop where he required to convert weights from one unit into other. So he need unit converter to make his task easy.
This is how we need to convert data from one unit to another .
✅ Concepts Covered in this Post :
- Use Switch Statement
- Write User Defined Functions
- Calling Function
📑 Let's Code :
Step 1 : Create Project in eclipse as we did in previous projects.
Create project (UnitConverter) ➝ Create package (com.codingbuddy.unit_converter) ➝ Create 2 java classes (UnitConverter.java and Main.java
• UnitConverter.java class will contain actual methods to convert value from one unit to another unit.
• Main.java class will contain main () method to test functionalities.
• Main.java class will contain main () method to test functionalities.
Step 2 : Now we will complete UnitConverter.java class -
This Class Contains -
- Data Members
- Getters and Setters
- Method for unit conversion
Here is the sample code for UnitConverter.java :
Step 3 : Now we will complete Main.java class -
This class contains -
- main () method
- Object of Scanner Class to read input from user
- Object of UnitConverter.java to access functionalities of that class
- Switch Statement to select particular operation and call specific method
- While Loop
- String.format() to set precision value to print after decimal point.
Main.java 1.1 |
Main.java 1.2 |
Step 4 : Let's Run the project and check the Output :)
Here is the Sample Output -
Here we are done with our new project :)
Find source code of project👉 here
Hope you like this post 😉
You can reach us by entering your details in Contact Us box in the side panel💬
All the Best 👍👍👍👍
Happy Learning 😇😇😇