Monday 26 October 2015

Java Expression Computation


Wow! We are becoming Java programmers gradually for reals.

Now I want to teach you something nice and that nice thing is called an operation or expression.

For one to refer to a chunk of code as an expression, two things must be involved:

1.   Operator
2.   Operand

Mathematically, an expression is something like 4 x 5 etc.
In the above example:
4 and 5 are known as operands while x is known as an operator and putting them together the way they are in the example makes it an expression.

This same thing goes for programming. Just like 4 x 5 as used in the example as a mathematical expression. It can also be a programming expression.

There are five major operators used in Java programming that I want to talk about today which are:


1.   +    -    Addition
2.   -    -    Subtraction
3.   *    -    Multiplication
4.   /    -    Division
5.   %    -    Remainder

As you can see this operators are very comprehensive and note that each one of these operators must require two operands, one at the left and one at the right to become an expression.

Take for Example I want to write a program that calculates this expression in java: 50 + 21 | Addition

What do I do first?

First, lets create a new file called ComputeExpression.java. Remember if you are using NetBeans in creating a new file all you need to do is write the name with no file extension, just write ComputeExpression.

Now once the file is created you should have something like this (referring to my own code, after removing the block comments):

package nicholas;
public class ComputeExpression{
}

remember that my package and yours should not be the same.

Now remember what I told you in the previous post, No Java program can run without a main method.
So our next step is to input our main method into the class block which goes thus:

package nicholas;
public class ComputeExpression{
public static void main(String [] args){

}
}

Perfect!!
Please always try to indent your blocks it makes your code look neat and awesome.
Now we need a command that would tell the computer to print out the answer to our expression right? YES
From the previous post we know that the command we need is:

System.out.println();

So the next step is to insert this command with the expression inside the round bracket which goes thus:

package nicholas;
public class ComputeExpression{
public static void main(String [] args){
System.out.println(50 + 21);
}
}

If you are vigilant you will notice that there is no quotation marks inside the System.out.println(); command like our HelloMe.java in the previous post.
The reason is this: We are telling the computer to do the calculation and display the answer that is all but if you but quotation marks in it you will be telling the computer to display 50 + 21 and not the calculation.

Next step is to configure and select our main class. I showed you how to do that in the previous post.

In my own config, I selected nicholas.ComputeExpression.

After you selection and clicking OK.
You can then run your program and see your answer right before you.

You should get 71.

Here is my code

Here is my console showing my output



I know you are happy! Well, you should definitely be.

Let Us try subtraction, this time around lets try coding 276 - 127
Editing the previous code, here is what the code should look like:
package nicholas;
public class ComputeExpression{
public static void main(String [] args){
System.out.println(276 - 127);
}
}
Save and click run and your answer pops up.

Here is my Code:

Here is my Output:


Now Let's try Multiplication, 673 * 1234
Here is what the code should look like:
package nicholas;
public class ComputeExpression{
public static void main(String [] args){
System.out.println(673 * 1234);
}
}
Save and click run, your answer pops up.

Here is my code:

Here is my output:

Trying Division, 12345 / 25
Here is what the code should look like:
package nicholas;
public class ComputeExpression{
public static void main(String [] args){
System.out.println(12345 - 25);
}
}
Save and click run, your answer pops up.

Here is my code:

Here is my output:

Trying Remainder, this operator is used to output the value that remains after a division is carried out for example 11 % 5 = 1

Let's Code so you could see.
package nicholas;
public class ComputeExpression{
public static void main(String [] args){
System.out.println(11 % 5);
}
}

Here is my code:

Here is my output:


I HOPE YOU GOT THE GIST.

There are situations where an expression consists of more than one operator and two operands.

Take for example you are asked to write a program that can solve:

4 x 5 + (20 / 2) - 3 + ((50 x 1) % 4)

Do not panic all you need do is write you code exactly as it is with the brackets in the correct place and changing x to *. That's all!

Here is an example code:
package nicholas;
public class ComputeExpression{
public static void main(String [] args){
System.out.println(4 * 5 + (20 / 2) - 3 + ((50 * 1) % 4));
}
}

The computer uses the well known BODMAS solution steps for handling algebra to handle this problem.
Mathematically you should get 29 as your answer.

Here is my code:

Here is the output:


Here is an assignment, write a program that can solve this expression and comment your answer. Make sure you use nothing but a java program.

((12 % 2) + 3.2) x 12 - 3 x 4 + 5 - 12 x (5 - 1 x (100 % 3))


Peace!!

No comments:

Post a Comment