Thursday, October 27, 2022

Java lab programs for ECE

//    Lab Task -1 NON RECURSIVE   program
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;
}
}
}

2 a) write a java program that prompts the user for an integer and then prints out all the prime numbers up to that integer.

import java.util.Scanner;
public class primeExample {
 
    public static void main(String[] args) {
        
        int min = 2;
        int max ;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the maximum number upto which prime numbers are needed");
        max=sc.nextInt();
        for(int n=min;n<=max;n++) {
            if(isPrime(n)) {
                System.out.println(n);
            }
        }
    }
     
    public static boolean isPrime(int num) {    
        for(int i = 2; i <= num/i; ++i) {
            if(num % i == 0) {
                return false;
            }
        }
        return true;
    }
}

2. b)Write a JAVA program for the following

          a. Example for call by value.

          b. Example for call by reference.

call by value.

public class CallByValue

{

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);

   }

}


call by reference.

public class Call By Reference

{

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);

   }

}

4.a)Java program to check weather a number is Palindrome or not and Prime or not by using function Overloading concept.

import java.util.Scanner;
public class MyClass {
int n;
    void find()
    {
int i;
        for(i=2;i<n;i++)
        {
            if(n%i==0)
                break;
        }
        if(n==i)
System.out.println("Number is prime");
        else
System.out.println("Number is not prime");
    }
    void find(int x)
    {
int r,s=0;
        while(n>0)
        {
            r=n%10;
            s=s*10+r;
            n=n/10;
        }
        if(s==x)
System.out.println("Number is pallindrome");
        else
System.out.println("Number is not pallindrome");
    }
    public static void main(String args[]) {
MyClass m1=new MyClass();
      Scanner s1=new Scanner(System.in);
      System.out.println("Enter a number for prime and palindrome test");
      m1.n=s1.nextInt();
      m1.find();
      m1.find(m1.n);
      }
}

4.b)Java program to check weather a number is Palindrome or not and Prime or not by using Constructor Overloading concept.

import java.util.Scanner;

public class MyClass {

int n;

MyClass()

    {

        Scanner s1=new Scanner(System.in);

        n=s1.nextInt();

    }

MyClass(int x)

    {

        n=x;

    }

    void Prime()

    {

inti;

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

        {

            if(n%i==0)

                break;

        }

        if(n==i)

System.out.println("Number is prime");

        else

System.out.println("Number is not prime");

    }

    void Palindrome(int x)

    {

intr,s=0;

        while(n>0)

        {

            r=n%10;

            s=s*10+r;

            n=n/10;

        }

        if(s==x)

System.out.println("Number is pallindrome");

        else

System.out.println("Number is not pallindrome");

    }

    public static void main(String args[]) {

MyClass m1=new MyClass();

MyClass m2=new MyClass(356);

      m1.Prime();

      m2.Palindrome(m1.n);

      }}

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]

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...