Well, the code I would share today would be a way to create a multiplication table using Java. I bet there are a lot of different codes out there to illustrate this, there are no wrong or right answer to coding but a matter of personal preference. However, I would like to point out that having comments in your code would greatly improve the readability and if eventually you get to secure a job that requires coding it would definitely be an advantage as others will be able to read your code more efficiently compared to those without comments. Comments can aid in understanding your logic when you are typing the code itself. Sorry to diverge a little now to the code, there are several ways to write an application with the same objective. However, different coders have their own way of thinking and hence each person code may vary depending on how they want the application and steps to flow. Hence, if you happen to research on multiplication table code online and find codes that vary with each other, don't get too much of shock. Start by studying the logic behind the codes and you will understand the differences. Well, to me, as long as it achieve what I want it to, I am pretty much fine now. However, if you are working on a application that you want to publish, try to keep the code simplified making it easy to read and edit by programmers whom you may employ in future if your application is successful.
For now, this is a slightly more complicated mathematical operation you can achieve using the code below:
Moving on, we have our classic "public class IntegersandBasicMathOperations {" and "public static void main(String args[]){". If you forget what these meant, you can refer back the post in 1.1 for the explanation and the basic in using eclipse for Java programming.
After that, as all of us know, in multiplication table, we will need two integers in order to multiply with each other. I think I had just typed something really stupid. But nevertheless, in our multiplication table that I'm illustrating would multiply a number of your choice with 1 to 10, which actually gives you the multiplication table of the number of your choice. Therefore, we need to define two variables as the integers (you may choose to rename the variable if you like). In this case we name the variable as a "num" and "a".
In order for the user to choose a number of their choice, they will need to input their option. A new for java to recognize the input is through the scanner utility. Therefore, we will need to call out and define a new scanner for this particular application. We do this by typing the following: Scanner in = new Scanner(System.in);
With the scanner created, we can then proceed to instruct the user to key in the particular number of their choice for the multiplication type with "System.out.println("Choose a number for the multiplication table!");". Also, we need to let the application know that the number that the user input will be the number we want to have as the variable "num". Therefore, we can type "num = in.nextInt;". It represents letting the variable 'num' to be the next integer that is input into the system.
Then, we need to explain to the user that the system will print out the multiplication table to the user using "System.out.println("Multiplication table for " + num);".
After that, we would reach a complicated part where we need to make the system repeatedly print the multiplication between the number from one to ten. If you are one of those hard working ones out there, you can definitely type in "System.out.println(num + "*" + 1 + "=" + (num*1));" and repeating this ten times replacing the 1 with 2, 3, 4, ... until you reached 10. However, there is another way to do this and that is by introducing a loop into your code. One of the way is typing the following:
for (a = 1, a < 11, a++)
System.out.println(num + "*" + a + "=" + (num*a));
The above means we have the loop to start at a = 1 and the condition for the loop to repeat would be "a" < 11 (which means once "a" reached 11 and above the loop will stop repeating itself) and the last "a++" would be the change that you want the loop to adapt after each cycle. "a++" is similar to typing "a = a + 1", which also means that we want variable "a" to increase by one after each loop cycle. I am not sure if this is clear, please give me some feedback if it's not so that I can improve the explanation and find a simpler way to explain this.
Alright, then we are pretty much prepare to run our application to test if it is working! If it is not, don't be too worried, remember to check that you have your semi-colon in the right place. Remember Java is sensitive to capital letters and hence check it you have included a capital or small letter in the wrong place as well.
Once you have successful launch, the application will ask you to input a number of your choice for the multiplication table.
From the above, you can see that in the console which shows your application, it will ask you to input a number of your choice. So click on your console once and key in any number of your choice. Make sure you key in an integer as we define everything in our code as integers. After that, just need to press and enter and you will see the following:
You can test it with different integers to make sure. And there you have it, you just program an application which shows you the multiplication table of an integers of your choice.
Of course, you can develop this code further to do something else, or makes it more interactive. It's great to edit the code and see what will happen if you change something to it, it will make you understand the code better and definitely you will improve your skills as a beginner programmer as well.
If you have any questions, just drop a feedback. I will try my best to answer it. Please limit the scope of your question to the code that is listed on this post, I am no expert and I am still learning myself also.
Hope most are sleeping right now since it is way past bed time. I am going to bed soon as well. Our weekend is going to end soon. How scary how fast time flies.
No comments:
Post a Comment