Tuesday, January 31, 2017

Learn Java for Beginner easily ! Part 15 : Using More Methods

What's up guys ! Welcome back to my Java tutorials series. In the couple of my tutorials ago, I've thaught you guys about how to run a method inside the other class from your main class.In this part, I'm gonna teach you guys about how to use a lot of methods. Hmm not really much by the way, but at least it's gonna be more than one method in a class.From now I want you guys to listen up and pay more attentions because this part is not gonna be easy, but I'm not saying this will be hard, because if you read this tutorial carefully and do some repetations, I'm pretty sure you guys gonna learn it well. So as usually, before I explain about it further, I will let you guys take your time typing both of the codes in the pictures below.



1.First Code (Main Class)



2.Second Code (Place where there are a lot of methods)
Next, I want you guys to run this program, Input what it asked and then Press 'Enter'. Remember guys, whenever you are using more than one class, Run your codes from the main class. In this case, my main class named "maain". This is an example of program that combine your answer with a string from another methods by using more methods. Let me explain to you each line of these codes.

First Program :
   Line 1

"Import java.util.Scanner;" is a keyword for you to tell Java that you need to use the scanner utility. So, whenever you want to input some stuff in your program when it runs, you will have to import the java scanner's utility by typing "Import java.util.Scanner;" in your program.

   Line 2-3

The declaration of your main class and the method inside it.

   Line 5

The code on the picture above is the declaration of variable "input" as a place where your Input stored. System.in  shows that whatever you type on your keyboard to input data, the data that you input will be stored in the "input" variable.

   Line 6

"NameOfClass (space) NameOfClassObject = new (space)NameOfClass( );"

When you typed this line, it means the other class has been called in.It's just like you tell java that you want to use another class.

   Line 8-9

Line 8 print out a text that told you to enter the a name(a String), then Line 9 determine where is your input will be stored.

   Line 11

It means that you want to use another class named "message" in the "change" methods by using the value of variable "name".

   Line 12
It means that you want to use another class named "message" in the "text" methods.

Second Program :
   Line 2

The declaration of your class.

   Line 3

Declaration of a new variable named "getname". In this case, private means that this variable can only be used in this class, it can't be used by another class

   Line 4

Declaration of a method called "change" which excecuted by using the value of variable "name".You can declared a new variable inside the parameter.

   Line 6

This declared that the value of "name" is equal "getname".

   Line 9

Declaration of a new variable named "backname".

   Line 11

It means that the value of "backname" variable is returned to the "getname". Which make both of the variables have the same value.

   Line 14

Declaration of a method called "text".

   Line 16-17
These are the what Java will do if you use/run "text" methods.Because these two lines are inside the curly brackets of "text" method.

So that's it guys for now. Thank you guys for visiting my blog, hope you guys enjoy it and see you on the next tutorial. If you guys got something to be asked, please leave a comment below and thank you very much.

Greetings from Programmer !

No comments:

Post a Comment