Saturday, December 30, 2017

Programming: Method


                
              “A method describes an action that the object can perform” (Mueller 77). The on the right is an example of a method. The word public and static modify the method, so it can be seen outside of the class that it is in hence the word public. Every method has a return type, which is the information that the method returns to the caller e after it has completed its task. The “void” from the picture on the right is example of a main method that doesn’t return any information to the caller. The “String” is basically a text, char variables are only used to form letters while string is a list of char variables placed together to from a word.

               Using the class from my previous post, I can add an object inside the main method. To add an object, I wrote the class name first followed by the new object I just created. I called by object “FavoriteBook”. The number in the parenthesis is the publish date.






               Now that we added an object, we can create a new method so it can perform a task. The new method I created is called page and when creating a new method, it must be out of the main method. So, it cannot be inside the curly brackets of the main method. In the parenthesis of my new method, I added “int pages” which would be the number of pages. Below my new method, I added the code “System.out.println ();” and by adding this it will be able to print out the text I put in the parenthesis. 

               Now that the page method is created, I can call the page method on the object I just created called, “FavoriteBook”. By doing this, I would write this line of code in the main method and I would be written out like this.

               If I were to run this, it would output the text that would be written in the parenthesis of the code “System.out.println();”. As an example, say I read 275 pages of my favorite book per hour. I would input the number 275 into the parenthesis of “FavoriteBook.page();”. I do this because that code that I input the number corresponds to the new method I created called page. I would then write out a text in the “System.out.println”. The entire code would look like this.

               Notice that the word “pages” in the text, the word that isn’t in blue, doesn’t have quotation marks around them. This is because it equals to a number, meaning it is not going to output the word “pages” but it would output a certain number and that certain number would be the number I plugged into the “FavoriteBook.page”. This is what the output would look like.

               To sum it up, a method is a set of instructions that can be called on by an object and a return value is the information that a method will return after the program runs. In the example above, “FavoriteBook” is an object while “page” is a method and the program I wrote is an example of a calling a method on an object. Calling a method makes easier to code and less writing for programmers as they would not have to rewrite the method whenever a new object is created. Learning how to program is like learning a language, they both require a lot of memorization. So, do you have any advice or a form of study when it comes to remembering a big chunk of information?


Mueller, John Paul. 
Java eLearning Kit For Dummies. Wiley, 2014.

1 comment: