program using method and method overloading in java




Method describe behavior of an object. A method is a collection of statements that are group together to perform an operation.
There are three step to define Method:
1)    Declaration-->   return_type  method_name()
2)    Define       -->        {    }
3)    Call           -->    call with object


Syntax:
return_type  method_name(){
            // body of method
}
Declaration of method:
void java_pivot(){
            //body of method
            System.out.println(“ I am java pivot method”);
}
 //program to add two no. using method

public class Methodadd {
    void add(int a,int b){
        int c= a+b;
        System.out.println(c);
    }
    public static void main(String[] args) {
        Methodadd ad = new Methodadd();
        ad.add(10, 12);
    }
   
}

Output:



print pyramid pattern in java. Example 2

If you want print pattern like given below:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15


//program to print pyramid pattern 

package pattern_progrm;
public class Pattern_progrm {
    public static void main(String[] args) {
        int i,j;
        int sum ;
        for(i=1;i<=5;i++){
            sum=i;
            for(j=1;j<=i;j++){ 
                System.out.print(" "+sum);
                sum+=5-j;
            }
            System.out.println();
        }

}

output:







print pyramid triangle in java

Pattern based exercises are a better  way to understand  nested loops in Java. There are many pattern based exercises and one of them is printing Pyramid pattern code as shown below:



// program to print Pyramid pattern in java

class Pyramid1{
                public static void main(String rgs[]){
                                int i, j,k;
                                for(i=1;i<=5;i++){
                                                for(j=5; j>=i;j--){
                                                                System.out.print(" ");
                                                }
                                                for(k=1;k<=i;k++){
                                                                System.out.print("* ");
                                                }
                                                System.out.println();
                                }
                }
}

output:




how to print pyramid pattern in java ? Example

Pattern based exercises are a better  way to understand  nested loops use in Java. There are many pattern based exercises and one of them is printing Pyramid Pattern code as shown below:
*  
* * 
* * * 
* * * * 
* * * * *
//program to print pyramid pattern in java

class Pattern1{
                public static void main(String[] jawed){
                                int i,j;
                                for(i=1;i<=5;i++){
                                                for(j=1;j<=i;j++){
                                                 System.out.print(i);      
                                                }
                                                System.out.println();
                                }
                }
}

Output:

Above given program , if print j then what will be the output:
Let’s see,


class Pattern1{
                public static void main(String[] jawed){
                                int i,j;
                                for(i=1;i<=5;i++){
                                                for(j=1;j<=i;j++){
                                                 System.out.print(j);      
                                                }
                                                System.out.println();
                                }
                }
}
Output:

If want print reverse Pattern then below show the code

class Pattern1_rev{
                public static void main(String[] jawed){
                                int i,j;
                                for(i=5;i>=1;i--){
                                                for(j=1;j<=i;j++){
                                                 System.out.print(i);      
                                                }
                                                System.out.println();
                                }
                }
}

Output:


That's all about how to print Pyramid using Java pattern.  Next differ pattern show in next post:
Hope you like this post
Plz share this!!! And have ou any query then write in comment.


program to call private member in different class using setter getter concept

If you declare private member in a class and want to call different class then you can you use setter getter concept.
Below the program mention using setter getter method.
Create multi.java class

package add_returnvalue;
public class multi {
    private int a;
    private int b;
    public void setA(int a){
      this.a= a;
    }
    public int getA(){
        return a;
    }
    public void setB(int b){
        this.b= b;
    }
    public int getB(){
        return b;
    }  
}

create main class multi_mainclass.java
in this class calling the private member from the other class

package add_returnvalue;
public class multi_mainclass {
    public static void main(String[] args) {
    multi ml = new multi();
    ml.setA(20);
    ml.setB(10);
    System.out.println(ml.getA()*ml.getB());
}
}


Output:



program to Add of two no. using method seter geter in java

package add_returnvalue;
import java.util.Scanner;
public class Add_returnvalue {
    int a=0,b=0;
                public void setA(int c)
                {
                                a=c;       
                }
                public int getA(){
                                return a;
                }
                public void setB(int d)
                {
                                b=d;      
                }
                public int getB(){
                                return b;
                }
                public int sum(){
                                int s=0;
                                s=a+b; 
                                return s;
    }
    public static void main(String[] args) {
        // TODO code application logic here
       Scanner obj= new Scanner(System.in);
                                Add_returnvalue d1= new Add_returnvalue();
                                System.out.println(" this program for Add of 2 no. plz enter a and b value:");
                                int a= obj.nextInt();
                                int b= obj.nextInt();
                                d1.setA(a);
                                d1.setB(b);
                                System.out.println("Value of A is :"+d1.getA());
                                System.out.println("Value of B is :"+d1.getB());
                                System.out.println("Add of 2 values is:"+d1.sum());       
    }
}

output:


If you declare private member in a class and want to call different class then you can you setter getter concept.
Below the program mention using setter getter method.
Create multi.java class

package add_returnvalue;
public class multi {
    private int a;
    private int b;
    public void setA(int a){
      this.a= a;
    }
    public int getA(){
        return a;
    }
    public void setB(int b){
        this.b= b;
    }
    public int getB(){
        return b;
    }  
}
create main class multi_mainclass.java
in this class calling the private member from the other class

package add_returnvalue;
public class multi_mainclass {
    public static void main(String[] args) {
    multi ml = new multi();
    ml.setA(20);
    ml.setB(10);
    System.out.println(ml.getA()*ml.getB());
}
}


Output:


program to show given no. is odd or even in java

let,s discuss on even or odd number, if you enter odd number like 1, 3,5, 7........... then show the result is "given no. is odd", or you enter number like- 2,4,6,8........... then show result is "given no. is even".
below the mention program to show given no is odd or even. i suggest you that you can program in netbeans.

// program to show given no. is odd or even in java

package odd;

import java.util.Scanner;

public class Odd {

//param args the command line arguments

    public static void main(String[] args) {

        // TODO code application logic here

        int i;
        Scanner s = new Scanner(System.in);
        System.out.println("enter n value:");
        int n =s.nextInt();
       
            if(n%2==0)
                System.out.println("given no is even");
        else
                System.out.println("given no is odd");
    }  
}


Output :




e gift