Sunday, December 26, 2010

Java Tutorial #15

0
Accessing method (with an argument inside the parameter) into another class

Again, the first thing to do is to create a new subclass.  Of course, that subclass will hold a method that later will be implement to our main class.  In order to understand this topic you must knew already how to make a method and that is recently explained in the previous topic.  So, I recommend everyone to read the previous topic before this one.
This method will hold an argument inside its parameter.  When a method is about to use a variable, the type of that variable must be declared inside the parameter of that method.  That is called an argument. "String " is the variable's data type and "Name" is a variable's name.
Consider this example


In this example, the method called “Method” will be using the variable “Name”.  So, in order to use that variable the method must declare the “Name” data type.  The type and the name of the variable will be placed inside the parameter of the Method and it is called an argument.
“It is an extra data needed by the method in order to work”, bucky said.
That method will simply print the word “Hello” plus the variable “Name”.  The argument specifies that the variable “Name” is a string.
 Back to the main class, import and create a new Scanner and create a new object.  Then, print an instruction to the user.  Use the method, you created from the subclass.


                           

The Output




Java Tutorial #14

0
Accessing a method (with empty parameter) into another class
                                 

 First thing to do is to create a new class (sub-class. If you already have your main class) 
You can do this by clicking the new class icon in the toolbar then choose "Class".
In the Dialog Box, uncheck the check box with the option of "public void static main(..)" then Finish.


In the SubClass.

Then create a new method.  A Method is a function inside your program.  It is like a little sub-program inside your main-program.  You can type actions and bunch of codes inside this method.



public means that all of the class can use this method. This can be replace by private which means that only that class can use the method. But, in this case, we don’t need to do that.
void means that the method will gonna do something but it will not return any data at all. This can be replace by any data types you want to associate with your method.  e.g. String if the method will return a string value.  int if the method will return an integer value.
MethodName( ) is any name you want to associate with your method. In this case, we will not put a parameter inside that method.


In the Main Class.


After creating your method, go back to your main class.  Then connect your main class to your sub-class. This can be done by creating an object that will hold the data from your subclass to the main class. Here’s how:

First indicate the name of your subclass. 
Create an Object. You can assign any name for your Object Name. 
Put an equal sign and type the word “new”, the name of your subclass and put an empty bracket.


Then you can now use the method declared. By using the Object you previously declared and the name of the method you created from the subclass.


After that it will output this one. It will do the command you created from the subclass.







Wednesday, December 1, 2010

Java Tutorial #13

0
Do-While Loop in Java
It will first execute a body of codes before testing for a condition.  Its general equation is:

do{
statements;
}while(condition);

Where:
condition = is where the loop will stop.

You maybe find this very similar to the while loop (because I use the same problem) but as we move on you will see their differences.  But for now, the obvious difference is that the while loop will test first for a condition then execute a command while do-while will execute a command first before testing for a condition.  Consider this example.



Java Tutorial #12

0
While Loop in Java
It’s general equation is:

while(condition){
Statements;
}

Where
Condition = is where the loop stop.
while keyword allows you to execute a blocks of commands by writing only one block of code for it.  Consider this example, 



Java Tutorial #11

0
For Loop in Java
The for statement is one of the useful statement in Java.  The for keyword has three arguments.  The first is where you want to start the Loop (INITIALIZATION), the second is where you want to end the Loop (CONDITION), and the last argument is how much you want to increment to value(INCREMENT).  Its general equation is:

for(INITIALIZATION, CONDITION, INCREMENT){
Statements;
}

NOTE: Variable++ will only increment a value of 1.  Variable+=value will increment a specific value which is greater than 1.

Consider this example



Java Tutorial #10

0
The Switch Statement in Java
The switch statement syntax is also the same as the C++.  It’s general syntax is:

switch(variable){

                case value1:
                Statements;
                break;

                case value2:
                Statements;
                break;


                default:
                Statements;
                break;
}

Where:
variable =  is the variable you want to switch
value1, value2 = is a Boolean value.   It can’t be a target of conditions.  It must be a numeral value not a character nor a string.

The switch statement is the enhanced version of if statement.  You maybe find it similar to if statement but switch statement is much more different to if statement.  The switch statement cannot be a target to any conditions of values.  The case can’t be a target of conditions to a value.  So, if you are about to use a conditions, still it is a good idea to use if-else if-else statement.   Consider this program.







Java Tutorial #9

0
The If Statement in Java
The Basic Syntax of if statement in C++ is:

if(condition/s){
Statements;
}
elseif(condition/s){
Statements;
}
else{
Statements;
}

But this Syntax will not work in Java.  The new syntax of if statement in Java is:

if(condition/s){
Statements;
}
if(condition/s){
Statements;
}

In this first syntax, multiple ifs is acceptable to Java.  But if you like to implement the general concept of if-elseif-else statement (which is very similar to c++) to your Java Program, you can still use it using this syntax.

if(condition/s){
Statements;
}
else if(condition/s){
Statements;
}
else if(condition/s){
Statements;
}
else(condition/s){
Statements;
}

 Consider this example.