Sunday, 12 March 2023

5. Write an interface called numbers, with a method in Process(int x, int y). Write a class called Sum, in which the method Process finds the sum of two numbers and returns an int value. Write another class called Average, in which the Process method finds the average of the two numbers and returns an int.



import java.util.*;

interface Number
{
    int process(int x,int y);
}

class Sum implements Number
{
    public int process(int x,int y)
    {
        return x+y;
    }
}

class Avg implements Number
{
    public int process(int x,int y)
    {
        return (x+y)/2;
    }
}

class Program5
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
         
        System.out.print("enter value for x: ");
        int x = sc.nextInt();
       
        System.out.print("enter value for y: ");
        int y = sc.nextInt();
       
        Number n = new Sum();
        System.out.println("sum is: " +n.process(x,y));
       
        n = new Avg();
        System.out.println("average is: " + n.process(x,y));
    }
}


4. Write program to create an array of company name and another array of price quoted by the company. Fetch the company name who has quoted the lowest amount.



class Program4
{
    public static void main(String args[])
    {
        String company[] = {"LG","VIVO","OPPO","SAMSUNG","MI"};
        int price[] = {2000,5000,1300,1200,6000};
       
        int min = price[0];
        for(int i=0; i<price.length; i++)
        {
            if(min > price[i])
            {
                min = i;
            }
        }
       
        System.out.print("Lowest amount company: " + company[min]);
       
       
    }
}


3. Write a program to find sum of two matrices of 3 x3.



import java.util.*;

class Program3
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
       
        System.out.print("enter number of matrix column and row: ");
        int c = sc.nextInt();
        int r = sc.nextInt();
       
        int arr1[][] = new int[c][r];
        int arr2[][] = new int[c][r];
        int arr3[][] = new int[c][r];
       
        System.out.println("enter value first matrix: ");
        for(int i=0; i<c; i++)
        {
            for(int j=0; j<r; j++)
            {
                arr1[i][j] = sc.nextInt();
            }
        }
       
        System.out.println("enter value second matrix: ");
        for(int i=0; i<c; i++)
        {
            for(int j=0; j<r; j++)
            {
                arr2[i][j] = sc.nextInt();
            }
        }
       
        for(int i=0; i<c; i++)
        {
            for(int j=0; j<r; j++)
            {
                arr3[i][j] = arr1[i][j] + arr2[i][j];
            }
        }
       
        System.out.println("\nSum of matrix: ");
        for(int i=0; i<c; i++)
        {
            for(int j=0; j<r; j++)
            {
                System.out.print(arr3[i][j] + "  ");
            }
            System.out.println("\n");
        }
       
    }
}


2. Write a program to create an array to store 5 integer values. Also initialize the array with 5 numbers and display the array Elements in reverse order


class Program2
{
    public static void main(String args[])
    {
        int arr[] = new int[] {1,2,3,4,5};
       
        System.out.print("array values: ");
        for(int i=0; i<arr.length; i++)
        {
            System.out.print(arr[i] + " ");
        }
       
        System.out.println("\nreverse of array: ");
        for(int i = arr.length-1; i>=0; i--)
        {
            System.out.print(arr[i] + " ");
        }
    }
}


 

1. Write a program to sort the elements of one dimensional array. Read value of array elements through command line argument.



class Program1
{
    public static void main(String args[])
    {
        int arr[] = new int[args.length];
        int temp;
       
        for(int i=0; i<args.length; i++)
        {
            arr[i] = Integer.parseInt(args[i]);
            System.out.print(arr[i] + " ");
        }
       
        for(int i=0; i<arr.length; i++)
        {
            for(int j=0; j<arr.length; j++)
            {
                if(arr[i] < arr[j])
                {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
       
        System.out.println("\n sorted value: ");
       
        for(int i=0; i<arr.length; i++)
        {
            System.out.print(arr[i] + " ");
        }
    }
}


Unit 2 java Programs


1. Write a program to sort the elements of one dimensional array. Read value of array elements through command line argument. 


2. Write a program to create an array to store 5 integer values. Also initialize the array with 5 numbers and display the array Elements in reverse order. 


3. Write a program to find sum of two matrices of 3 x3.


4. Write program to create an array of company name and another array of price quoted by the company. Fetch the company name who has quoted the lowest amount. 


5. Write an interface called numbers, with a method in Process(int x, int y). Write a class called Sum, in which the method Process finds the sum of two numbers and returns an int value. Write another class called Average, in which the Process method finds the average of the two numbers and returns an int. 


6. Create a class called NumberData that accept any array of the five numbers. Create a sub class called Numplay which provides methods for followings: 1. Display numbers entered. 2. Sum of the number. 3. Average of the numbers. 4. Maximum of the numbers. 5. Minimum of the numbers. Create a class that provides menu for above methods. Give choice from the command-line argument. 


7. Declare an abstract class Vehicle with an abstract method named numWheels( ).provide subclasses Car and Truck that each implements this method. Create instance of these subclasses and demonstrate the use of this method 


8. Write an interface called Exam with a method Pass(int mark) that returns a Boolean. Write another interface called Classify with a method Division(int average) which returns a string. Write a class called Result which implements both Exam and Classify. The pass method should return true if the marks is greater than or equal to 35 else false. The division method must return “First” when the parameter average is 60 or more, “second” when average is 50 or more but below 60, “no division” when average is less than 50. 


9. Create class calculation with an abstract method area( ). Create Rectangle and Triangle subclasses of calculation and find area of rectangle and triangle. 


10. The abstract Vegetable class has four subclasses named cabbage, carrot and potato. Write an application that demonstrates how to establish this class hierarchy. Declare one instance variable of type string that indicates the color of a vegetable. Create and display instances of these object. Override the toString() method of object to return a string with the name of the vegetable and its color.


10. Write a java program to display powers of 2 i.e. 2,4,8,16 etc up to 1024 using bitwise operators.



class Program10
{
    public static void main(String args[])
    {
        int a=1;
        for(int i=0; i<11; i++)
        {
            System.out.println(a << i);
        }
    }
}


9. A bank gives 6.5% per annum interest on deposits made in that bank. Write a program to calculate the total amount that a person will receive after the end of 5 years for a deposit of Rs.5000 for compound interest. Create necessary methods and constructors too.

 



class Cintrest
{
    double p,r,n;
    double ci;
   
    Cintrest(double p1,double r1,double n1)
    {
        p = p1;
        r = r1;
        n = n1;
    }
   
    double calculate_intrest()
    {
        ci = p * (Math.pow((1+ (r/100)),n));
        return ci;
    }
}

class Program9
{
    public static void main(String args[])
    {
        double p=5000,r=6.5,n=5;
       
        Cintrest obj = new Cintrest(p,r,n);
       
        System.out.println("compound intrest: " + obj.calculate_intrest());
    }
}


8. A shop during festival season offers a discount 10% for purchase made up to Rs.1,000, 12% for purchase value of Rs.1,000 or more up to Rs 1,500 and 15% for purchase value of Rs.1,500 or more. Write a program to implement the above scheme for a given sales and print out the sales and print out the sales value, discount and net amount payable by a customer. Create necessary methods and constructors

 



import java.util.*;

class Offer
{
    int discount;
    int amount;
    float finalamount;
   
    Offer()
    {
        discount = 0;
        amount = 0;
        finalamount = 0;
    }
   
    void calculate_amount(int amt)
    {
        if(amt <= 1000)
        {
            discount = 10;
        }
        else if(amt < 1500 && amt>1000)
        {
            discount = 12;
        }
        else
        {
            discount = 15;
        }
        finalamount = amt - ((amt*discount) / 100);
        System.out.println("\nyour sales value: " + amt +"\nyour discount:"
+ (amt*discount))/100);

        System.out.println("amount with discount: " + finalamount);
    }
}

class Program8
{
    public static void main(String args[])
    {
       
        Offer obj = new Offer();
        Scanner sc = new Scanner(System.in);
       
        System.out.print("enter your amount: ");
        int amount = sc.nextInt();
       
        obj.calculate_amount(amount);
       
    }
}


7. Create a complex number class. The class should have a constructor and methods to add, subtract and multiply two complex numbers and to return the real and imaginary parts.



class Complex
{
    int real,img;
   
    Complex()
    {
        real = img = 0;
    }
   
    Complex(int x)
    {
        real = img = x;
    }
   
    Complex(int x,int y)
    {
        real = x;
        img = y;
    }
   
    Complex add(Complex a, Complex b)
    {
        Complex temp = new Complex();
       
        temp.real = a.real + b.real;
        temp.img = a.img + b.img;
       
        return (temp);
    }
   
    Complex sub(Complex a, Complex b)
    {
        Complex temp = new Complex();
       
        temp.real = a.real - b.real;
        temp.img = a.img - b.img;
       
        return (temp);
    }
   
    Complex mul(Complex a, Complex b)
    {
        Complex temp = new Complex();
       
        temp.real = a.real * b.real;
        temp.img = a.img * b.img;
       
        return (temp);
    }
   
    void display()
    {
        System.out.print("\nreal: " + real + "  imaginary: " + img);
    }
}

class Program7
{
    public static void main(String args[])
    {
        Complex c1 = new Complex(2,3);
        Complex c2 = new Complex(2,5);
        Complex c3 = new Complex();
       
        c3 = c1.add(c1,c2);
        c3.display();
       
        c3 = c1.sub(c1,c2);
        c3.display();
       
        c3 = c1.mul(c1,c2);
        c3.display();
       
    }
}


6. Write a program to calculate the area of square and rectangle by overloading the area method.

 



class Example
{
    void area(float x)
    {
        System.out.println("Square area: " + x*x);
    }
       
    void area(float x, float y)
    {
        System.out.println("Rectangle area: " + x*y);
    }
}

class Program6
{
    public static void main(String args[])
    {
        Example e1 = new Example();
        e1.area(5);
        e1.area(5,7);
    }
}


5. Write a program to calculate the hypotenuse of right angled triangle when other sides of the triangle are given. (Hypotenuse = square root (x*x + Y *Y))

 



import java.util.*;

class Program5
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
       
        System.out.print("\nenter value for x: ");
        double x = sc.nextInt();
       
        System.out.print("\nenter value for y: ");
        double y = sc.nextInt();
       
        double h = Math.sqrt((x*x) + (y*y));
       
        System.out.print("\nHypotenuse of right angled triangle is: " + h);
    }
}


4. Write a java program to scan 3 integer values from the command line argument and display the maximum number using conditional operator.

 



class Program4
{
    public static void main(String args[])
    {
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        int c = Integer.parseInt(args[2]);
        int max;
       
        max = (a>b)? ((a>c)? a: c) : ((b>c)? b : c);
       
        System.out.println("Maximum number is: " + max);
       
    }
}


3. A car accessories shop assigns code 1 to seat covers, 2 to steering wheel covers , 3 to car lighting and 4 for air purifiers. All other items have code 5 or more. While selling the goods, a sales tax of 2% to seat covers ,3% to steering wheel covers, 4% to car lighting, 2.5% to air purifiers and 1.2% for all other items is charged. A list containing the product code and price is given for making a bill. Write a java program using switch statements to prepare a bill.



import java.util.*;

class Program3
{
    public static void main(String args[])
    {
        System.out.println("1. seat cover: 1000rs\n2. steering wheel: 1500rs");
        System.out.println("3. car lighting: 2000rs\n4. air purrfiers:1200rs");
        System.out.println("5. other items: ");
       
        int ch;
        double amount;
        System.out.print("\nenter your choice: ");
        Scanner sc = new Scanner(System.in);
        ch = sc.nextInt();
       
        switch(ch)
        {
            case 1: amount = 1000 + (1000 * 2) / 100;
                    System.out.println("seat cover total charges: " + amount);
                    break;
                   
            case 2: amount = 1500 + (1500 * 3) / 100;
                    System.out.println("steering wheel total charges: " + amount);
                    break;
                   
            case 3: amount = 2000 + (2000 * 4) / 100;
                    System.out.println("car lighting total charges: " + amount);
                    break;
                   
            case 4: amount = 1200 + (1200 * 2.3) / 100;
                    System.out.println("air purifiers: " + amount);
                    break;
                   
            case 5: System.out.print("\nenter item name: ");
                    String str = sc.next();
                   
                    System.out.print("enter amount: ");
                    amount = sc.nextDouble();
                   
                    amount = amount + (amount * 1.2) / 100;
                    System.out.println("total charges is: " + amount);
                    break;
                   
            default : System.out.print("\n--> enter valid choice <--");
        }
    }
}


2. A motor cycle dealer sells two-wheelers to his customer on loan, which is to be repaid in 5 years. The dealer charges simple interest for the whole term on the day of giving the loan itself. The total amount is then divided by 60(months) and is collected as equated monthly instalment (EMI). Write a program to calculate the EMI for a loan of Rs. X, where X is given from command line argument. Print the EMI value in rupees.



class Program2
{
    public static void main(String args[])
    {
        int amount = Integer.parseInt(args[0]);
        float rate = Float.parseFloat(args[1]);
        int time = 5;
       
        float si = (amount * rate * time) / 100;
       
        float tamount = si + amount;
       
        float emi = tamount / 60;
       
        System.out.println("EMI is: " + emi);
    }
}


1. Write a program to evaluate simple interest of a given principle, rate and time.



class Intrest
{
    public static void main(String args[])
    {
        int p=13000,t=5;
        double intrest, r=2.5;
       
        intrest = (p * r * t) / 100;
       
        System.out.println("principle= " + p + "\nrate: " + r + "\ntime: "
+ t);

        System.out.println("\nSimple intrest is: " + intrest);
    }
   
}


Unit 1 Java Programs


1. Write a program to evaluate simple interest of a given principle, rate and time.


2. A motor cycle dealer sells two-wheelers to his customer on loan, which is to be repaid in 5 years. The dealer charges simple interest for the whole term on the day of giving the loan itself. The total amount is then divided by 60(months) and is collected as equated monthly instalment (EMI). Write a program to calculate the EMI for a loan of Rs. X, where X is given from command line argument. Print the EMI value in rupees.


3. A car accessories shop assigns code 1 to seat covers, 2 to steering wheel covers , 3 to car lighting and 4 for air purifiers. All other items have code 5 or more. While selling the goods, a sales tax of 2% to seat covers ,3% to steering wheel covers, 4% to car lighting, 2.5% to air purifiers and 1.2% for all other items is charged. A list containing the product code and price is given for making a bill. Write a java program using switch statements to prepare a bill.


4. Write a java program to scan 3 integer values from the command line argument and display the maximum number using conditional operator.


5. Write a program to calculate the hypotenuse of right angled triangle when other sides of the triangle are given. (Hypotenuse = square root (x*x + Y *Y))


6. Write a program to calculate the area of square and rectangle by overloading the area method.


7. Create a complex number class. The class should have a constructor and methods to add, subtract and multiply two complex numbers and to return the real and imaginary parts.


8. A shop during festival season offers a discount 10% for purchase made up to Rs.1,000, 12% for purchase value of Rs.1,000 or more up to Rs 1,500 and 15% for purchase value of Rs.1,500 or more. Write a program to implement the above scheme for a given sales and print out the sales and print out the sales value, discount and net amount payable by a customer. Create necessary methods and constructors


9. A bank gives 6.5% per annum interest on deposits made in that bank. Write a program to calculate the total amount that a person will receive after the end of 5 years for a deposit of Rs.5000 for compound interest. Create necessary methods and constructors too.


10. Write a java program to display powers of 2 i.e. 2,4,8,16 etc up to 1024 using bitwise operators.




Gujrat University Semester 4 Java Programs




 Unit 1


Unit 2


Unit 3




Gujrat University Semester 4













Saturday, 27 August 2022

Merge Sort in C Language

 // merge sort...


#include<stdio.h>

#include<conio.h>


void main()

{

int no, i, j, l=0, mid, r, count=-1, temp, flag=0;

int arr[20], a[20], b[20];

clrscr();


printf("how many element you want to enter: ");

scanf("%d",&no);

r = no-1;


printf("\nenter element one by one: \n");

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

{

printf("enter %d element: ",i+1);

scanf("%d",&arr[i]);

}


mid = (l+r) / 2;

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

{

a[i] = arr[i];

count++;

}

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

{

flag=0;

for(j=0; j<mid-i; j++)

{

if(a[j] > a[j+1])

{

flag=1;

temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

}

}

if(flag==0)

{

break;

}

}


for(i=0; i<r-count; i++)

{

b[i] = arr[i+mid+1];

}


for(i=0; i<r-count-1; i++)

{

flag=0;

for(j=0; j<r-count-1-i; j++)

{

if(b[j] > b[j+1])

{

flag=1;

temp = b[j];

b[j] = b[j+1];

b[j+1] = temp;

}

}

if(flag==0)

{

break;

}

}


l=0; r=0; i=0;

while((l <= mid+1) && (r <= no-1))

{

if(b[r] < a[l])

{

if(r-1 != no-1)

{

arr[i] = b[r];

i++; r++;

}

else if((r-1 == no-1) && (l != mid))

{

arr[i] = a[l];

i++; l++;

}

}

else if(a[l] < b[r])

{

if(l-1 != mid)

{

arr[i] = a[l];

i++; l++;

}

else if((l-1 == mid) && (r-1 != no-1))

{

arr[i] = b[r];

i++; r++;

}

}

else

{

arr[i] = a[l];

i++; l++;

arr[i] = b[r];

i++; r++;

}

}


printf("\n\nsorted element:");

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

{

printf("%d ",arr[i]);

}

getch();

}

Bubble Sort in C Language

 // Bubble sort...


#include<stdio.h>

#include<conio.h>


void main()

{

int arr[20], no, i, j, temp, flag;

clrscr();


printf("how many element you want to enter: ");

scanf("%d",&no);


printf("\nenter element one by one:\n");

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

{

scanf("%d",&arr[i]);

}


for(i=0; i<no-1; i++)

{

flag=0;

for(j=0; j<no-1-i; j++)

{

if(arr[j] > arr[j+1])

{

flag=1;

temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

if(flag==0)

{

break;

}

}


printf("\nsorted element: ");


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

{

printf("%d ",arr[i]);

}

getch();

}

Sorting in C Language


 ● Bubble Sort


● Merge Sort


Friday, 26 August 2022

Binary Search in C Language


#include<stdio.h>

#include<conio.h>


void main()

{

int  no, arr[20], search, mid, l=0, r, i, j, temp;

clrscr();


printf("how many element you want to enter: ");

scanf("%d",&no);


printf("\nenter element one by one:\n");


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

{

printf("%d element: ",i+1);

scanf("%d",&arr[i]);

}


for(i=0; i<no-1; i++)

{

for(j=0; j<no-1; j++)

{

if(arr[j] > arr[j+1)

{

temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}


printf("\nyour element set in assending order: ");

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

{

printf("\n%d element: %d",i+1,arr[i]);

}


printf("\n\nsearch element: ");

scanf("%d",&search);


r=no-1;


while(l <= r)

{

mid = (l + r) / 2;


if(search == arr[mid])

{

printf("\n%d element at %d position",search,mid+1);

break;

}

else if(search < arr[mid])

{

r = mid-1;

}

else

{

l = mid+1;

}

}


if(l > r)

{

printf("\n%d element is not found",search);

}

getch();

}

python programs

1. sum of two number