Tuesday, August 16, 2016

Muhammad Akbar

How to do Mathematical Calculations in Java-Best Education Service

mathematical calculations in java

[Tutorial 5] :

How to do Mathematical Calculations in Java

How to do Mathematical Calculations in Java ?  At times we need to do Mathematical calculations using Java. Be it your school project or some others purposes, it is always beneficial to know how java works and how you can do calculations using Java. In a nutshell , You will learn to create a program which is a Java Math Calculator. These explanations will be the basics which will help you to create a Java Calculator Program.
What is the use of a programming language if it cannot perform mathematical calculations ? 
So, In order to mathematical calculations in Java, we make use of the Arithmetic operators. As we already know and we have also seen in the previous post that the basic Arithmetic operators are + , – , * , /. These alone are used for basic calculation whereas their combination is used for advanced calculations. So , let us proceed on how to make use of them.
Before we do this , let us learn what and variable is and how to use a variable.
Variable – A variable is a named memory location that hold as particular data value of a particular data type.  For example :-  int x – 10;  In this case “int” is a data type , “x” is a variable and “10” is the value assigned to the variable.
How to initialize Variable  – In order to initialize a variable , we need to initialize it in the following form :-
int a = 10;
So , we have seen that to initialize a variable , we first write the data type , then we  give any name to the variable (Name can be of any length )  and then we  give any value to the variable by putting an equal to sign(=) between the variable and the value.
So, Now coming to the topic , in order to make calculations  , firstly we need to initialize the variables that we are going to use in our calculations. After initializing them , we have two options :-
  • We can store the result in another variable.
  • We can simply print the result.
Examples of both these type of programs are given below :-

public class calculation{    public static void main()     {                        int x = 10 ;                        int y = 20;                        int z = x + y;        }}
PS – In this program , the value of x+y will be store in the variable z and nothing will be printed in this program. In order to print the value of z , we need to give another command “System.out.println(z);”      Remember, you need not put inverted commas between the parenthesis. 

public class calculation{    public static void main ()     {                        int x = 10 ;                        int y = 20;                        System.out.println(x+y);        }}
This program will print the result as the value of x and y and will not store its value anywhere.
So , In this was you can do simple calculations with the use of Java.
Now let us proceed a bit further. How to do advanced calculations.
Let me show you a program in which we are solving an algebraic expression. Let us the simplest expression  i.e (a²+b²+ab)

How to solve an algebraic expression using Java

public class calculation{    public static void main ()     {                        int a = 10 ;                        int b = 20;                        System.out.println(a*a + b*b + 2*a*b);        }}
This program will print the value of the expression.
Now you would be thinking that do we have to write a separate program for each calculation that we have to do. Well, the answer is no 😀 You can use the same program to perform the calculations with different numbers. Moreover, you can also make this program, user-oriented which means that the user will enter the value of x and y and your program will display the result. Let us see how to do this:-
To make the program user oriented, you need to use Parameters.

What are parameters in Java 

Parameters are used in Java to add a user choice to the program? Whenever the program is executed, the user is asked to enter a value for the parameter and that value will be used in the main course o f the program.
Now, in the next program, The use of Parameters is shown. In the following program, the user will be asked to enter a value to the variable and then the result will be calculated accordingly. Let us more forward to our program.

How to use parameters

public class calculation{    public static void main (int a , int b)     {                        System.out.println(a*a + b*b + 2*a*b);        }}

So, In order to use  parameters, we need to place them inside the parenthesis. Let us see how this works :-
When you add a parameter between the parenthesis, you will see that the program gets executed in a somewhat different way.

How parameters work :-

  • Open BlueJ
  • Create a new project and new class.
  • Copy the same program as given below :-
public class calculation{    public static void main (int a , int b)     {                        System.out.println(a*a + b*b + 2*a*b);        }}
  • Now click on the compile button.
  • Close the program window and right click on your program.
  • Now click on “ void main(int a , int b) “
  • You will see something like the following image :-
  • In this dialog box , enter the values you want to give to the variables in your program.
  • Now click on “OK “
  • You will see the output of the program in the next window.

Muhammad Akbar

About Muhammad Akbar -

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you