Difference between revisions of "Java/C2/Arithmetic-Operations/English"
(Created page with ''''Title of script''': Arithmetic Operations in Java '''Author''': TalentSprint '''Keywords: '''datatype, int, float, numerical data, arithmetic operations, operator precedence…') |
|||
(One intermediate revision by the same user not shown) | |||
Line 21: | Line 21: | ||
'''Learning Outcomes''' | '''Learning Outcomes''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| In this tutorial, you will learn about the various ''' | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| In this tutorial, you will learn about the various '''Arithmetic Operations ''' namely |
− | + | ||
'''Addition''' | '''Addition''' | ||
Line 43: | Line 42: | ||
'''JDK 1.6''' and | '''JDK 1.6''' and | ||
− | '''Eclipse 3.7''' | + | '''Eclipse 3.7'''. |
− | + | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 4 | | style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 4 | ||
'''Prerequisites''' | '''Prerequisites''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| To follow this tutorial you must have eclipse installed in your system |
− | + | ||
− | + | ||
− | + | and you must '''know how to create, save and run a file in Eclipse'''. | |
Line 84: | Line 80: | ||
'''}''' | '''}''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| We have Eclipse IDE and the skeleton required for the rest of the code. | + | | style="border:0.035cm solid #000000;padding:0.097cm;"|Here We have Eclipse IDE and the skeleton required for the rest of the code. |
Line 100: | Line 96: | ||
'''int result<nowiki>;</nowiki>''' | '''int result<nowiki>;</nowiki>''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| | + | | style="border:0.035cm solid #000000;padding:0.097cm;"|'''int x = 5;''' |
+ | '''int y = 10;'' print result | ||
− | '''x''' and '''y''' will | + | |
+ | '''x''' and '''y''' will be the operands and the '''result''' will store the output of operations. | ||
|- | |- | ||
Line 111: | Line 109: | ||
'''System.out.println(result);''' | '''System.out.println(result);''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Let us add them and print the result. | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| Let us add them and print the result.''' Result= x+y;''' system. out. println ''''in parantesis '''result'' |
− | + | ||
Line 118: | Line 116: | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''. | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''. | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| | + | | style="border:0.035cm solid #000000;padding:0.176cm;"|'Save it with ControlS and contron F11 to''' Run''' |
− | + | ||
Line 125: | Line 122: | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to output. | | style="border:0.035cm solid #000000;padding:0.176cm;"| Point to output. | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| We see, that the output of addition is stored and the value has been printed |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Change '''x = 5 '''to '''x = 75''' and '''y = 10 '''to '''y = 15''' | | style="border:0.035cm solid #000000;padding:0.176cm;"| Change '''x = 5 '''to '''x = 75''' and '''y = 10 '''to '''y = 15''' | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| Let us change the values | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| Now Let us change the values . |
+ | '''x=75''','''y = 15''' | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''. | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''. | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' it '''Run''' |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.097cm;"| Point to output | | style="border:0.035cm solid #000000;padding:0.097cm;"| Point to output | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| we see the output has been changes accordingly |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Change '''y = 15 '''to '''y = -25.''' | | style="border:0.035cm solid #000000;padding:0.176cm;"| Change '''y = 15 '''to '''y = -25.''' | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| Now let us try negative values. | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| Now let us try negative values. '''y = -25.'' |
− | + | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Save''' and '''Run'''. | | style="border:0.035cm solid #000000;padding:0.097cm;"| '''Save''' and '''Run'''. | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| '''Save''' | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| '''Save''' '''Run.''' |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to output | | style="border:0.035cm solid #000000;padding:0.176cm;"| Point to output | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| we see that the output of 75 plus of -25 has been printed |
− | + | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''y = -25''' to '''y = 5''' and '''x + y '''to '''x – y''' | | style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''y = -25''' to '''y = 5''' and '''x + y '''to '''x – y''' | ||
Line 163: | Line 157: | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Now let us try subtraction. | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| Now let us try subtraction. '''y = 5'' and change x+y to x-y |
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''. | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''. | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save ''' | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save it ''' '''Run.''' |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to output | | style="border:0.035cm solid #000000;padding:0.176cm;"| Point to output | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| we see that the output of 75-5 has been printed, |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''x – y '''to '''x * y''' | | style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''x – y '''to '''x * y''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Let us try the multiplication. | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| Now Let us try the multiplication. Change '''minus''' to ''' asterisk''' |
− | + | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run.''' | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run.''' | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' '''Run.''' |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to output | | style="border:0.035cm solid #000000;padding:0.176cm;"| Point to output | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| we see that by using asterisk we could multiply 75*5. . |
− | + | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''x * y''' to '''x / y''' | | style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''x * y''' to '''x / y''' | ||
Line 197: | Line 187: | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| | + | | style="border:0.035cm solid #000000;padding:0.097cm;"|Now let us try '''division''' Remove asteriskand type slash |
− | + | ||
The front slash symbol is used to perform division. | The front slash symbol is used to perform division. | ||
Line 204: | Line 193: | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run.''' | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run.''' | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save it''' '''Run.''' |
|- | |- | ||
Line 223: | Line 212: | ||
The result must be 7.5. | The result must be 7.5. | ||
− | So let us change the result to float. | + | So let us change the result to a float. |
+ | |||
|- | |- | ||
− | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save it''' '''Run'''. | |
− | + | ||
− | + | ||
− | + | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' | + | |
Point to output. | Point to output. | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| Note that although the expected result is 7.5, we get | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| Note that although the expected result is 7.5, we get output as 7.0 |
|- | |- | ||
Line 244: | Line 230: | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Change '''y = 10''' to '''y = 10f''' | | style="border:0.035cm solid #000000;padding:0.176cm;"| Change '''y = 10''' to '''y = 10f''' | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| Let us change y to a float. | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| Let us change y to a float. y=10f |
− | + | ||
− | + | ||
− | + | ||
+ | |||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run.''' Point to output. | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run.''' Point to output. | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' '''Run.''' |
Line 266: | Line 250: | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Now let us see what happens when there is more than one operator. | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| Now let us see what happens when there is more than one operator.Remove all the operands |
|- | |- | ||
Line 273: | Line 257: | ||
Point to output | Point to output | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| | + | | style="border:0.035cm solid #000000;padding:0.097cm;"|int result= 8+4-2. Save it run |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
As we can see, the output is as expected. | As we can see, the output is as expected. | ||
Line 289: | Line 270: | ||
Point to output | Point to output | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''minus''' to ''' | + | | style="border:0.035cm solid #000000;padding:0.097cm;"|Now Change '''minus''' to a ''' s lash''' |
− | Now the output | + | Now the output would be 6 if the addition is done before division. |
− | + | Or it would be 10 if division is done before addition. | |
Line 301: | Line 282: | ||
− | As we can see, the division is done before addition. This is because the division operator has more precedence than the addition operator. | + | As we can see, the output is 10 and the division is done before addition. This is because the division operator has more precedence than the addition operator. |
Line 314: | Line 295: | ||
Point to output | Point to output | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| |
− | + | ||
By adding parentheses, we instruct Java to do the addition before the division. | By adding parentheses, we instruct Java to do the addition before the division. | ||
− | Let us run the file. | + | Let us run the file now. |
− | As we can see, the output is 6 as expected. | + | As we can see, addition has been performed first and the output is 6 as expected. |
Line 335: | Line 315: | ||
'''Summary''' | '''Summary''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| This brings us to the end of this tutorial. |
In this tutorial we have learnt | In this tutorial we have learnt | ||
Line 350: | Line 330: | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Find out what is meant by the '''modulo''' operator and what it does. | + | | style="border:0.035cm solid #000000;padding:0.097cm;"|As an assignment for this tutorial Find out what is meant by the '''modulo''' operator and what it does. |
|- | |- |
Latest revision as of 11:06, 8 May 2013
Title of script: Arithmetic Operations in Java
Author: TalentSprint
Keywords: datatype, int, float, numerical data, arithmetic operations, operator precedence, video tutorial
Visual Cue | Narration |
Slide 1
Welcome |
Welcome to the spoken tutorial on Arithmetic Operations in Java. |
Slide 2
Learning Outcomes |
In this tutorial, you will learn about the various Arithmetic Operations namely
Addition Subtraction Multiplication Division and How to use them. |
Slide 3
Tools Used |
For this tutorial we are using
Ubuntu 11.10, JDK 1.6 and Eclipse 3.7. |
Slide 4
Prerequisites |
To follow this tutorial you must have eclipse installed in your system
and you must know how to create, save and run a file in Eclipse.
|
Slide 5
Operators |
Here is a list of operators and the mathematical operations they perform
We shall look at each of them in detail. |
Minimize Slides and open Eclipse
Eclipse should contain the following code public class ArithmeticOperations{ public static void main(String[] args){ } } |
Here We have Eclipse IDE and the skeleton required for the rest of the code.
|
Type in main method
int x = 5; int y = 10; int result; |
int x = 5;
'int y = 10; print result
|
Type in main method
result = x + y; System.out.println(result); |
Let us add them and print the result. Result= x+y; system. out. println 'in parantesis result
|
Save and Run. | 'Save it with ControlS and contron F11 to Run
|
Point to output. | We see, that the output of addition is stored and the value has been printed |
Change x = 5 to x = 75 and y = 10 to y = 15 | Now Let us change the values .
x=75,y = 15
|
Save and Run. | Save it Run |
Point to output | we see the output has been changes accordingly |
Change y = 15 to y = -25. | Now let us try negative values. 'y = -25.
|
Save and Run. | Save Run. |
Point to output | we see that the output of 75 plus of -25 has been printed |
Change y = -25 to y = 5 and x + y to x – y
|
Now let us try subtraction. 'y = 5 and change x+y to x-y
|
Save and Run. | Save it Run. |
Point to output | we see that the output of 75-5 has been printed, |
Change x – y to x * y | Now Let us try the multiplication. Change minus to asterisk
|
Save and Run. | Save Run. |
Point to output | we see that by using asterisk we could multiply 75*5. . |
Change x * y to x / y
|
Now let us try division Remove asteriskand type slash
The front slash symbol is used to perform division. |
Save and Run. | Save it Run. |
Point to output | As we can see, the output is as expected. |
Change y = 5 to y = 10
|
Now let us see what happens when the expected result is a decimal point number.
So let us change the result to a float.
|
Save it Run.
Point to output. |
Note that although the expected result is 7.5, we get output as 7.0 |
Point to the operands. | This is because both the operands involved in the division are integers. |
Change y = 10 to y = 10f | Let us change y to a float. y=10f
|
Save and Run. Point to output. | Save Run.
|
Remove everything inside the main function, except the print line and add the following, before
Int result = 8 + 4 – 2;
|
Now let us see what happens when there is more than one operator.Remove all the operands |
Save and Run.
|
int result= 8+4-2. Save it run
|
Change 8 + 4 – 2 to 8 + 4 / 2
|
Now Change minus to a s lash
|
Change 8 + 4 / 2 to (8 + 4) / 2
|
By adding parentheses, we instruct Java to do the addition before the division.
|
Minimize the Eclipse window and switch to slides.
Summary |
This brings us to the end of this tutorial.
In this tutorial we have learnt How to perform basic mathematical operations in Java. About operator precedence, and, How to override it. |
Slide 7Assignment
|
As an assignment for this tutorial Find out what is meant by the modulo operator and what it does. |
Slide 8About the Spoken Tutorial Project
|
To know more about the Spoken Tutorial project, watch the video available at the following link, that summarizes the project.Alternatively, you can download and watch it. |
Slide 9Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team. Conducts workshops using spoken tutorials and gives certificates for those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
Slide 10Acknowledgement
|
Spoken Tutorial Project is a part of the Talk to a Teacher project and is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro |
Slide 11About the contributor
|
This tutorial has been contributed by TalentSprint. Thanks for joining.
|