Skip to main content

Featured

How to make a simple calculator with GUI in JAVA (using Netbeans)?

First of all create a new package , then create a simple class. Then create a new JFrame Form class. On the right side you will find every tool needed like buttons textfield radio buttons etc. We'll need the buttons and text field and a JLabel Drag and drop required buttons you want to appear on the calculator eg. 1,2,3,4,5,6,7,8,9,0,+,-,*,/,=,AC,OFF arrange them all then on double clicking any button you will enter the source code tab, for digits 0-9 you have to write this code:         String Number =answer.getText() +  one.getText();         answer.setText(Number); Here answer is the jTextFields variable name you can right click the text field and click change variable name. similarly you have to set the code for every digit then for operations you have to write the code: for "+" operation: firstnumber = Double.parseDouble(answer.getText());         jLabel1.setText(answer.getText()+"+"); ...

Pass by value and reference

Passing any variable by value:

When we pass a variable by value to a function its value is given to the function only.
any changes made to the function wont affect the original variables value.

In the below example we have a function i.e passing(int )
when we call the function in main body giving the value of "a" which is 3
the function performs its body operation.
and when we cout the a variable
cout<<a;

we get the original value because we passed the value to function by value so that isnt going to affect the original variable "a".


#include <iostream>
using namespace std;

void passing(int b)
{
 b=b+5;
 
}

int main()
{ 

int a;
 
a=3;

passing(a);
cout<<a;
 
return 0;
}


Screenshot:





Passing any variable by reference:


For passing variable by reference we use a pointer variable in the receiving function and a &variable
in the calling function.

in the below example we have used a function with variable *b
and we are actually passing the address of variable a in the int main body
&variable means the address of the variable .When a function has address of a variable then when we or the function changes the value of the variable 'b' it actually changes the value of a.
As both a = b for now
a=address=for example= house 123
b=address=for example=house 123

when we deconstruct b's house
we actually are deconstructing a's house.
:)


#include <iostream>
using namespace std;

void passing(int *b)
{
 *b=*b+5;
 
}

int main()
{ int a;
 
a=3;

 passing(&a);
cout<<a;
 


return 0;
}



Screen shot:




Comments