idiot4
:)
Homepage: https://idiot4.wordpress.com
Java Tutorial 11 – Inheritance
Posted in Java on June 29, 2012
inheritance means Inheritance ! (hehe you know that )
i mean consider two classes one is parent and second is child class and if you inherits child from parent class that child class can access all the variables and methods which are public in parent.
Let we have 2 classes Car and TataNano:
Car:
Java Tutorial 10 – Enhanced For Loops
Posted in Java on June 28, 2012
enhanced for loops are very useful for arrays.
to loop through array what we generally do is we take one int as index and use this index inside loop to point to array location and then we increment it.
But there is one more easiest way to loop through array which is called Enhanced For Loop. it automatically increments index and stops at the end of the array. let me show you one example:
For loop:
Java Tutorial – 9 Method Overloading ( function Overloading )
Posted in Java on June 28, 2012
Overloading is nothing but defining different behavior ( functionality ) for same function.
for example u can create add method which will add two ints, three ints, two doubles, three doubles.
it is not possible to define one method again and again with same name but u can do it by changing arguments.
example:
add(int x) { return x++; }
add(int x, int y) { return x+y; }
add(double x, int y) { return ((int)y + x); }
- if u call the method with 1 int it will call the first add method and return incremented value
Example.
int ans =add(3); - if u call the method with 2 ints it will return addition of them
Example.
int ans =add( 3, 4 ); - and with 1 double and 1 int i will return again the sum.
Example.
int ans =add( 3.5, 4 );
This was simple example but we can also overload as many times u want, it will provide very flexibility to the software.
as always doubts invited.
have a nice 1, Peace.
Java Tutorial 8 – Passing objects as argument
Posted in Java on June 28, 2012
first create 1 new class (i have named it Class3 for make it simple to understand)
and add the code to all thre classes as follows
(after copy and pasting press Alt+Shift+f and it will formate everything…)
Class1:
public class Class1 {
public static void main(String[] args) {
//created two objects
Class3 c3ob = new Class3();
Class2 c2ob = new Class2();
//passing object of Class3 to method of Class2
c2ob.basket(c3ob);
}
}
Class2:
public class Class2 {
int i;
public void basket(Class3 receivedfriut){
for(i=0;i<3;i++)
System.out.println(receivedfriut.fruit[i]);
}
}
Class3:
public class Class3 {
String fruit[] ={ “apple”, “banana”, “grapes” };
}
- we created two objects of class2 and class3
- we passed and object of class3 as arguiment of method of class 2
- we defined basket method in class2 which takes object of Class3 and save it in receivedfruit
- we used receivedfruit object and printed the String array.
This is useful when you are dealing with lots of int,doubles and etc. You can make a different class for it and use it in other Classes and methods by passing objects.
inough bla bla bla, Doubts are invited,
Have a nice day, Peace.
Java Tutorial 7 – passing arguments and returning values
Posted in Java on June 28, 2012
Passing arguments between methods is very easy.
lets use class1 and class2 that we created before.
and change class1 to :
public class class1 {
public static void main(String[] args) {
Class2 c2object= new Class2();
c2object.boy(“Yash”);
c2object.girl(“sweety”);
}
}
and Class2 :
public class Class2 {
public void girl(String name){
System.out.println(name + ” is present”);
}
public void boy(String name){
System.out.println(name + ” is present”);
}
}
Here we have changed
public void boy()
to
public void boy(String name)
so now every time we call boy method it takes a string, see we have passed a string in boy(“Yash”) in class 1.
- boy(“yash”) passes a string to boy function.
- this string is cached into the String name in class 2.
- then we used name to print a line.
You can also pass int, double etc. but when you pass an int you must define other int in definition of method to catch that int.
for example:
public static void main(String[] args){
add(12,13);
public static void add(int x, int y){
System.out.println(x+y);
}
}
we have passed two arguments and cached it in int x and y and used it to display sum.
Returning values:
to return value just add one line at bottom of method called “return (variable)”
in above example we can return the sum like this way:
public static void main(String[] args){
int ans;
ans = add( 12, 13 );
System.out.println(ans);
public static int add(int x, int y){
return (x+y);
}
}
what we have done here is , we simply get 2 int in method and returned the addition of them.
now notice that we have changed public static void add(int x, int y) to public static int add(int x, int y)
it is because now our method returns an int.
same way if your method returns double or String method would be public static double add(int x, int y)
and in class 1 we written ans = add( 12, 13 ) here ans is the variable which will store the returned value.
same way if you returns a String you must have String variable to store it instead of int ans.
In next tutorial we will learn how to pass Objects between methods.
Java Tutorial 6 – Multiple class methods
Posted in Java on June 28, 2012
If u want to make more complex and big application You should divide your whole code into different classes.
for example if you are creating a Calculator you can have a class that holds main method and another class which contains all operations like add, sub, multiply etc. and when dealing with GUI you can also have one more class for defining all the GUI elements like buttons, Textfields etc.
(maan plz stop talking…)
- Create 2 classes whatever you want.
- create main method in one class.
- create methods that you want to use in other class.
Like this way
Class1:
Class 2:
- We have created 2 classes one has methods and other will use it.
- class 1 has main method that will be run first and class 2 has methods that we will use in class 1
- class 2 has 2 methods boy() and girl()
- Now to access method of class2 we created object of class2 in class 1 that is c2object.
- now only we have to do is to refer method of class 2 bye calling object.method() means if u want to use girl() then c2object.girl().
- you can name object anything you want.
- if you have an int in class2, you can also use it by referring c2object.intname
Practice more and implement different logic to improve you speed and accuracy.
Have a nice day, Peace.
Java Tutorial 5 – Creating methods
Posted in Java on June 28, 2012
We have used system defined methods like println() and nextInt(), Now let’s create our own method and use it.
Defining method is very useful when bunch of code is getting repeated, just put that code in a method and just call that method instead of writing all the code again
create a class and add this code as always we do.
- Why int and Scanner before main method?
if we create int inside the main method, then, only main method can use or change that int. so defining anything in the class and outside any method is called Global declaration.
- Why Static?
to use that int into the main or any other method because main itself is static so any variable used within it must be static and thats why we also crated all the methods static.
We created 3 methods and it can be defined anywhere in the class.Observe the out put and you will dig out everything.
u can create different methods of adding subtracting etc. and create calc.
Doubts invited,
Have a nice day, Peace.
Java Tutorial 4 – Looping
Posted in Java on June 28, 2012
I think you guys can do following:
- create new class
- now add following lines in main method
no need to import anything because we are not using scanner here.
Syntax:
while ( condition ) {
….to do …
}
it will first check the condition and if condition is true it run the code within the {} and again check the condition, this will continue till condition will not become false. Once it become false loop is ended.
we created here an int i and printed 1 line each time it loops.
and If u know C/C++ its going to boring for you ! because its same.
Again Doubts and suggestions are invited.
Peace.
Java Tutorial 3 – If Else Statements
Posted in Java on June 28, 2012
If else is exactly as same as If else in C/C++.
Lets do this in New Class to create a new Class don’t create new project. click on package above your class and select new –> java class and give name to the class anything you want then hit enter.
Add this code to new created class and you might get idea.
- create main() method.
- defined an int and a Scanner object for user input (refer to previous tutorial)
Here i have not used {} after if() because if there is only 1 line in the if(){ … } it is optional to do {}.
you can try it by removing {} from else also.
You can add more den 1 condition in if statement by separating them with &&(and) ||(or)
else if() is also the same and u can try it at your own.
(what the fridge ! is it Home work !)
Again Doubts are invited.
Have a nice 1, Peace.
Java Tutorial 2 – User Input and Maths
Posted in Java on June 27, 2012
First add this code:
Java is case sensitive so take care while typing.
How it works:
to get input from user we need to create a Scanner object that takes input.
We created Scanner object named “input”, you ca name it anything you like (eg. popcorn, bitch).
Now we created 3 doubles “x , y and ans”. Double is simply a variable that hold numeric value with decimal point.
We added System.out.println() lines to make it interactive.
in line “x= input.nextDouble();” we used input object and it’s “nextDouble()” method which returns a double and we stored it in the x.
We did same for variable y.
You can see suggestions while typing so you can find out other methods like nextInt() etc…
now understand this, System.out.println(“x+y =” + (x+y));
here “x+y =” is just a string but (x+y) is a value.
( confused..)
Any string in Java must be written in “…” and more than one strings can be printed together as “String 1” + “String 2”
and if u want to print string, int, double together simply saparet them with +.
for eg. If u want to print “your age is 10” and 10 is stored in variable x, you can do this by
System.out.println(“your age is ” + x ) and it will print the same.
And ya if you run it, it will run your previous class, but to run this class click shift+F6.
Thats it for this tutorial. Doubts are welcomed.
have a good 1, Peace.






