Pages

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..

do while loop (java)

do while loop

This is another type of loop ,In which a statement in the body is executed before any condition. And as we can see that clearly from the name as well, In the while loop & do while loop one major different is ...
In while loop compiler 1st checks the condition of a while loop
But in do while loop body is executed one time always before any condition.



The syntax of do while loop is as follow



  do{
   1 body
     
 2  incrementation
     }


     3 while(condition);



Example 1 : 

write down a program and print out assalam-o-alaikum 10 times



    /*variable intitiallization*/

int i = 1;

    do{
      
     /*out put*/
      
System.out.println("Assalam-o-alaikum");
 
   /*incrementation*/
    i = i + 1;
    }
   
    /*condition*/
   

    while(i <= 10);



Out put

Assalam-o-alaikum
Assalam-o-alaikum
Assalam-o-alaikum
Assalam-o-alaikum
Assalam-o-alaikum
Assalam-o-alaikum
Assalam-o-alaikum
Assalam-o-alaikum
Assalam-o-alaikum
Assalam-o-alaikum



Explanation of Example : 1




1 . In the do while loop the compiler first check the body what ever in the body is , it executes essentially. In the above example Assalam-o-alaikum executed for one time without check any variabe any condition.

2. After wards it goes and increments the value you initiallized before the do body.In the above example it changed to 2.

3. After incrementation it checks wether the condition we have in the while is true or not if its true.Compiler goes back to body and executes body statements.

4.And this proccess is in progress until the given condition become false.
In the above example the condition became false when the value of i reached to 11.






Example 2 :

write down a program in do while loop take name in the input from the user and print the name back to the user . repeat this proccess for five users



package doWhileLoop;

import java.util.Scanner;

public class main {
    public static void main(String[]args){
 
/*variable intitiallization*/
Scanner scan = new Scanner(System.in);
int i = 1;
String  j;
    do{
      
    
      
   System.out.println("Enter your name");
   j = scan.nextLine();
   System.out.println("Your name is " +j);

    i = i + 1;
    }
   
    /*condition*/
   
    while(i <= 5);
}
   

}



Out put

Enter your name
riaz
Your name is riaz
Enter your name
waseem
Your name is waseem
Enter your name
rafeeq
Your name is rafeeq
Enter your name
talha
Your name is talha
Enter your name
abdul wahaab
Your name is abdul wahaab