Showing posts with label SYCS Practicals. Show all posts
Showing posts with label SYCS Practicals. Show all posts

Monday, 18 February 2013


Program to input numbers from user using array and to calculate their Sum

Code:
/**
 * Program to input numbers from user using array and to calculate their Sum.
 **/
import java.io.*;
public class Arry_Input
{
    public static void main()throws IOException
    {
        try
        {
            int sum =0;
         int ary[] = new int[3];
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         for(int i=0;i<3;i++)
         {
             System.out.print("Enter number "+(i+1)+" ==> ");
             ary[i] = Integer.parseInt(br.readLine());
         }
         for(int i1=0; i1<3; i1++)
         {
            sum = sum+ ary[i1];
         }
         System.out.print("Sum of  numbers ==> "+sum);
    }
    catch(Exception z)
    {
        System.out.println(z.toString());
    }
}
}

OUTPUT
Enter number 1 ==> 10
Enter number 2 ==> 20
Enter number 3 ==> 30
Sum of numbers ==> 60
Prof.Mahendra Kanojia

Program to demonstrate Linear Search Using Arrays

Code:
/**
 * Program to demonstrate Linear Search Using Arrays.
 **/
import java.io.*;
public class Arry_Search
{
     public static void main()throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       System.out.print("Enter the number of elements you want to input ==> ");
       int size = Integer.parseInt(br.readLine());
        int arry[] = new int[size];
        int flag = 0, loc = 0;
        for(int i=0;i<arry.length;i++)
         {
             System.out.print("Enter number "+(i+1)+" ==> ");
             arry[i] = Integer.parseInt(br.readLine());
         }
        System.out.print("Enter number to be searched ==> ");
        int num = Integer.parseInt(br.readLine());
        for(int i = 0; i < arry.length ;i++ )
        {
            if (arry[i] == num)
            {
                flag = 1;
                loc = i;
                break;
            }
        }
        if (flag == 1)
        {
            System.out.println("Number "+num+" found in "+(loc+1)+" location");
        }
        else
        {
            System.out.println("Number not found ");
        }
    }
}

OUTPUT
Enter the number of elements you want to input ==> 4
Enter number 1 ==> 10
Enter number 2 ==> 20
Enter number 3 ==> 30
Enter number 4 ==> 40
Enter number to be searched ==> 30
Number 30 found in 3 location
Prof.Mahendra Kanojia

Friday, 18 January 2013


Program to Demonstrate Typecasting in Java (BlueJ)

Code:
/***** PROGRAM TO DEMONSTRATE TYPECASTING *****/
public class TypeCast
{
    public static void main()
    {
        System.out.println("***** Type Cast Demo *****");
        int i = 10;
        float f = 10.10f;
        double d = 12.12;
        char c = 'a';
        boolean b = true;
       
        int res1 = (int) f; // EXPLICIT TYPE CASTING
        System.out.println("type cast float 10.10 to int ==> "+res1);
        System.out.println("Type cast char "+ c + " to int ==> "+(int)c);
        double resd = i+f; // IMPLICIT TYPE CASTING
        System.out.println("type cast addition of "+i+ " + " + f + " = "+resd);
       
     
    }
}

OUTPUT:
***** Type Cast Demo *****
type cast float 10.10 to int ==> 10
Type cast char a to int ==> 97
type cast addition of 10 + 10.1 = 20.100000381469727
Prof.Mahendra Kanojia

Program to Demonstrate Call by Value and Call by Reference in Java (BlueJ)

A) Program to Demonstrate Call by Reference
code:
/***** PROGRAM TO DEMONSTRATE CALL BY REFERENCE *****/
public class CallByRef
{
   public static  int num1 = 100, num2 = 200;
    public static void main()
    {
         CallByRef cbr = new CallByRef();
        
        System.out.println("***** Program to demonstrate call by reference *****");
        System.out.println("Value of num1 before call to swap :"+cbr.num1);
        System.out.println("Value of num2 before call to swap :"+cbr.num2);
        System.out.println("***********************************************");
      
        swap(cbr); // calling swap method by reference
       
        System.out.println("Value of num1 after call to swap :"+cbr.num1);
        System.out.println("Value of num2 after call to swap :"+cbr.num2);
        System.out.println("***********************************************");
    }
    public static void swap (CallByRef cbr2)
    {
        int temp;
        temp = cbr2.num1;
        cbr2.num1 = cbr2.num2;
        cbr2.num2 = temp;
       
        System.out.println("Value of num1 in swap :"+cbr2.num1);
        System.out.println("Value of num2 in swap :"+cbr2.num2);
        System.out.println("***********************************************");
    }
}

OUTPUT:
***** Program to demonstrate call by value *****
Value of num1 before call to swap :100
Value of num2 before call to swap :200
***********************************************
Value of num1 in swap :200
Value of num2 in swap :100
***********************************************
Value of num1 after call to swap :200
Value of num2 after call to swap :100
***********************************************

A) Program to Demonstrate Call by Value

Code: 
/***** PROGRAM TO DEMONSTRATE CALL BY VALUE *****/
public class CallByVal
{
    public static void main()
    {
        int num1 = 100, num2 = 200;
        System.out.println("***** Program to demonstrate call by value *****");
        System.out.println("Value of num1 before call to swap :"+num1);
        System.out.println("Value of num2 before call to swap :"+num2);
       
        swap(num1, num2); // calling swap method by value
       
        System.out.println("Value of num1 after call to swap :"+num1);
        System.out.println("Value of num2 after call to swap :"+num2);
    }
    public static void swap (int num11,int num22)
    {
        int temp;
        temp = num11;
        num11 = num22;
        num22 = temp;
       
        System.out.println("Value of num1 in swap :"+num11);
        System.out.println("Value of num2 in swap :"+num22);
       
    }
   
}

OUTPUT:
***** Program to demonstrate call by value *****
Value of num1 before call to swap :100
Value of num2 before call to swap :200
Value of num1 in swap :200
Value of num2 in swap :100
Value of num1 after call to swap :100
Value of num2 after call to swap :200
Prof.Mahendra Kanojia

Program to Demonstrate Method Overloading in Java (BlueJ)

Code:
/***** PROGRAM TO DEMONSTRATE METHOD OVERLOADING *****/
public class methodOverloading
{
    public static void main()
    {
        int num1 = 10, num2 = 20;
        float num3 = 10.10f;
       
        methodOverloading call = new methodOverloading();
       
        call.add(num1, num2);
        call.add(num1, num2, 30);
        methodOverloading.add(num1, num3);
        add(30.30f, num2);
       
    }
    public static void add(int no1 , int no2)
    {
        System.out.println("Addition of "+no1+ " and "+no2+ " ==> "+(no1+no2));
    }
    public static void add(int no1 , float no2)
    {
        System.out.println("Addition of "+no1+ " and "+no2+ " ==> "+(no1+no2));
    }
    public static void add(float no1 , int no2)
    {
        System.out.println("Addition of "+no1+ " and "+no2+ " ==> "+(no1+no2));
    }
    public static void add(int no1 , int no2, int no3)
    {
        System.out.println("Addition of "+no1+ " + "+no2+ " + "+no3+" ==> "+(no1+no2+no3));
    }
   
}

OUTPUT:
Addition of 10 and 20 ==> 30
Addition of 10 + 20 + 30 ==> 60
Addition of 10 and 10.1 ==> 20.1
Addition of 30.3 and 20 ==> 50.3
Prof.Mahendra Kanojia

Program to Demonstrate Return Type, Ternary Operator , Call by Reference and Odd Even Check in Java (BlueJ)
Code:
/ ***** PROGRAM TO DEMONSTRATE RETURN TYPE, TERNARY OPERATOR , CALL BY REFERENCE AND ODD EVEN CHECK ******/

public class ReturnDemo
{
     public static int num = 101;
    
    public static void checkOddEven()
    {
         ReturnDemo rd = new ReturnDemo();// object creation
        
      String rply = rd.check(rd);
      System.out.println(rd.num+ " is a "+rply+" number");
    }
    public static String check(ReturnDemo rd)
    {
        // Using ternery operator
        String ans = (rd.num % 2 == 0)? "EVEN" : "ODD";
        return ans;
    }
}

OUTPUT:
101 is a ODD number
Prof.Mahendra Kanojia