Pages

Thursday, October 20, 2016

OOP , inheritance , abstract , encapsulation and polymorphism

OOP , inheritance , abstract , encapsulation and polymorphism




What does Object oriented progarmming language mean ?


All the languages which include abstract , encapsulation , 
inheritance and polimorphism is called Object oriented progrmming language



abstract class


abstract means to epitmise or to take from so In oop when you want to make a class abstract this cannot be instantiate in any of the class. 
While you can use it and access it using the subclasses of it (In other words all the classes which is inhereted from the super abstract class can be instantiate with
the reference variable of that super class ). 

abstract methods 

when  an abstract method is created  in a super class all the inherited class (subclass) of that class must use  this method. 

In java the rules for abstract method

1. Abstract should be declare in abstract class.
2. must use the keyword abstract 
3. no body could be define with abstract method ie
    
  abstract void student() ; 

4. Inhered class must have similar specifier as it has in
 the super class 
 ie (private , public , protected)

5. All the sub classes have to have this method but being defined
  ie

 public void student(){ ... }


implementation of abstract class in java



suppose we have a class student and we want that 

"to every student have their name put for admission "

 this implementation could be done easily with abstraction 

ie 


abstract class student{


public abstract void student_name(); 

}

class student1 extends student{


//this is abstract method of student classs
public void student_name(){

System.out.println("yasir") ; 

}
}


//second student 

class student2 extends student{



//this is abstract method of student classs

public void student_name(){

System.out.println("Rafiq") ; 

}
}


//third student
class student3 extends student{



//this is abstract method of student classs

public void student_name(){

System.out.println("Rezwaan") ; 
}
}



//main class 

class main{

public static void main(String[]args){

student std1  = new student1() ; 
student std2  = new student2() ; 
student std3  = new student3() ; 


}

}


out put 


yasir
Rafiq
Rezwaan










Encapsulation 




encapsulation means to get or set private member of another class is called encapsulation

 Ie

 suppose we have a student class which include some data about a student


class student{

private String name ; 

public void init(){

name ="Mr abrar Khan" ; 

}

//method to access name variable out side this class
public String getName(){

return name ; 

}

//method to change the value of name from outside classes
public void setName(String name){

this.name= name ; 

}

}

and we have another class From where we want to change the student name or to access the name of the student 

class accessStudent{


public static void main(String[] args){
  
//create object for the student class 

  student std = new student() ; 

String accessName = std.getName() ; 


System.out.println(accessName); 

String change_Name = std.setName("Kamal Cr") ;

System.out.println(change_Name) ; 

}


}


Output :

Mr abrar khan
Kamal Cr



Inheritance 




Inheritance means to get public members of super a class ...

 ie 

suppose we have character ( good / bad )  common in all the student so to implement this using inheritance

class student { 

void character(){

 System.out.println("Good");
}

}


And now if you want to be the character method use for all student so instead of creating this method in all the student classes you just have to inheret this class
in all the student classes 


In order to inherit a class you have to put keyword ( extends ) in java 

ie 




class student1 extends student{



}
class student2 extends student{



}

class student3 extends student{



}


class main{

public static void main(String[]args){

 student1 std1 = new student1(); 
 std1.character() ; 
  student1 std2 = new student2();  
   std2.character() ; 

   student1 std3 = new student3(); 
 std3.character() ; 


}

}







So the out put will be 



good 
good 
good





polymorphism 




In polymorphism we can assign  all the subclass to one super class object (also called Reference variable) 


ie suppose we have some classes inhereted from a super class student 

class student1 extends student{

 public void student(){
    
System.out.println("Jamil Khan") ; 
 }

}

class student2 extends student{

 public void student(){
    
System.out.println("Salman") ; 
 }

}

class student3 extends student{

 public void student(){
    
System.out.println("Iqbal Hussain") ; 
 }

}


class student{


public void student(){

System.out.println("Kamal hassan") ; 
}


}

class main{

public static void main(String[]args){
// here is the point you can assign any subclass to super class object

student std[] = new std[3] ; 

std[1] = new student1(); 
std[2] = new student2() ; 
std[3] = new student3() ; 
for(int i = 0 ; i<std.length ; i++){
  std[i].student(); 
}


}

}




So long story short in polymorphism you don't have to create many object for sub classes  




Monday, October 17, 2016

Speech (my parent)

Honorable Principle ,
 Respected Teachers and my dear School fellows!

 Assalm-O -Allaikum (w.wabaraktuhu)


   I am supposed to be stand here today and 
   talk to you about parents.

   I am unsure of how i am supposed to start talking
   about one of the most important things in my life.

   But i will do my best. I thought to myself
   
    what is parent ? 

    They are more than any one will ever be able to speak.
    I can't began to term what a parent is or what they are
    suppose to be. 
   
    But i can tell you that all of them have shown us love
    and caring to bring us forth in this world .

     Whether young , old , dying, famous , rich or poor.
     Our love to you as parents is unconditional. 




    Respected Audience ! 


   I realized that from the past, Unfortunately , friends come 
   and friends go, Friends change , interests change or ideas
    change . 

   And unfortunately some friends move far away. 

   Our relationship and friendships change with life.

   But the love our parents have for us does not change. 
  No matter what we do , where we go , or what we think our        parents posses an undying love for their children.

  They have brought us up in a world full of obstacles . They passed   us from these obstacles. 

  



  Dear fellows ! 

   No words could  ever repay them for all of the love they have    shown us.

  But I would like to let every parent know that what they do for us is really so appreciated.

   Mom and Dad I love you . 


 Wassalam.


 Thank you !

Linked List , Types of linked list and why we use linked list

Linked List


 Definition : It is that type of lenear data structure where    nodes/ item are connected with one another through pointer. Diagrammatically the connection betwwen two nodes is represented by arrows.

 For example 

 the first nodes of the linked list is called head and the last node of the linked list is called 'tail' .

 Types of Linked List :

  There are three types of Linked List.
  
  1. one way linked list: 
  
  It is a linked list where two adjacent nodes are connected by   one pointer ( Represented by single headed arrow).
  
   
   For example 
  

  in one way linked list each nodes is divided into two parts 
  
  a .(info part) 
  b.(Address part)
  
  The info parts contain the actual data/information 
  and the address part of a node contain the address of the next node .
  
  Note :  the address part of the last node does not contain      any address it contains null address
  
  

2 Circular linked list :

 It is a linked list where the last node is connected back to the first node.

The address part of the last node contain the address of the first node.

For example ....




Multiple Linked List : 

   It is that type of linked list where nodes are connected by more than one pointer /Where two adjacent nodes  is connected by two pointer.
 
For example:



 
Here each nodes has three part 
 
 
1 info part : This part contain the actual data/information 

2 left part :this part contain the address of next node

3 Right part :   This part contain the address                  of succeeding (previous) node. 
 
 
 Why we use Linked List
 
 The most important application of linked list is the creation 
  of file on secondary storage device.




Writter/publisher : Shafiq from university of Abdul wali khan Mardan timergara campus

Saturday, September 17, 2016

Static and dynamic arrays implementation


Static array


an array which doesn't change at compilation  time is called static array


Example


class student {


/* following array is static and can't be changed */


final int static_array[]= new int [20] ;

public void init (){

static_array [0]= 5;
static_array [1]=6;

/*print two elements from static array */

System.out.println (static_array [0] +static_array [1]);

}

public static void main(String [] args){

student std = new student();

std.init ();
}

};

Result :

11

Explanation :

In the above example we have created an array by name (static_array) .

The size of this array is fixed and cannot be changed at compilation time.

How ever we can assign different values to this array indexes.


When an array is initiallized with a fixed size its mean that it is a static array.


Dynamic array :


An array which changes in compilation time is called dynamic array

Example :

class student{

/* first  size of array is 30 */

int dynamic_array [] = new int [30];

public void size1 (){

/* print 1st size */

System.out.println(dynamic_array.length);

}

public void size2(int size){

/* 2nd initialization of dynamic array */

dynamic_array = new int [size] ;


/* print 2nd size */

System.out.println (dynamic_array.length);

}

public static void main (String [] args) {

student std = new student ();

/* call first size method */

std.size1 ();

/* call 2nd method */
/* give array another size ie */

std.size2 (60);

}

};

Result :

30
60

So as we can see we have changed the size of the array.

So its mean , whenever an array changes during compilation time that will be a dynamic array.

So this is quite enough for static and dynamic arrays...i hope you like it..If any problems , right now leave them down in the comments below.And i will try hard to help you out

Java inheritance

 



Inheritance means to access other classes members...

how to inherit a class.


In java to inherit a class a word extends is used to creat a sub class for a super class..


Super class


Also called parent class.or base class..

A class from which other class/classes is derived..

Or

A class which members are used by another classes.is know as super class.


Sub class


Also called child class or derived class.

A class which use the members of a parent class is called derived class
to creare a derived class the following syntax are used.


class class_name extends
base_class {
};


Where

Class: is a keyword to identify this is a class


class_name: this is the name of the derived class
Or inother words the name of the class.

extends: is a keyword used to identify this class is derived..and an inherited class.

base_class: represent the parent.which members will be used / inherited in this class.


Practical example of extends...


A class student_print  is inherited from a parent class student

class student {

public int student_id;
public String name;

};

class student_print extends student{
student_id=30;// student  id is inherited
name="Umair ahmad";  /* name is inherited */

};



class main {
//main method
public static void main (String []args){

student std = new student();

System.out.println (std.student_id);/*student id is printed*/
System.out.println (std.name);/*student name is printed*/

}
};

Friday, September 9, 2016

What is class (java)

Class 
A class is the collection of data and functions,T
The data items and functions are defined within the class.
(data item : variable , constant etc.)
The functions taking task from the data items of a class each function has a unique relation ship with the data item of the class..

Defining a class :
The syntax for defining a class is as follow

class class_name {
body
}
 Class = this is the keyword. By which java compiler know’s that this       is a class.
Class_name : here we write the name of the class, it could be anything you want. But for better organization you can give a name so you can identify the class puposes easily.. for example for the student registration .. the name of the class should be (student_registration )

Body of the class , here we defines  members/data items and functions.

How to access a class
Mean to call the members of a class from another class is called access a class.
To access a class you have  to create an object for that first.

Syntax for object

class_name temporary name = new class_name() ;
where
class_name : represent the class . It should be written same as class name.
temporary name : represent a temporary name so you can use this name in the current class for calling members and function of  the class , For which you have created this object.
new : is the keyword it must be used in creating objects for class.
class_name() = again name of the class with two parenthesis.

For example an object for class (student) would be like this.
student std = new student() ;

If right now you have a class (student) and you want to access it from main class . You will do something like this.
class student{
public int a = 20 ;

};
class main{
//main method
public static void main(String[]args){
// create object for student class
student std = new student() ;

//print a from class student.
System.out.println(std.a) ;
}

};

The result will be like this.
20



If you have  a constructor in the class .. constructor will be discussed later.
Then creating object for only calling constructor is very simple and short.

Syntax for calling constructor.
New class_name() ;

Practical use of calling constructor of a class : to call student class from main class would be like this.

new student() ;


Public members, private members and protected members.
A class actually includes three types of members , but we’ll discussed two of them and the third one would be discussed later down the road.


Public members .
These type of class members can be accessed in the class and outside the class.Note if you don’t put any specifier befor the class member it would be public by default .

Public int id;

So this variable can be access and used in side and out side the class. Ie

salman

Private members.
These type of variable can only be access within the class. You cannot access private variable from outside the class.
To access and use private members of the class you have to have get mehod and set method within that class. Get method and set method will be discussed later ..

class student {
private int id = 3;
}
 class admin{
 public  student name ;
name = new student() ;
System.out.println(name.id);

}

Static :
Static members of a class or static methods of a class can be accessed and used in out side classes freely . the difference between public and static member is for the public member you have to create an object first and then call your member , and for the static member put the name of the class and a dot and then your member.
For example
class student{
// declare and initialize static variable
public static int a = 3 ;
public static int b = 3 ;

//create static methods
Public static void name(){
System.out.println(“your name is khan”);
}

};
 class call{
public static void main(String[]args){
System.out.println(student.a) ;
System.out.println(student.b) ;
Student.name();

}
};
The result will look like this.

1
2
Your name is khan.


Inner classes ;

This is  a type of nested class.
The class which is defined with in the other class is called inner class.

The inner class can accessed freely all the members of the outer class , however the outer class can’t access inner class members without creating an object..



Thursday, September 8, 2016

Add image to jlabel , jpanel or button (java)

Source code is below


Explanation

In java you can add an image to a button or Jlabel with just simple code in this page down is the code for how to add an image to a Jlabel just copy the code and paste to the eclipse and play around it
 
package my_game;
/*dailymotion.com/abdul-wahab65*/
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Display{
public static void main(String[]args){
   
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    /*create an object for Jlabel*/
    JLabel label = new JLabel();
   
    frame.setSize(599,500);
    frame.setVisible(true);
    frame.add(panel);
   
     panel.add(label);
    label.setPreferredSize (new Dimension(599,500));
    /*add an image now
     * copy the path of
     * your file where your
     * file is stored
     */
   
    /*and make sure to
     * type two back
     * slashes
     */
    ImageIconimage = new ImageIcon ("C:\\Users\\abdul\\Desktop\\Dekstop\\New folder\\aaa.jpg");
       /*add your image to
        * the label
        */
     label.setIcon(image);
}
}

create radio button java

Explanation

In java programming some times you have multiple choice in something and you have to select true of them or false of them..so to do that here comes JRadioButton....JRadioButton help you in choosing something in java.So in this page i am going to show you how to create and add them into Java JFrame or JPanel
package myPackage;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class myClass extends JFrame 
             implements ActionListener{
   
  /*create JPanel*/
  JPanel panel = new JPanel();
  
   
     /*create RadioButtons here*/
  
 JRadioButton button =new JRadioButton();
 JRadioButton button1=new JRadioButton();
 JRadioButton button2=new JRadioButton();
 JRadioButton button3=new JRadioButton();
  
 public myClass(){
   setSize(400,600);
  setDefaultCloseOperation
  (JFrame.EXIT_ON_CLOSE);
     setVisible(true);
  setTitle("Radio Button");
  add(panel);
  radioButton();
 
 }
    
 
 public void radioButton(){
  
   
     /*add all buttons to 
      * the panel */
 
     panel.add(button);
     panel.add(button1);   
     panel.add(button2);
     panel.add(button3);
    
  
     /*add text on your button*/ 
     button.setText("apple");
     button1.setText("guava");
     button2.setText("banana");
     button3.setText("orange");
  
 
     /*select them false*/
     button.setSelected(false);
     button1.setSelected(false);
     button2.setSelected(false);
     button3.setSelected(false);
  
   
     /*now add actionListener*/
     button.addActionListener(this);
     button1.addActionListener(this);
     button2.addActionListener(this);
     button3.addActionListener(this);
  
  
  
 }
     
  
/*method for to add actionPerformed*/
 
public void actionPerformed(ActionEvent e){
     Object source = e.getSource();
     
    
 /*actionListener for button*/
     
 if(source==button ){
    
     button.setSelected(true);
     button1.setSelected(false);
     button2.setSelected(false);
     button3.setSelected(false);
 
  
    /*only button to be true*/
 }
 
  
     /*actionListener for button1 */ 
      
 if(source==button1  ){
      
     button.setSelected(false);
     button1.setSelected(true);
     button2.setSelected(false);
     button3.setSelected(false);
   
    
 /*only button1 to be true*/
   }
 
 
 
  /*actionListener for button2*/ 

 if(source==button2){
 
     button.setSelected(false);
     button1.setSelected(false);
     button2.setSelected(true);
     button3.setSelected(false);
   
   
 /*only button2 to be true*/
   }
  
  
   
    /*actionListener for button3*/ 

  if(source==button3){
      
     button.setSelected(false);
     button1.setSelected(false);
     button2.setSelected(false);
     button3.setSelected(true);
   
    
    /*only button3 to be true*/
   }
 }
}
And now if your run this so it would like below

add action listener to java buttons

Explanation

If you will create a button in java so you have to add an action to the button.(action) means if someone click the button you added to the java JPanel/JFrame/canvas/applet So you want that it should to open something with it. below is the source so get them and copy them to the eclipse and practice hard to get the idea
myClass = sorce code of myClass

package myPackage;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class myClass extends JFrame {
   
/*create JPanel*/ 
  JPanel panel = new JPanel();
  
/*create JButton here*/  
 JButton button =new JButton("Click me"); 
 
 
 public myClass(){
   setSize(400,600);
 setDefaultCloseOperation
           (JFrame.EXIT_ON_CLOSE);
     setVisible(true);
     setTitle("simple Button");
  add(panel);
button.setPreferredSize(new Dimension(100,50));
  panel.add(button);


    /*add Action Listener*/
button.addActionListener(new ActionListener(){
   
public void actionPerformed(ActionEvent e) {

/*asign source in an object*/     

 Object source = e.getSource();
  /*add action*/
 if(source==button){
       
 /*print text to the console*/
System.out.println("i am clicked");

 } 
 }
   
   
  });
 
 }  
 
  
}  

myMain = source code of myMain
package myPackage;

 public class  myMain  {
   
   
public static void main(String[]args){
   myClass cl= new myClass();
       
    }
 }


If right now if you have any problem about java programming just contact me throw i have given the contact form in each of my page.So just click on that and leave me a comment or just message me and trust me.i'd love help you out from every difficulties you have..

add button in java


 package  myPackage;
 
 import javax.swing.JButton;
 
 public class myClass{

 JButton button = new JButton("button");
 
 public void myButton(){
 
 panel.add(button);
 
 
 }
 
 }
 
 

how to add textarea in jpanel or frame ( java )

add text area to the panel

this is just a normal text area.that i am going to add with JPanel or JFrame...Source code below
 package myPackage;  

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
 

 
public class myClass {
  
public static void main(String[]args){

JFrame frame = new JFrame();

JPanel panel = new JPanel(); 

/*create JTextArea Object */

JTextArea area = new JTextArea();
 
  
frame.setSize(400,400);
frame.setDefaultCloseOperation
           (JFrame.EXIT_ON_CLOSE);


frame.setVisible(true);
frame.setTitle("java beginner");

frame.add(panel);
 

/*give size first*/
 
area.setPreferredSize(new Dimension(100,30));
 
/*add area to the
  * panel
  */

panel.add(area);
 
 }
}
 
And if you run this.So it would look like this

Stylish area code

now if you wanna make you area a little cooler than this so copy this code and paste it to your project
 
 
 
  
  /*setBackground color*/ 
 area.setBackground(Color.BLACK);
 

 /*setText color of area*/ 
 area.setForeground(Color.white);
  
 
 /*setFont of area*/ 
 Font font =
 new Font("arial",Font.BOLD,22);
 area.setFont(font);
 
 
If right now if you have any problem just contact me i have given the contact form in each of my page.So just click on that and leave me a comment or just message me and trust me.i'd love help you out from every difficulties you have..

add a jpanel to the jframe




Source code




  
package myPackage;

import java.awt.Color;

import javax.swing.JFrame;
/*JPanle import below
 make sure to import 
 */
import javax.swing.JPanel;


public class myClass{
 
 public static void main(String[]args){
  
 JFrame frame = new JFrame();
 /*create a JPanel object Here*/
JPanel panel = new JPanel();
 
frame.setDefaultCloseOperation
          (JFrame.EXIT_ON_CLOSE);
  
frame.setTitle("create Jpanel");
  
frame.setVisible(true);
  
frame.setSize(450, 500);
  
panel.setBackground(Color.BLACK); 

frame.add(panel);
  
 }
 
}

  


And if you run this it would look like


Create a method for JPanel in java and add to the frame..So for this we'll hava two classes a main and a mehthods class. so let's work
this is the main class
package myPackage;
 
public class myMain {
 public void main(String[]args){
  myClass clas = new myClass();
 }
}

mehtods class
package myPackage;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class myClass {
 
/*create a Jframe here*/

JFrame frame = new JFrame();

/*create a JPanel here*/

JPanel panel = new JPanel();

public myClass() {
 frame();
}
public void frame(){
 frame.setDefaultCloseOperation
          (JFrame.EXIT_ON_CLOSE);
 
 frame.setTitle("create Jpanel");
   
 frame.setVisible(true);
   
 frame.setSize(450, 500);
 /*join both together
  * cause we are call 
  * this mehtod in
  * the constructer
  * so call the panel
  *  method
  */
 panel();
}
  public void panel(){
   
 panel.setBackground(Color.black);
frame.add(panel);
  
  }
}

and same result
If right now if you have any problem just contact me i have given the contact form in each of my page.So just click on that and leave me a comment or just message me and trust me.i'd love help you out from every difficulties you have..

how to create a frame in java

Add some text to a label in java

how to add text in java

Today i am gonna be showing you.how to add text in java frame / panel pretty simple ...below is the source code copy them and paste it to eclips...play with it by yourself ..And you will clear i am sure
IN these code this is a whole process and in this we'll do all stuff to be excutable
 
 package myPackage;

import java.awt.Color; 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

 
public class myClass {
  
  
 public static void main(String[]args){
  
JFrame frame = new JFrame();

JPanel panel = new JPanel();

/*create object first */

JLabel label = new JLabel();
 

frame.setSize(400,600);
 
frame.setDefaultCloseOperation
          
   (JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

frame.setTitle("JLabel");

frame.add(panel);

/*add the label into the
* panel*/

panel.add(label);

/*set label text*/

label.setText("who are you?");

/*set background 
  * of label*/
label.setBackground(Color.red);

 }
 
}
 
 
and if you run so it will look like this
Mehtod for the simple JLabel is below
 public void label(){
 
 
 JLabel label = new JLabel();
 
 label.setText("this is a label");
 
 label.setBackground(Color.blue);

 panel.add(label);

 }
 
 
YOu can also create some cool looking border..Source code below
 
 public void smartLabel(){
/*create a label*/
JLabel label = new JLabel();
 
  /*set text of label*/
label.setText("this is a label");
  
  /*set border of 
    label */
Border border =
BorderFactory.createLineBorder(Color.red,2);
label.setBorder(border);

/*setBackground of label*/
label.setBackground(Color.WHITE);

/*set Size of label*/
label.setPreferredSize(new Dimension(44,10));
 }
 
}
 

functions / methods in java

 Explanation

Till now as we know that, in java we have a main method called main function.But in java we also use some codes to do some separate task. So thats why here comes Method.

Difinition

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value. Each method has its own name.
you can use many methods in a simple program.

parts of method

Parts of method mean's, In java method there are some parts ( codes ), Which java method is made.Like modifier, return part,name part,pass value and a body.Method is simply just made of these things.
Method condition
1.A method can't be in main function
2.If a method is an int method so it should has a return statement
3.If a method is void. So it's mean no return value should be use.
4.You can create many methods in a program
5.If a method is private so it's mean it can't be access out of Class
6.if a method is static so you can access it from anywher
7.You can call a Method many times.

Simple out put Method

So now here I am going to show you how to create a method without any return value andpassed value let's have a look at this example
 package myPackage;
 
 public class myClass{
 
 public static void main(String[]args){
  /*main function body*/
 }
 
 /*this is the method*/
 public static void myName(){
    /*I am in the method body*/
  System.out.println("Abdul wahab");
 
 }
 }
  

Example explanation

In this example myName is the name of the function.and in the function body as I print out "abdul wahab" so this means..that this method would just print out Abdul wahab.And now if you run this, So it won't work.Because we haven't called this method yet.So to call this method pretty simple.See this example
 package myPackage;
 
 public static void main(String[]args){
 /*
to call myName method just put the name 
with round braces*/

    myName();
 
}
  static void myName(){
 
 System.out.println("Abdul wahab");
 
 }
 
Now it will show something like this to the console if you are using eclips, As shown below
You can call a method many times its upto you as much as you need.Look at the Example
 package myPackage;
 
 public class myClass{
 
 public static void main(String[]args){
  /*call myName method three times*/
  
  myName();
  myName();
  myName();
 
 }
 static void myName(){
 
 System.out.println("Abdul wahab");
 }
 
 }
And it would show to the console as below

Method with String variable

You can use a variable also in a method.like below I am gonna show you a Stringvariable,How you can print out a String variable in methods.
package myPackage;

public class myClass{

 public static void main(String[]args){
 
 /*call your method right here*/
 
  myName(); 
 }
 static void myName(){
 
 /*create a String variable*/
 String a="my name is abdul wahab";
 
/*print out a String variable*/
 System.out.println(a);
 
 }
}

It would print out something like this.If you are using Eclips

passing a value to void method

Explanation

Passing a value to a void method is mean to put variables in the parameters of a method ie
package myPackage;

public class myClass{

public static void main(String[]args){

}
static void myMethod(int d){
/*Here we've passed an int to our
 method*/
}
static void myName(String name){
/*Here we have passed 
a String to our method*/

}
static void towValues(int height,int width){
/*You can pass many values
to a single method*/

}
}
So as you can see these are some simpleExamples about where we pass values in a method. Iam sure you got the idea!
.
As we are clear about how to passes values in methods.Now this is the time to know how to use them.Why we passes values in a method. I will make this pretty clear with some Examplesbelow
package myPackage;

public class myClass{
 
 public static void main(String[]args){
 
 /*call the method and put a
    number as a parameter*/
 myMethod(5);
 
 /*Call again with different value
 
 myMethod(88);
 }
static void myMethods(int number){
/*print out number variable*/
System.out.println(number);
}
}
What we did here? In the parameter of the method(myMethod).We just created an int variable named it number, .And then we print out the number variable. Then we called this method from the main function.and we passed a value 5 as a parameter. And then we recall the method and this time we passed a new value 88.And now if we run this so it would give us a result as shown below

passing many int variables in methods

You can pass many variable in a single methods.Just put commas in between each variable.ie
package myPackage;

public class myClass{
public static void main(String[]args){

/*call your method and put
 two values as you given a,b*/ 
 
 myMethod(8,8);
}
static void myMethod(int a,int b){

/*Here we passed two variable
let's add them with each other*/

System.out.println(a+b);
}
}
What you need to do, 1.Create a method. 2.pass two int variable a and b. 3.In print out add them. 4.And call them in main,and make sure to give some values other wise it cannot proccess with a and b. I put (8,8). 5.And that set.
And now if you run this so it should be like this

passing String in method

Explanation

You can pass a String variable as well into the parameter of a me thod.Like in below ExampleI am gonna be passing a String variable to the method as a parameter
what you have to do is just you have to put String instead of int ie
package myPackage;
public class myClass{
 public static void main(String[]args){
 
 /*call your method*/
 
 myMethod("abdul wahab");
 
 /*put your name in double quotes
 as a parameter*/
 }
static void myMethod(String name){
/*print out name*/

System.out.println(name);
}
}
What we have done here? We just put a String variable into the method as a parameter,named it number. and then we print out we then called this method from the main function. And we gave value as "abdul wahab".Now if you run this so it would look like the same as shown below

Passing String and int both in a method

You can pass all types of variables an int,String and double etc in a single method.Have a look at the Example below
 package myPackage; 
 public class myClass{
  
 public static void main(String[]arga){
   
    myMethod(String health,int age){
   
   if(health=="good"){
  
  System.out.println("congrats you can come");
  
  /*if statement is in another if statement*/
  
  if(age>33){
     
  System.out.println("you are old");
}
else{
  System.out.println("you are out");
  }
 }
else{
   System.out.println("you can come");
    
   } 
  }
  }
Here we passed two different types of variables in a single method.And it works perfect and fine.So its mean you can pass many types of variable in a single method

Methods with return type

There is another type of method in java programming.In which we get a value back as return.Let's have a look at the example below.While in this example I am gonna be returning an int back to my main function.ie
package myPackage;

public class myClass{
 public static void main(String[]args){
 /*call the method in print out 
 statement*/
 System.out.println(myMethod());
 }
 static int myMethod(){
 /*return 8 as an int*/
 
 return 8;
 }

}
As you saw we created an int method in which we return a value 8 and then we called this return value from the main method and now if we run this so it would print out

Return String method

You can create a String method as well in java programming which will return a value back to you as shown below
package myPackage;
public class myMehtod{
 public static void main(String[]args){
 
 /*call the method*/
 System.out.println(myMethod());
 }
 static String myMethod(){
 
 /*assign the value of String as
 how are you?*/
 
  return "how are you?";
 }
}
I hope it makes scence to you.method is the same.We just put String instead of an int, and if you run this so it would print out

Passing a value to the void method

As i discussed already there are some parts of a method modifier, return type,name of method , parameter list and a body ofcource. Now here we'll go over what a parameter list is and how to use it in a method.So first of all the parameter list can be known as the codes in the round braces.ie
package myPackage;

public class myClass{
 public static void main(String[]args){
 
 }
 static void myMehtod(){
  
  /*after the myMethod the round brases()
  is called parameter list*/
  
 }
}
So now as you got the idea about what a parameter list.Now this is the time for how to use.So for this let see this example first
package myPackage;

public class myClass{
 public static void main(String[]args){
  
  /*call the function 
  and pass in an int value
 myMethod(8);
 }
 /*create an int and name it
 number*/
 static void myMehtod(int number){
 /*print out number*/
 System.out.println(number);
 }

}
After run this is the result for you
So what we did here, we createed a parameter list with an int named number..And then in the method body we printed out the number.And then we called this method up in the main function. But (notice this) make sure.It will take an int value as a parameter.As i put 8 in theexample above.
As well as you can pass two values or many values in a single method ie
 package myPackage;
 
 public class myClass{
 public static void main(String[]args){
 /*call the method and
 pass three values into 
 the method,as you mentioned
 (a,b,c)in the method(myMethod)*/
 
 myMethod(10,4,4);
 
 }
  static void myMethod(int a,int b,int c){
 /*Here we passed three values
 in a single method*/
 
 System.out.println(a+b-c);
 /*
 this will add first two values
 while it will then 
 subtract the last one 
 from thier total*/
 }
 }
 
As a result you will get

Passing value using different data types in a method

You can pass different data types in a singlie method.What this mean is actually if you need a method in which you wanna use all the data types like Stringint and double etc.So yeah let's see an example for to make yourself clear about this.
  package myPackage;
  public class myClass{
  public static void main(String[]args){
  /*call the method
  and give the values*/
  myMethod("your total is ",10,2.8);
  }
  static void myMehtod
    (String a,int b,double c){
  /*Here we used 3
  data types in a single
  method*/
  System.out.println(a+(b-c));
  /*subtract c from b*/
  
  }
  
Now let's run this

Passing values into return type method

To pass a value into return type method is also the same as we did in void but this time the method is change.In this method we'll get return something, to return something put return at the end of your method, otherwise it wont work ,and also put int instead of void..ie
package myPackage;
public class myClass{
public static void main(String args){

/*call the method 
in out put statement
and pass a value as a 
parameter*/

System.out.println(myMethod(8));
}
public static int myMethod(int number){
  
  /*return the number*/
  
  return number;
}
}
Here we have a method,which is obviosly a return type,Now in this method we passed an int variable (name=number) as a parameter.And then we return the number.And in the main method we printed out this method with value 8.Now let's run and see!
You can pass and return two or more than two values as well like below
package myPackage;

public class myClass{
public static void main(String[]args){

/*print out myMethod
and pass two values
for the height and width*/

System.out.println(myMethod(3,9));
}
public static int myMethod(int height,int width){
 
 /*return the multiplication
 of height and width*/
return height*width;
}
}
 
And if you run it would show
So this was a simple example i am sure nothing here to explain.But if you really want to know more about this.leave me a comments below.

Use of Scanner Object in a return type method

Now this is another Example and in this Example am gonna be showing you using of Scanner object in a return type method
 package myPackage;  

import java.util.Scanner; 
public class myClass{
 
 public static void main(String[]args){
 /*create a scanner object*/
 
  Scanner d= new Scanner(System.in);
  System.out.println("types in three numbers")
  
  /*get first input in a*/
  
  int a= d.nextInt();
  
  /*get second input in b*/
  
  int b=d.nextInt();
  
  /*get third input in c*/
  
  int c=d.nextInt();
 
  
  /*now call the method
  and pass all the three values
  you got from the user
  into the parameter*/
  
  myMethod(a,b,c);
 }
 static int myMethod(int a,int b,int c){ 
   
  
  /*use an if statement*/
  
if(a>b && b>c){
 System.out.println("b is bigger");

 }else if(b>a && b>c){

 System.out.println("b is bigger"); 

}else{

System.out.println("c is begger");
}  

/*and now return nothing 
because we don't need
something to return*/

return 0;
 }   

  }
And now if you run this
So yeah that's all for a method....If you have any problem belong to this topic ,or related to java you can leave me a comment and i would answer all your questions in very short time
If right now if you have any problem just contact me i have given the contact form in each of my page.So just click on that and leave me a comment or just message me and trust me.i'd love help you out from every difficulties you have..