Skip to main content

Lab Programs Cycle II


a) Develop a Java program that displays
i) The roots of a quadratic equation ax2+bx+c=0
ii) The nature of roots by calculating the discriminate D.

import java.util.Scanner;
public class Roots 
{
    public static void main(String args[])
    {
        int a,b,c;
        double root1,root2,discriminate;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter value for a : ");
        a = sc.nextInt();
        System.out.print("Enter value for b : ");
        b = sc.nextInt();
        System.out.print("Enter value for c : ");
        c = sc.nextInt();
        discriminate = b*b-4*a*c;
        if(discriminate > 0)
        {
        System.out.println("Roots are real and distinct");
        root1 = (-b+( Math.sqrt(discriminate) / (2*a) ) );
        root2 = (-b-( Math.sqrt(discriminate) / (2*a) ) );
        System.out.println("Root1 = "+root1+" Root2 = "+root2);
        }
        else if(discriminate == 0)
        {
        System.out.println("Roots are real and equal");
        root1 = -b / (2*a);
        System.out.println("Root = "+root1);
        }
        else
        {
        System.out.println("Roots are Imaginary");
        root1 = -b / (2*a);
        System.out.println("Root = "+root1+"+i"+Math.sqrt(-discriminate));
        System.out.println("Root = "+root1+"-i"+Math.sqrt(-discriminate));
        }
    }
}



b) N bikers compete in a race such that they drive at a constant speed, which
may or may not be the same as the other. To qualify the race, the speed of a
racer must be more than the average speed of all N racers. Take as input, the
speed of each racer and print back the speed of qualifying racers.

import java.util.Scanner;
public class Racers 
{
    public static void main(String args[])
    {
        int a[] = new int[5];
        double average = 0,sum = 0; 
        Scanner sc = new Scanner(System.in);
        for(int i =0;i<5;i++)
        {
            System.out.print("Enter speed for Racer "+(i+1)+": ");
            a[i] = sc.nextInt();
        }    
        for(int i =0;i<5;i++)
        {
            sum = sum + a[i];
        }
        average = sum/5;
        for(int i =0; i<5;i++)
        {
            if( a[i] > average )
            {
                System.out.println("Racer "+(i+1)+" is qualified" );
            }
        }
    }
}


c) Develop a Java program that displays the name of the day, based on the
value of day, using the switch statement.

import java.util.Scanner;
public class SwitchCase
{
public static void main(String args[])
{
int day;
Scanner sc = new Scanner(System.in);
// prompt the user for day value and store in day
System.out.print("enter the day value : ");
day = sc.nextInt();
// based on the day value corresponding  switch case statements will be executed
// if day value doesn't match with any of case value statements in default will be executed 
switch(day)
{
case 1: System.out.println("Monday");
break;
case 2: System.out.println("Tuesday");
break;
case 3: System.out.println("Wednusday");
break;
case 4: System.out.println("Day is Thursday");
break;
case 5: System.out.println("Day is Friday");
break;
case 6: System.out.println("Day is saturday");
break;
case 7: System.out.println("Day is sunday");
break;
default:System.out.println("Please Enter value range from 1 to 7");
break; 
}
}
}


d) Develop a Java program to search for an element in a given list of elements
using Linear Search.

import java.util.Scanner;
public class LinearSearch
{
public static void main(String args[])
{
int totalElements,key,found = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter total no of elements in array :");
totalElements = sc.nextInt();
int a[] = new int[totalElements];
// read all the elements into an array
for(int i = 0; i<totalElements; i++)
{
System.out.print("Enter element at "+i+" index :");
a[i] = sc.nextInt();
}
System.out.print("Enter the key Element to be searched :");
key = sc.nextInt();
// check if the element exists in array linearly, if found change flag value to 1
for(int i=0; i<totalElements; i++)
{
if(a[i] == key)
{
System.out.println("Element found at "+i+" index");
found = 1;
}
}
// If given key value doesn't found in array of elements, flag value will be 0.
if(found == 0)
{
System.out.println("Element not found");
}
} //End of main
} //End of class LinearSearch


e) Develop a Java program to perform multiplication of two matrices.

import java.util.Scanner;
class MatrixMultiplication
{
    public static void main(String args[])
    {
    int mat1row, mat1col, mat2row, mat2col,i,j,k,sum;
    Scanner sc = new Scanner(System.in);
    // read no of rows and columns of two matrices 
    System.out.println("Enter the number of rows of matrix1");
    mat1row = sc.nextInt();
    System.out.println("Enter the number columns of matrix 1");
    mat1col = sc.nextInt();
    System.out.println("Enter the number of rows of matrix2");
    mat2row = sc.nextInt(); 
    System.out.println("Enter the number of columns of matrix 2");
    mat2col = sc.nextInt();
    // perform matrix multipication only if 
    // number of columns in matrix1 = no of rows in matrix 2  
    if(mat1col==mat2row)
    { 
        int mat1[][] = new int[mat1row][mat1col]; 
        int mat2[][] = new int[mat2row][mat2col]; 
        int resMat[][] = new int[mat1row][mat2col];
        // read all elements for matrix 1 
        System.out.println("Enter the elements of matrix1"); 
        for ( i= 0 ; i < mat1row ; i++ )
            {  
                for ( j= 0 ; j < mat1col ;j++ )
                mat1[i][j] = sc.nextInt(); 
            }
        // read all elements for matrix 2
        System.out.println("Enter the elements of matrix2"); 
        for ( i= 0 ; i < mat2row ; i++ )
            {  
                for ( j= 0 ; j < mat2col ;j++ )
                mat2[i][j] = sc.nextInt();
            }
        // perform matrix multiplication and store result in resMat 
        System.out.println("matrix multiplication is ");
        for ( i= 0 ; i < mat1row ; i++ ) 
        {
            for ( j= 0 ; j <mat2col  ; j++)
            {
                sum=0;
                for ( k= 0 ; k <mat2row; k++ )
                {
                    sum +=mat1[i][k]*mat2[k][j] ; 
                }
                resMat[i][j]=sum;
            }
        }
        // display elements of result matrix    
        for ( i= 0 ; i < mat1row; i++ )
        {
            for ( j=0 ; j < mat2col;j++ )
            {    
                System.out.print(resMat[i][j]+" ");
            }    
            System.out.println();    // Go for next line to print elements of next row
        }
        }    // end of if 
    else
        {    
            System.out.print("multipication does not exist ");
        }
    }    // End of main
}    // End of class MatrixMultiplication


f) Develop a Java program using StringBuffer to perform various operations on
a string. 

public class StringBufferMethods
{
   public static void main(String arg[])
{
// by default capacity allocated will be of 16 characters
StringBuffer sbdefault=new StringBuffer(); 
System.out.println("The capacity of the string sbdefault is : " +sbdefault.capacity());
sbdefault.append("ECE");
System.out.println("This string sbdefault is appended with ECE: " +sbdefault );
System.out.println("The capacity of the string sbdefault after appending : " +sbdefault.capacity());
// below capacity allocated will be 16 characters plus length of initialString
StringBuffer sb=new StringBuffer("InitialString"); 
System.out.println("This string sb is : " +sb);
System.out.println("The capacity of the string sb is : " +sb.capacity());
System.out.println("The length of the string sb is : " +sb.length());
System.out.println("The character at an index of 6 is : " +sb.charAt(6));
sb.setCharAt(3,'x');
System.out.println("After setting char x at position 3 : " +sb);
sb.append(" Im appended"); 
System.out.println("After appending : " +sb);
sb.insert(13," Hai Inserted");
System.out.println("After inserting : " +sb);
sb.delete(13,17);
System.out.println("After deleting : " +sb);
sb.reverse();
System.out.println("After reversing : " +sb);
}
}

Comments