Java Tutorials - Data Operations With Example.

in #steemstem6 years ago (edited)

Hello guys!! Welcome to our next tutorial on Java programming. In last tutorial we learned different data types in Java. Today we are going to see different data operators and how we can perform operations using these operators. So, let's start.

IMG-20180214-WA0002.jpg

Data operations in Java :

We have mainly 4 different types of operators in Java as follows :

  1. Arithmetic operators
  2. Unary operators
  3. Relational operators
  4. Logical operators

Arithmetic operators:

To perform arithmetic operations, we have
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modulus (%)
Addition, subtraction, multiplication and division are the operators we use generally but the Modulus is something you may not be familiar with if you are not from programming background. Basically, what modulus does is that it performs division but rather than giving the quotient what modulus gives you is remainder of that division.
For example: 5%3
Here, rather than getting 1 what you are going to be getting is 2 as the remainder value.

Unary operators:

• Increment operator (++)
• Decrement operator (--)
Let’s say there’s a specific value that you have initialized let’s say:
A=10
And somewhere in your program you want to increment it after a certain set of operations. So, what you are going to do is increment the value of A with the increment operations. We will not go deep here because we will be using the increment operator with the loop statements and I will show how it plays an essential role as a part of control statement.

Relational operators

• Less than (<)
• Less than or equal to (<=)
• Greater than (>)
• Greater than or equal to (>=)
• Not equal to (!=)
• Equals (==)
This is something you may be familiar with. We have seen this type of operators in math but with little bit of different symbols.

Logical operators

• And (&&)
• Or (II)
• Not (!)
Here, you are going to be working with different conditions. You are going to be using ‘And’ and ‘Or’ operations on different conditions. This works same as the truth tables of ‘And’, ‘Or’ and ‘Not’ gates in logic gates.

Let’s see an example:
Let us assume the following values for the variable:

  • A=10
  • B=15
  • C=25
  • D=true

After applying various operations on it :

  • (A+B)-C/5 = 20

  • A++ = 11

  • C>=A = false

  • (B<C)&&(A>C) = false

  • !D = false

Let's apply this in Java using an example.

Ramesh comes to your store to purchase following items :

ProductQuantity
Bread1
Coke2
Butter3

Costs of the products are given below :

ProductCost
Bread150
Coke100
Butter400

When Ramesh came to your store there was 10% of discount on all product. And finally a service tax of 5% is applicable on total cost.
So, compute total cost using Java program.

Now let's go ahead and write a code to compute this value. We have 3 items that we are going to use them as variables. Since they don't have decimal value so we can make them integer. And for price we are going to use double. We are going to declare variables as well as initialize them with a corresponding a value. As you can see in the screenshot given below.

dataoperations1.png

Now, let's write the expression for the total value . Always remember that the expression to he right hand side of assignment is computed first and then the corresponding value is stored in the variable to the left. So, below are the expressions to compute the final value.

dataoperations2.png

Now run the code using right click anywhere on the code > run as > java application. As you can see we get the total value as the output.

dataoperations3.png

So, that's enough for today. In our next tutorial we are going to see control statements in Java.

If you have any questions please comment below.

Important :

All the images used in this article are the screenshots taken in my phone and my pc and some of them are cropped using Paint.

References :

  1. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
  2. Book : The Java programming language by Arnold.

Don't forget to upvote and resteem.
Follow @pratique007

Sort:  

This post has received a 1.26 % upvote from @boomerang thanks to: @pratique007

@boomerang $2.16 for $2.0 . that's not fair

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by pratique007 from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Coin Marketplace

STEEM 0.26
TRX 0.12
JST 0.031
BTC 61258.08
ETH 2873.80
USDT 1.00
SBD 3.56