Java/C2/Arithmetic-Operations/English

From Script | Spoken-Tutorial
Revision as of 15:41, 27 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 Numeric Operators in Java that are

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

For this tutorial, you should have

Eclipse installed on your system and

should know how to create, save and run a file in Eclipse.


If not, for relevant tutorial please visit our website as shown.

Slide 5

Operators

Here is a list of operators and the mathematical operations they perform


  • plus symbol for addition
  • minus for subtraction
  • asterisk for multiplication
  • front slash for division

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){

}

}

We have Eclipse IDE and the skeleton required for the rest of the code.


We have created a class by name ArithmeticOperations and added the main method.


Let us add some variables.

Type in main method

int x = 5;

int y = 10;

int result;

We have created three variable.


x and y will behave as operands while result will store the output of two operands.

Type in main method

result = x + y;

System.out.println(result);

Let us add them and print the result.



Save and Run. Now let us Run the code.



Point to output. As we can see, the result now has the sum of values in x and y.
Change x = 5 to x = 75 and y = 10 to y = 15 Let us change the values now.


So type 75 in place of 5

And 10 in place of 15

Save and Run. Save and Run
Point to output As we can see the output changes as we change the values.
Change y = 15 to y = -25. Now let us try negative values.

So type -25 in place of 15

Save and Run. Save and Run.
Point to output As we can see, the effect is same as subtracting y from x.
Change y = -25 to y = 5 and x + y to x – y


Now let us try subtraction.


So type 5 in place of -25

And minus in place of plus

Save and Run. Save and Run.
Point to output As we can see, we can perform subtraction using the minus operator.
Change x – y to x * y Let us try the multiplication.

So type asterisk in place of minus.

Save and Run. Save and Run.
Point to output As we can see, the asterisk symbol is used to perform multiplication.
Change x * y to x / y


So type front slash in place of asterisk.


The front slash symbol is used to perform division.

Save and Run. Save and 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.


Change 5 to 10


The result must be 7.5.

So let us change the result to float.



Change int result to float result Change int to float
Save and Run.

Point to output.

Note that although the expected result is 7.5, we get the 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.


Add an f at the end.

Save and Run. Point to output. Save and Run.


Now we can see that the result is as expected.


Keep in mind that when the expected result is a float, one of the operands must be a float to get the expected output.

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.
Save and Run.


Point to output

The result we would expect is 10.


Let us Save and Run


As we can see, the output is as expected.

Change 8 + 4 – 2 to 8 + 4 / 2


Save and Run.


Point to output

Change minus to front slash


Now the output could be 6 if the addition is done before the division.


And 10 if division is done before addition.


Let us Run and see the output.


As we can see, the division is done before addition. This is because the division operator has more precedence than the addition operator.


In such situations, if we need to override the precedence, we use parentheses.

Change 8 + 4 / 2 to (8 + 4) / 2


Save and Run.


Point to output

Add parentheses around 8 + 4


By adding parentheses, we instruct Java to do the addition before the division.


Let us run the file.


As we can see, the output is 6 as expected.


As a rule, keep in mind to use parentheses when the order of operations is not clear.

Minimize the Eclipse window and switch to slides.


Slide 6

Summary

We have come 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


Find out what is meant by the modulo operator and what it does.
Slide 8About the Spoken Tutorial Project
  • It summarizes the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


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
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test


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
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


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
  • www.talentsprint.com
  • Thanks for joining


This tutorial has been contributed by TalentSprint. Thanks for joining.



Contributors and Content Editors

Chandrika, Sneha