Thursday, November 28, 2013

Program using if - else if - else construct




Q- 1.
Input number of calls, calculate and print the bill.

          For first 50 calls               0
          For next 150 calls             1 Re/Call
          For next 150 calls             .75 p/Call
          For next 150 calls             .60 p/Call
          Over 500 calls                  .5 p/Call
          Add : Rs. 180 as rent and 12.05% as service tax.



//Telephone Bill
import java.io.*;
public class Elec_Bill
    {
        public static void main(String args[])throws IOException
            {
                int call;
                double bill=0;
                BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("Enter no of calls : ");
                call=Integer.parseInt(Br.readLine());
                if(call<=50)
                    {
                        bill=180;
                    }
                else if(call<=200)
                    {
                        bill=180+(call-50)*1 ;
                    }
                else if(call<=350)
                    {
                        bill=180+(100*1)+(call-150)*0.75 ;
                    }
                else if(call<=500)
                    {
                        bill=180+(100*1)+(150*0.75)+(call-350)*0.6 ;
                    }
                else
                    {
                         bill=180+(100*1)+(150*0.75)+(150*0.6)+(call-500)*0.5;
                    }      
                bill=bill+(bill*12.05/100.0);
                System.out.println("\n\tThe Telephone bill is Rs."+bill);
        }
    }



Q-2.
        a) Largest of two number
        b) Largest of three Number

//largest of three using logical operator
import java.io.*;
public class Large
    {
        public static void main(String args[])throws IOException
            {
                int a,b,c;
                BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("Enter First No. : ");
                a=Integer.parseInt(Br.readLine());
                System.out.print("Enter Second No. : ");
                b=Integer.parseInt(Br.readLine());
                System.out.print("Enter Third No. : ");
                c=Integer.parseInt(Br.readLine());               
                Large2(a,b);
                Large3(a,b,c);

        }
        public static void Large2(int a, int b)
            {
                if(a>b)
                    {
                        System.out.println("\n\t"+a+" is the largest");
                    }
                else
                    {
                        System.out.println("\n\t"+b+" is the largest");
                    }
            }
        public static void Large3(int a, int b,int c)
            {
                if(a>b && a>c)
                    {
                        System.out.println("\n\t"+a+" is the largest");
                    }
                else if(b>c)
                    {
                        System.out.println("\n\t"+b+" is the largest");
                    }
                else
                    {
                        System.out.println("\n\t"+c+" is the largest");;
                    }           
            }       

    }


Program using ternary operator

Q-1.
          a) Largest of two number


    public class Ternary_Operator
        {
            public static void main(String args[])
                {
                    int a=10,b=20;
                    System.out.println("The largest between a and b ("+a+","+b+") = "+(a>b?a:b));
                }
            }



          b) Largest of three Number


    import java.io.*;   
    public class Ternary_Operator_1
        {
            public static void main(String args[])throws IOException
                {
                    int a=10,b=20,c=5;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("Enter First No. : ");
                    a=Integer.parseInt(Br.readLine());
                    System.out.print("Enter Second No. : ");
                    b=Integer.parseInt(Br.readLine());
                    System.out.print("Enter Third No. : ");
                    c=Integer.parseInt(Br.readLine());               
                    System.out.println("The largest between a and b ("+a+","+b+","+c+") = "+(a>b?(a>c? a : c):(b>c?b:c)));
                }
            }

Thursday, November 21, 2013

User Defined Function




Java provides vast numbers of ready to use built-in functions that can be utilize to solve math, string and other problems. Most of java built-in functions are imported from the default package java.lang. But as a programmer we too can write user defined packages and user defined functions. UDF (user defined function) make the programs easier to understand, easy to debug and write.

Java program execution start from main function and any other User defined function would execute if it called. When a UDF is called, it transfer control to the UDF from caller method. While calling a function, we may or may not send some parameter and the called function may or may not return any data. While calling a method the parameter passed is known as actual parameter and the parameter mention in called function is known as formal parameter.

User defined function has three parts:

 Method signature, method name and the parameters.

Example :

//Without parameter and return statement

import java.io.*;
    public class UDF_1
        {
            public static void main(String[] args) throws IOException
                {
                    Sum();
                }


       public static void Sum() throws IOException
                {
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    int a,b;
                    System.out.println("Enter two No ");
                    a=Integer.parseInt(br.readLine());
                    b=Integer.parseInt(br.readLine());                   
                    System.out.println("\n\tSum of : "+a+" and "+b+" is = "+(a+b));
                }
      }               




//Calling function with Parameters

import java.io.*;
    public class UDF_2
        {
            public static void main(String[] args) throws IOException
                {
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    int a,b;
                    System.out.println("Enter two No ");
                    a=Integer.parseInt(br.readLine());
                    b=Integer.parseInt(br.readLine());                      
                    Sum(a,b);
                }


       public static void Sum(int x, int y) throws IOException
                {
                 
                    System.out.println("\n\tSum of : "+x+" and "+y+" is = "+(x+y));
                }
      }               



//With return statement

import java.io.*;
    public class UDF_3
        {
            public static void main(String[] args) throws IOException
                {
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    int a,b;
                    System.out.println("Enter two No ");
                    a=Integer.parseInt(br.readLine());
                    b=Integer.parseInt(br.readLine());    
                    //calling sum() and prinitng the value returned by the sum() function
                    System.out.println("\n\tSum of : "+a+" and "+b+" is = "+Sum(a,b));
                }
                //return type is integer
       public static int Sum(int x, int y) throws IOException
                {
                 
                    return(x+y);
                }
      }               





Thursday, November 14, 2013

Loop in Java



A loop is a sequence of instructions which is continually repeated a part of a program until the given condition is satisfied. Often when the given condition failed to satisfy either intentional or due to programming error, the loop failed to stop and it is known as infinite loop or unconditional loop.

In java we have three types of Loop for, while, do-while.

For Loop

For loop is combined with three parameters, initialization of loop variable, final value of loop variable and increment or decrement value. 'For' is a entry controlled loop as condition checked at the entry point and also known as incremental loop.

Syntax:
     for(int i=1;i<=10;i++)

We also can have more than three parameters in for but there will be only a pair of semi-colon before final value and other separated by a comma.

Syntax:

     for(i=1,j=0;i<=10;j++,i++)

infinite loop :

     for(;;);

While Loop

While loop is a conditional loop and it is too is a entry controlled loop. The test condition given in a pair of bracket followed by 'while'. If any initial value for loop variable then it should be given before 'while' and the increment and decrement value should be inside body of the loop.

Syntax:

     int i=0;
     while(i<10)
        {
            //some code;
            i++;
        }

do - while Loop

While and do-while is almost same but do-while is a exit controlled loop as the condition tested at the end of the loop. Therefore, the do-while loop perform at least once no mater whatever is the condition.

Syntax:

     int i=0;
        do
            {
              //some code;
              i++;
            }while(i<10)

For Loop      While Loop   Do-while
for is an entry controlled loop and  is very similar to while loop except that the loop has three parameter for initialization of loop variable, the test condition and the incremental or decrement value. It is also known a incremental loop.
          While loop is a conditional loop and also an entry controlled loop.       Like while loop do-while loop too is a conditional loop but it is an exit-controlled loop.



Jump Statement

In Java we have three jump statements - break, continue, and return. These statements used to transfer control back to a desired part of the program.

Break

Break statement is used to jump out of current loop unless a label is defined next to it.  Break statement also used in switch – case statement.

Syntax:

In a Loop

     for(int i=1;i<=10;i++)
        {
            if(i==5)
              break;  //↓ transfer control out of I loop
        }              

Label

Outer:     //Label Declared
     for(int i=1;i<=10;i++)
        {
            for(j=0;j<=10;j++)
              {
                   if(j==5)
                             break Outer; // transfer control back to the outer loop                                          }
        }

In switch-case

     int c=1;
        switch(c)
        {
            case 1: //some code;
              break;
            case 2://some code;
              break;
        }

Continue

Continue statement is used only within looping statements.  Continue statement is forced the next iteration of the loop. The statements after continue within loop are skipped. Execution starts from the top of the loop.

Syntax

     int i=0;
     for(i=1;i<=10;i++)
     {
        if(i==5)
            continue;    // transfer control to next iteration i.e. i=6;↑
        System.out.println(i);
     }

Return

The return statement is used to return to parent method (caller) from the child (called) method. Return statement terminate executing the method is called and transfer back to the caller function and continue with the remaining statement in the caller function.

     void calledMethod()                            int calledMethod(int var)
        {                                                          {
            //some code;                                       //some code;
            return;                                                  return var;
        }                                                          }


void callerMethod()                                  Void callerMethod()
     {                                                             {
         calledMethod();                                        int var=calledMethod(var);
     }                                                             }