Saturday, October 15, 2022

Lab Programs For CS&AIML

 

OBJECT ORIENTED PROGRAMMING THROUGH

JAVA LAB(CSE-2022)

1.      Write a JAVA program to display default value of all primitive data types of JAVA

2.      Write a JAVA program that displays the roots of a quadratic equation ax2+bx+c=0. Calculate the discriminate D and basing on the value of D, describe the nature of roots.

3.      Write a JAVA program to display the Fibonacci sequence.

4.      Write a JAVA program give example for command line arguments.

5.      Write a JAVA program to give the example for ‘this’ operator. And also use ‘this’ keyword as return statement.

6.      Write a JAVA program to demonstrate static variables, methods, and blocks.

7.      Write a JAVA program to search for an element in a given list of elements (linear search).

8.      Write a JAVA program to search for an element in a given list of elements using binary search mechanism.

9.      Write a JAVA program to sort given list of numbers.

10.  Write a JAVA program to sort an array of strings

11.  Write a JAVA program to check whether given string is palindrome or not.

12.  Write a JAVA program to determine the addition of two matrices.

13.  Write a JAVA program to determine multiplication of two matrices.

14.  Write a JAVA program for the following

a.  Example for call by value.      

b. Example for call by reference.

15.  Write a JAVA program that illustrates simple inheritance.

16.  Write a JAVA program that illustrates multi-level inheritance

 

17.  Write a JAVA program demonstrating the difference between method overloading and method overriding.

18.  Write a JAVA program demonstrating the difference between method overloading and constructor overloading.

19.  Write a JAVA program to give the example for ‘super’ keyword.

20.  Write a JAVA program illustrating multiple inheritance using interfaces.

21.  Write a JAVA program to illustrate the concept of final keyword in the program.

22.  Write a JAVA program to create a package named pl and implement this package in ex1 class.

23.  Write a JAVA program to create a package named my pack and import it in circle class.

24.  Write a JAVA program to give a simple example for abstract class.

25.  Write a JAVA program that describes exception handling mechanism.

26.  Write a JAVA program for example of try and catch block. In this check whether the given array size is negative or not.

27.  Write a JAVA program to illustrate sub class exception precedence over base class.

28.  Write a JAVA program for handling of user defined exception by using throw.

29.  Write a JAVA program to illustrate the concept of throws keyword.

30.  Write a JAVA program to illustrate creation of threads using runnable class.(start method start each of the newly created thread. Inside the run method there is sleep() for suspend the thread for 500 milliseconds).

31.  Write a JAVA program to create a class My Thread in this class a constructor, call the base class constructor, using super and starts the thread. The run method of the class starts after this. It can be observed that both main thread and created child thread are executed concurrently

32.  Write a JAVA program to illustrate the concept of thread synchronization.

33.  Write Java program by implementing the concepts of different collections as list, map and set

34.  Write a JAVA program that describes the life cycle of an Applet.

35.  Write a JAVA program to design a laughing baby face.

36.  Write a JAVA program to create a simple calculator.


********************************************************************


 Task No: 1

AIM: Write a JAVA program to display default value of all primitive data types of JAVA.

PROGRAM:

//LabTask1.java

public class LabTask1

{

static boolean val1;

static double val2;

static float val3;

static int val4;

static long val5;

static String val6;

public static void main(String args[])

       {

System.out.println("Default values....");

System.out.println("Boolean default is = " + val1);

System.out.println("Double default is = " + val2);

System.out.println("Float default is = " + val3);

System.out.println("Int default is = " + val4);

System.out.println("Long default is = " + val5);

System.out.println("String deafult is = " + val6);

      }

}



Task No: 2

AIM: Write a JAVA program that displays the roots of a quadratic equation ax2+bx+c=0. Calculate the discriminate D and basing on the value of D, describe the nature of roots.

PROGRAM:

// LabTask2.java

import java.util.Scanner; 

public class LabTask2

public static void main(String args[])  

     { 

         Scanner input=new Scanner(System.in); 

System.out.print("Enter value of a:"); 

double a=input.nextDouble(); 

System.out.print("Enter value of b:"); 

double b=input.nextDouble(); 

System.out.print("Enter value of c:"); 

double c=input.nextDouble(); 

double d=b*b-4.0*a*c; 

if (d>0.0)  

         { 

double r1=(-b+Math.pow(d,0.5))/(2.0*a); 

double r2=(-b-Math.pow(d,0.5))/(2.0*a); 

System.out.println("THE ROOTS ARE "+r1+" AND "+r2); 

          }  

else if (d==0.0)  

         { 

double r1=-b/(2.0*a);

System.out.println("ROOTS ARE REAL AND EQUAL"); 

System.out.println("The root is "+r1); 

       }  

else

      { 

System.out.println("IMAGINARY ROOTS"); 

       }

     }

}


Task No: 3

AIM: Write a JAVA program to display the Fibonacci sequence.

class Fibonacci

{

public static void main(String ar[])

{

int n1=0,n2=1,n3,i,count=10;

System.out.println("fib series:");

System.out.print(n1+" "+n2);

for(i=2;i<=count;i++)

{

n3=n1+n2;

System.out.print("  "+n3);

n1=n2;

n2=n3;

}

}

}

Lab Task No: 4

AIM: Write a JAVA program give example for command line arguments.

PROGRAM:

// LabTask4.java

public class LabTask4

{

public static void main(String args[])

   {

int x=Integer.parseInt(args[0]);

int y=Integer.parseInt(args[1]);

int sum=x+y;

System.out.println("Sum of two numbers is:"+sum);

   }

}

Lab Task No: 5

AIM: Write a JAVA program to give the example for ‘this’ operator. And also use ‘this’ keyword as return statement.

PROGRAM:

//LabTask5

//biggest.java

import  java.util.Scanner;

public class biggest{

private int num1;

private int num2;

public biggest SetValues()

    {

       Scanner sc= new Scanner(System.in);

System.out.print("Enter 'num1' value: ");

int num1=sc.nextInt();

System.out.print("Enter 'num2' value: ");

int num2=sc.nextInt();

       this.num1=num1;

       this.num2=num2;

return this;

    }

public void display()

    {

if(num1>num2)

System.out.print("num1 is bigger");

else

System.out.print("num2 is bigger");

    }

public static void main(String args[])

    {

biggest obj = new biggest();

obj=obj.SetValues();

obj.display();

    }

 }


Lab Task No: 6

AIM: Write a JAVA program to demonstrate static variables, methods, and blocks.

PROGRAM:

// LabTask6.java

public class LabTask6{

static String s="HITLER";

static int x=245;

static int y;

static void fun(int z)

   {

System.out.println("s: "+s);

System.out.println("x= "+x);

System.out.println("y= "+y);

System.out.println("z= "+z);

   }

static

   {

System.out.println("static block is invoked");

     y=x-98;

   }

public static void main(String args[])

   {

fun(10);

   }

}      


Lab Task No: 7

AIM: Write a JAVA program to search for an element in a given list of elements (linear search).

PROGRAM:

// LabTask7.java

import   java.util.Scanner; 

public class LabTask7  

public static void main(String args[]) 

  { 

int arr[],n,search,i; 

    Scanner sc=new Scanner(System.in); 

System.out.println("Enter number of elements: "); 

    n=sc.nextInt();  

arr=new int[n];

System.out.println("Enter "+n+" elements: "); 

for(i=0;i<n;i++) 

arr[i]=sc.nextInt(); 

System.out.println("Enter the value to find: "); 

search=sc.nextInt(); 

for(i=0;i<n;i++) 

    { 

if (arr[i]==search)

      { 

System.out.println(search+" is found at location "+(i+1)); 

break; 

      } 

    } 

if(i==n)

System.out.println(search+" isn't found in array");

  }

}  

Lab Task No: 8

AIM: Write a JAVA program to search for an element in a given list of elements using binary search mechanism.

PROGRAM:

// LabTask8.java

import java.util.Scanner;

public class LabTask8

public static void binarySearch(intarr[],intfirst,intlast,int search)

   { 

int mid=(first+last)/2; 

while(first<=last)

     { 

if(arr[mid]<search )

first=mid+1;    

else if(arr[mid]==search)

       { 

System.out.println("Element "+search+" is found at position "+(mid+1)); 

break; 

    }

else

       {

last=mid-1;

       } 

mid=(first+last)/2; 

     } 

if(first>last) 

System.out.println("Element is not found in array");

   } 

public static void main(String args[])

   {

int  arr[],n,search,i; 

     Scanner sc=new Scanner(System.in); 

System.out.println("Enter number of elements: "); 

     n=sc.nextInt();  

arr=new int[n];

System.out.println("Enter "+n+" elements: "); 

for(i=0;i<n;i++) 

arr[i]=sc.nextInt(); 

System.out.println("Enter the value to find: "); 

search=sc.nextInt();

int first=0;

int last=n-1;   

binarySearch(arr,first,last,search);    

   } 

}

Lab Task No: 9

AIM: Write a JAVA program to sort given list of numbers.

PROGRAM:

//LabTask9.java

import java.util.Scanner;

public class LabTask9

{

public static void main(String args[])

    {

intn,temp;

        Scanner sc=new Scanner(System.in);

System.out.print("Enter array size:");

        n=sc.nextInt();

int a[]=new int[n];

System.out.println("Enter the elements:");

for(int i=0;i<n;i++)

        {

a[i]=sc.nextInt();

        }

for(int i=0;i<n;i++)

        {

for(int j=i+1;j<n;j++)

            {

if(a[i]>a[j])

                {

temp = a[i];

a[i] = a[j];

a[j] = temp;

                }

            }

        }

System.out.print("Sorting The No.s In Ascending Order:");

for(int i=0;i<n;i++)

System.out.print(a[i]+" ");

    }

}

Lab Task No: 10

AIM: Write a JAVA program to sort an array of strings.

PROGRAM:

//LabTask10.java

import java.util.Arrays; 

public class LabTask10 

public static void main(String args[])  

   {   

      String[] Heroes={"Adolf Hitler","BenitoMussolini","JosephStalin","VladimirPutin","Barack Obama","ElonMusk","Nicholas Tesla","SatyaNadella"};  

      //Arrays.sort(Heroes); 

int n=Heroes.length; 

for(int i=0;i<n;i++)  

      { 

for(int j=i+1;j<n;j++)  

         {   

if(Heroes[i].compareTo(Heroes[j])>0)  

            {  

               String temp=Heroes[i]; 

               Heroes[i]=Heroes[j]; 

               Heroes[j]=temp; 

            } 

         } 

      } 

System.out.println(Arrays.toString(Heroes)); 

   } 

Lab Task No: 11

AIM: Write a JAVA program to check whether given string is palindrome or not.

PROGRAM:

//LabTask11.java

import java.util.Scanner;

public class LabTask11

{

public static void main(String args[])

   {

      String str,rev="";

      Scanner sc=new Scanner(System.in);

System.out.println("Enter the string: ");

str=sc.nextLine();

int l=str.length();

for(int i=l-1;i>=0;i--){

rev=rev+str.charAt(i);

      }

if(str.equals(rev))

System.out.println(str+" is a Palindrome");

else

System.out.println(str+" is not a Palindrome");

   }

}


Lab Task No: 12

AIM: Write a JAVA program to determine the addition of two matrices.

PROGRAM:

//LabTask12.java

import java.util.Scanner;

public class LabTask12{

public static void main(String args[])

     {

int x,y;

        Scanner sc=new Scanner(System.in);

System.out.println("Enter no.of rows of a matrix: ");

        x=sc.nextInt();

System.out.println("Enter no.of columns of a matrix: ");

        y=sc.nextInt();

int mat1[][]=new int[x][y];

int mat2[][]=new int[x][y];

int sum[][]=new int[x][y];

System.out.println("Enter elements of 1st matrix: ");

for (int i=0;i<x;i++)

        {

for(int j=0;j<y;j++)

mat1[i][j]=sc.nextInt();

        }

System.out.println("Enter elements of 2nd matrix: ");

for(int i=0;i<x;i++)

        {

for(int j=0;j<y;j++)

mat2[i][j]=sc.nextInt();

        }

for(int i=0;i<x;i++)

        {

for(int j=0;j<y;j++)

sum[i][j]=mat1[i][j]

+mat2[i][j];

        }

System.out.println("Addition of matrices: ");

for(int i=0;i<x;i++)

        {

for(int j=0;j<y;j++)

System.out.print(sum[i][j]+"\t");

System.out.println();

System.out.println();

        }


Lab Task No: 13

AIM: Write a JAVA program to determine multiplication of two matrices.

PROGRAM:

//LabTask13.java

importjava.util.Scanner;

public class LabTask13{

public static void main(String args[])

     {

int n;

        Scanner sc=new Scanner(System.in);

System.out.println("Enter base of matrices: ");

        n=sc.nextInt();

int mat1[][]=new int[n][n];

int mat2[][]=new int[n][n];

intmul[][]=new int[n][n];

System.out.println("Enter elements of 1st matrix: ");

for (int i=0;i<n;i++)

        {

for(int j=0;j<n;j++)

mat1[i][j]=sc.nextInt();

        }

System.out.println("Enter elements of 2nd matrix: ");

for(int i=0;i<n;i++)

        {

for(int j=0;j<n;j++)

mat2[i][j]=sc.nextInt();

        }

for(int i = 0; i < n; i++)

        {

for(int j = 0; j < n; j++)

           {

for(int k = 0; k < n; k++)

mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j];

           }

        }   

System.out.println("Multiplication of matrices: ");

for(int i=0;i<n;i++)

        {

for(int j=0;j<n;j++)

System.out.print(mul[i][j]+"  ");

System.out.println();

System.out.println();

        }

     }

}


Lab Task No: 14

AIM: Write a JAVA program for the following

          a. Example for call by value.

          b. Example for call by reference.

PROGRAM:

//LabTask14a.java

public class LabTask14a

{

public static void main(String[] args)

   {

int a=10;

int b=20;

System.out.println("Values of a and b before the call...");

System.out.println("a="+a+",b="+b);

swap(a,b);

System.out.println("Values of a and b after the call...");

System.out.println("a="+a+",b="+b);

   }

public static void swap(inta,int b)

   {

System.out.println("Inside swap(), before swapping...");

System.out.println("a="+a+",b="+b);

int temp=a;

      a=b;

      b=temp;

System.out.println("Inside swap(), after swapping...");

System.out.println("a="+a+",b="+b);

   }

}


 

__________________________________________________________________________________

//LabTask14b.java

public class LabTask14b

{

inta,b;

public static void change(LabTask14b obj)

   {

obj.a=20;

obj.b=10;

   }

public static void main(String args[])

   {

      LabTask14b obj=new LabTask14b();

obj.a=10;

obj.b=20;

System.out.println("Before changing...");

System.out.println("a="+obj.a+",b="+obj.b);

change(obj);

System.out.println("After changing...");

System.out.println("a="+obj.a+",b="+obj.b);

   }

}

Lab Task No: 15

AIM: Write a JAVA program that illustrates simple inheritance.

PROGRAM:

//LabTask15.java

class Animal{

void eat(){System.out.println("eating...");}

}

class Dog extends Animal{

void bark(){System.out.println("barking...");}

}

public class LabTask15

{

public static void main(String args[])

   {

      Dog d=new Dog();

d.bark();

d.eat();

   }

}


Lab Task No: 16

AIM: Write a JAVA program that illustrates multi-level inheritance.

PROGRAM:

//LabTask16.java

class Animal{

void eat(){System.out.println("eating...");}

}

class Dog extends Animal{

void bark(){System.out.println("barking...");}

}

classBabyDog extends Dog{

void weep(){System.out.println("weeping...");}

}

public class LabTask16

{

public static void main(String args[])

   {

BabyDog d=new BabyDog();

d.weep();

d.bark();

d.eat();

   }

}


Lab Task No: 17

AIM: Write a JAVA program demonstrating the difference between method overloading and method overriding.

PROGRAM:

//LabTask17a.java

//Method Overloading

classMotorBike

{

private String startMethod = "Kick";

public void start()

    {

System.out.println(startMethod+" starting...");

    }

 

public void start(String method)

    {

this.startMethod = method;

System.out.println(startMethod+" starting...");

    }

}

public class LabTask17a

{

public static void main(String args[])

    {

MotorBike b=new MotorBike();

b.start();

b.start("Self");

    }

}

__________________________________________________________________________________

 

//Labtask17b.java

//Method Overriding

classMotorBike

{

public void start()

  {

System.out.println("Using kick paddle to start...");

    }

}

classSelfStartMotorBike extends MotorBike

{

public void start()

    {

System.out.println("Using self start button to start...");

    }

}

public class LabTask17b

{

public static void main(String args[])

    {

SelfStartMotorBike b=new SelfStartMotorBike();

b.start();

    }

}


Lab Task No: 18

AIM: Write a JAVA program demonstrating the difference between method overloading and constructor overloading.

PROGRAM:

//LabTask18a.java

//Method Overloading

classMotorBike

{

private String startMethod = "Kick";

public void start()

    {

System.out.println(startMethod+" starting...");

    }

 

public void start(String method)

    {

this.startMethod = method;

System.out.println(startMethod+" starting...");

    }

}

public class LabTask18a

{

public static void main(String args[])

    {

MotorBike b=new MotorBike();

b.start();

b.start("Self");

    }

}

__________________________________________________________________________________

//LabTask18b

//Constructor Overloading

class LabTask18b

{

   String lang;

LabTask18b()

   {

this.lang="Java";

   }

LabTask18b(String lang)

   {

this.lang=lang;

   }

public void getLang()

   {

System.out.println("Programming Langauage: "+this.lang);

   }

public static void main(String[] args)

   {

     LabTask18b obj1=new LabTask18b();

     LabTask18b obj2=new LabTask18b("C++");

obj1.getLang();

obj2.getLang();

   }

}

Lab Task No: 19

AIM: Write a JAVA program to give the example for ‘super’ keyword.

PROGRAM:

//LabTask19.java

class Animal{

public void animalSound(){System.out.println("Animal makes sound");}

}

class Dog extends Animal{

public void animalSound()

   {

super.animalSound();

System.out.println("Dog says: bow wow");

   }

}

public class LabTask19

{

public static void main(String args[])

   {

      Dog d=new Dog();

d.animalSound();

   }

}


Lab Task No: 20

AIM: Write a JAVA program illustrating multiple inheritance using interfaces.

PROGRAM:

//LabTask20.java

interface Printable

{

void print();

}

interface Showable

{

void show();

}

class LabTask20 implements Printable, Showable

{

public void print(){System.out.println("Hello");}

public void show(){System.out.println("World");}

public static void main(String args[])

   {

      LabTask20 obj=new LabTask20();

obj.print();

obj.show();

   }

}


Lab Task No: 21

AIM: Write a JAVA program to illustrate the concept of final keyword in the program.

PROGRAM:

//LabTask21.java

public class LabTask21

{

finalint x=10;

public static void main(String args[])

   {

      LabTask21 obj=new LabTask21();

obj.x=12;

System.out.println(obj.x);

   }

}


Lab Task No: 22

AIM: Write a JAVA program to create a package named pl, and implement this package in ex1 class.

PROGRAM:

//LabTask22

//A1.java

package pl;

public class A1

{

public void msg(){System.out.println("Hello Incredible");}

}

 

__________________________________________________________________________________

 

 

//LabTask22

//ex1.java

package mypack5;

import pl.*;

public class ex1

{

public static void main(String args[])

   {

      A1 obj=new A1();

obj.msg();

   }

}


Lab Task No: 23

AIM: Write a JAVA program to create a package named mypack and import it in circle class.

PROGRAM:

//LabTask23

//CircleArea.java

packagemypack;

public class CircleArea{

double a;

public void calcArea(int r){

         a=3.14*r*r;

System.out.println("Area of Circle is: "+a);

      }

}

 

__________________________________________________________________________________

//LabTask23

//Circle.java

packagepack2;

importmypack.*;

public class Circle{

public static void main(String args[])

      {

CircleArea c=new CircleArea();

      }

}

Lab Task No: 24

AIM: Write a JAVA program to give a simple example for abstract class.

PROGRAM:

 

//LabTask24.java

abstract class Animal{

public abstract void animalSound();

public void eat(){System.out.println("eating...");}

 

}

class Pig extends Animal{

public void animalSound(){

System.out.println("The pig says: wee wee");

     }

}

publicclass LabTask24

{

public static void main(String args[])

     {

        Pig p=new Pig();

p.animalSound();

p.eat();

     }

}

 

 

 

Lab Task No: 25

AIM: Write a JAVA program that describes exception handling mechanism.

PROGRAM:

//LabTask25.java

public class LabTask25

{

public static void main(String args[])

   {

try

      {

int x=67/0;

      }

catch(ArithmeticException e1)

      {

System.out.println("Exception 1: "+e1);

      }

try

      {

         String s=null;

System.out.println(s.length());

      }

catch(NullPointerException e2)

      {

System.out.println("Exception 2: "+e2);

      }

   }

}

Lab Task No: 26

AIM: Write a JAVA program for example of try and catch block. In this check whether the given array size is negative or not.

PROGRAM:

//LabTask26.java

public class LabTask26

{

public static void main(String args[])

   {

try

      {

intarr[] = new int[-6];

      }

catch(NegativeArraySizeException e)

      {

System.out.println("Generated exception :"+e);

      }

finally

      {

System.out.println("The 'try catch' is finished...");

      }

   }

}


Lab Task No: 27

AIM: Write a JAVA program to illustrate sub class exception precedence over base class.

PROGRAM:

//LabTask27

//Room.java

import java.io.*;

class Building

{

voidcolor() throws Exception

   {

System.out.println("Blue");

   }

}

class Room extends Building

{

voidcolor() throws Exception

   {

System.out.println("White");

   }

public static void main(String args[])

   {

      Building obj=new Room();

try

      {

obj.color();

      }

catch(Exception e){}

   }

}


Lab Task No: 28

AIM: Write a JAVA program for handling of user defined exception by using throw.

PROGRAM:

//LabTask28.java

importjava.util.Scanner;

classNegativeAmountException extends Exception

{

    String msg;

NegativeAmountException(String msg)

    {

        this.msg=msg;

    }

public String toString()

    {

returnmsg;

    }

}

public class LabTask28

{

public static void main(String args[])

    {

        Scanner sc=new Scanner(System.in);

System.out.print("Enter the amount: ");

intamt=sc.nextInt();

try

        {

if(amt<0)

            {

throw new NegativeAmountException("Invalid Amount...");

            }

else

            {

System.out.println("Amount Deposited...");

            }

        }

catch(NegativeAmountException e)

        {

System.out.println(e);

        }

    }

}

 

 

Lab Task No: 28

AIM: Write a JAVA program for handling of user defined exception by using throw.

PROGRAM:

//LabTask28.java

importjava.util.Scanner;

classNegativeAmountException extends Exception

{

    String msg;

NegativeAmountException(String msg)

    {

        this.msg=msg;

    }

public String toString()

    {

returnmsg;

    }

}

public class LabTask28

{

public static void main(String args[])

    {

        Scanner sc=new Scanner(System.in);

System.out.print("Enter the amount: ");

intamt=sc.nextInt();

try

        {

if(amt<0)

            {

throw new NegativeAmountException("Invalid Amount...");

            }

else

            {

System.out.println("Amount Deposited...");

            }

        }

catch(NegativeAmountException e)

        {

System.out.println(e);

        }

    }

}

Lab Task No: 29

AIM: Write a JAVA program to illustrate the concept of throws keyword.

PROGRAM:

//LabTask29

//Derived.java

import java.io.*;

class Base

{

void method() throws Exception

   {

System.out.println("Parent");

   }

}

public class Derived extends Base

{

public static void main(String args[])

  {

      Base obj=new Base();

try

      {

obj.method();

      }

catch(Exception e){}

   }

}

 

 

Lab Task No: 30

AIM: Write a JAVA program to illustrate creation of threads using runnable class.(start method start each of the newly created thread. Inside the run method there is sleep() for suspend the thread for 500 milliseconds).

PROGRAM:

//LabTask30.java

//ThreadSleep

public class LabTask30 implements Runnable

{

public void run()

    {

for(int i=6;i>=1;i--)

        {

try

            {

Thread.sleep(500);

System.out.println(i);

            }

catch(InterruptedException e)

            {

System.out.println(e);

            }

        }

    }

public static void main(String args[])

    {

        LabTask30 obj1=new LabTask30();

        Thread obj2=new Thread(obj1);

obj2.start();

    }

}

Lab Task No: 31

AIM: Write a JAVA program to create a class MyThread in this class a constructor, call the base class constructor, using super and starts the thread. The run method of the class starts after this. It can be observed that both main thread and created child thread are executed concurrently.

PROGRAM:

//LabTask31.java

classMyThread extends Thread

{

MyThread()

    {

super();

start();

    }

public void run()

    {

try

        {

for (int i=3;i>=1;i--)

            {

Thread.sleep(500);

System.out.println("Running "+Thread.currentThread().getName()+": "+i);

            } 

        }

catch(InterruptedExceptionie)

        {

System.out.println(ie);

        }

System.out.println("Exiting "+Thread.currentThread().getName()+"...");

    }

}

public class LabTask31

{

public static void main(String args[])

    {

MyThreadobj=new MyThread();

try

        {

for(int i=3;i>=1;i--)

            {

 

Thread.sleep(500);

System.out.println ("Running "+Thread.currentThread().getName()+": "+i);

            }

        }

catch (InterruptedExceptionie)

        {

System.out.println(ie);

        }

System.out.println ("Exiting "+Thread.currentThread().getName()+"...");

    }

}

 

 

 

 

Lab Task No: 32

AIM: Write a JAVA program to illustrate the concept of thread synchronization.

PROGRAM:

//LabTask32.java

class First

{

public void display(String msg)

  {

System.out.print ("["+msg);

try

    {

Thread.sleep(1000);

    }

catch(InterruptedException e)

    {

e.printStackTrace();

    }

System.out.println ("]");

  }

}

 

class Second extends Thread

{

  String msg;

  First fobj;

  Second (First fp,Stringstr)

  {

fobj = fp;

msg = str;

start();

  }

public void run()

  {

synchronized(fobj)      //Synchronized block

    {

fobj.display(msg);

    }

  }

}

 

public class LabTask32

{

public static void main (String[] args)

  {

    First fnew = new First();

    Second ss = new Second(fnew, "welcome");

    Second ss1= new Second (fnew,"new");

    Second ss2 = new Second(fnew, "programmer");

  }

}

 

 

 

 

Lab Task No: 33

AIM: Write Java program by implementing the concepts of different collections as list, map and set.

PROGRAM:

//LabTask33.java

importjava.util.*;

public class LabTask33

{

public static void main(String args[])

   {

     //create a HashSet to store Strings

HashSet<String>hs=new HashSet<String>();

     //Store some String elements

hs.add("India");

hs.add("America");

hs.add("Japan");

hs.add("China");

hs.add("America");

     //view the HashSet

System.out.println ("HashSet = " + hs);

     //add an Iterator to hs

     Iterator it = hs.iterator ();

     //display element by element using Iterator

System.out.println("Elements Using Iterator: ");

while(it.hasNext())

     {

       String s=(String)it.next();

System.out.println(s);

     }

 

     //create an empty stack to contain Integer objects

     Stack<Integer>st=new Stack<Integer>();

st.push(new Integer(10));

st.push(new Integer(20));

st.push(new Integer(30));

st.push(new Integer(40));

st.push (new Integer(50));

System.out.println(st);

System.out.println("Element at top of the stack is: "+st.peek());

System.out.println("Removing element at the TOP of the stack: "+st.pop());

System.out.println("The new stack is: "+st);

 

HashMap<Integer,String>hm=new HashMap<Integer,String>();

hm.put(new Integer(101),"Naresh");

hm.put(new Integer(102),"Rajesh");

hm.put(new Integer(103),"Suresh");

hm.put(new Integer(104),"Mahesh");

hm.put(new Integer(105),"Ramesh");

     Set<Integer> set=new HashSet<Integer>();

set=hm.keySet();

System.out.println(set);

   }

}





No comments:

Post a Comment

Machine Learning Course Assignments 2024(Dec)

 Machine Learning Course Assignments 2024 1)   Explain the concept of "generalization" in machine learning. Why is it a central go...