program using method and method overloading in java

22:36 Unknown 0 Comments




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:


0 comments:

print pyramid pattern in java. Example 2

21:48 Unknown 0 Comments

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:






0 comments:

print pyramid triangle in java

21:06 Unknown 0 Comments

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:



0 comments:

how to print pyramid pattern in java ? Example

22:16 Unknown 0 Comments

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.

0 comments:

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

21:27 Unknown 0 Comments

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:


0 comments:

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

20:48 Unknown 0 Comments

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:

0 comments:

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

20:57 Unknown 0 Comments

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 :



0 comments:

program to check given age is kids, Teen ager , Adult or senior citizen

22:56 Unknown 0 Comments

Program to check if user enter age then print you are kid or Teen ager or Adult or senior citizen. In this program used  && (logical And) operator and if else control Structures.

// program to check given age is kids, Teen ager , Adult or senior citizen


import java.util.Scanner;
class Agecheck{
                public static void main(String args[]){
                Scanner sc = new Scanner(System.in);
                System.out.println("Plz Enter age:");
                int ag = sc.nextInt();
                if(ag<10){
                                System.out.println("you are kid.");
                }
                else if (ag>=10&&ag<=17){
                                System.out.println("you are Teen ager");
                }
                else if(ag>=18&&ag<=55){
                                 System.out.println("you are Adult");
                }
                else
                                System.out.println("you are senior citizen");
                }
}






Save program with .java extension: program_name.java
 Program_name always should main class name

For compile :
Open cmd and goto your file and type command
Javac Agecheck.java

For Run:
Java Agecheck
Output:


0 comments:

Program to print argument length in java

22:40 Unknown 0 Comments

Below given program Show the number of arguments that how many runtime arguments you passed. Only use args.length.

//program to print argument length

class Arg_length{
                public static void main(String args[]){
                               System.out.println(args.length);
                }
}

Save program with .java extension: program_name.java
 Program_name always should main class name

For compile :
Open cmd and goto your file and type command
Javac Arg_length.java

For Run:
Java Arg_length hi hello

Output:


Here you can show only number of Arguments. If you want to print Arguments then use args[0] + args[1] and so on.

//program to print Arguments length with print Arguments.

 class Arg_length{
                public static void main(String args[]){
System.out.println("\n\t Hello Everyone!!\n Following are the value of "+args.length+ "  RunTime \n\n1)"+args[0]+"\n2)"+args[1]);
                }
}

Save program with .java extension: program_name.java
 Program_name always should main class name

For compile :
Open cmd and goto your file and type command
Javac Arg_length.java

For Run:
Java Arg_length hi friends

Output:

0 comments:

e gift