Welcome to the second Java programming tutorial! Hope you have been typing and messing around with the code from tutorial one with different strings of sentences.
Now for this tutorial we will touch on how to use integer and do basic mathematical operations with the Java. No worries, it is not a big jump from what we have learnt, just a little add on. With this, we can use Java to solve our maths homework! Well, I will just do a basic calculation Java code example here, it may not be used to solve the complicated maths problem we all have from schools.
I hope you still remember how to create a Java application and a class in order to input your code for the Java application.
For this second code, we will be doing a little classification of the numbers we input into Java and use these numbers to do a little mathematical operation.
The above code represents an example to do multiplication with integer on Java. Similarly, first you create a Java application project and name it. After which, you got to create a class within the specific Java application project that you created, and name it as well. For class, the naming should not contain any spaces or special characters if not there would be an error.
Now, I will move on to explain the key points for this code. Mainly, the first interesting thing you might want to note would be the "int num;". This actually represents your will in classifying the variable "num" as an integer. You may wish to change the variable and give it a different name as you wish, and it will still work the same way. Try exploring yourself to further understand what's a variable.
After that, in order for the application to work out some maths for us, we need to tell the application what we want to calculate. For this example, I want to multiply a number by 3. So, let's choose a random number 157 and type in "num = 157;" to enable to application to know which number we want to calculate. You may choose a different number, and it will still work. However, as we have classify the number as an integer, you should not key in decimals or fractions as it will cause an error when you run the program. Your code will have to be logical for the computer/application to understand what you want them to do. If you classify the number as integer but you key in a decimal (for example 157.52), you are contradicting your code and it would result in an error as the computer would expect an integer instead of a decimal.
Now after keying in the number we want to use to do the multiplication, we have to tell the computer to multiply both the number and 3 to get our answer. Hence, we type in the code "num = num * 3;" and this will tell the computer what we want to be calculated. However, it does not end here. Yes, the computer have calculated the number but you have not tell the computer to show or print out the answer. Therefore, adding in "System.out.println("The product of the number we choose with 3 is " + num);" will give us the answer.
You may wish to change the multiplication to division, addition or subtraction as you like. Also, you can try a combination of these operations to get a more complicated operations done. We have to explore more on ourselves in order to understand how it works and eventually use it to solve our mathematical problems that we have.
This is a simple mathematical code for this tutorial. I may post a slightly more complicated one later, so that for those who are learning, we can practice together. If you have an interesting code, please feel free to post it or email it to me so that I can also learn more about it. Learning alone is pretty boring, however, when you are able to find like minded individuals, learning tends to get more interesting.
Stay tuned to the upcoming tutorial.
No comments:
Post a Comment