Search This Blog

Sunday, 21 August 2022

B5. Java code for thread creation by implementing the Runnable Interface.

/* B5. 2019 syllabus Java code for thread creation by implementing the Runnable Interface */
import java.io.*;
import java.lang.*;
class Multi implements Runnable
{
    public void run()
    {
        try
        {
            // Display the running thread.
              System.out.println("Thread " + Thread.currentThread().getId()+ " is running");
        }
        catch (Exception e)
        {
            // Throw an exception
            System.out.println("Exception is caught");
        }
    }
}
// Main Class
class B5
{
    public static void main(String args[])
    {
        int n = 5; // Number of threads
        for (int i = 0; i < n; i++)
        {
            Thread obj = new Thread(new Multi());
            obj.start();
        }
    }
}

B4. Package program to find prime, fib and Armstrong.

/* B4.2019 Syllabus Package program to find prime, fib and Armstrong.
Steps to follow:
Step 1: Create a folder “myPackage” in “C:\Java\Bin”.
Step 2: Type and Save CheckNumber.java in myPackage folder.
Step 3: Compile it as: C:\Java\Bin>javac   myPackage\CheckNumber.java  */
//Code for CheckNumber.java:
package myPackage;
public class CheckNumber
{
    public static void prime(int num)
    {
        int pflag=0;
        for(int i=2;i<=num/2;++i)
        {
            if(num%i==0)
                pflag=1;
            break;
        }
        if(pflag==0)
                System.out.println("It is a Prime Number!");
        else
                System.out.println("It is not a Prime Number!");
        
    }
    public static void fibonacci(int num)
    {
        boolean flag=false;
        if(num>0)
        {
            flag=isFibonacci(num);
        }
        if(flag)
            System.out.println("It is a Fibonacci Number!");
        else
            System.out.println("It is not a Fibonacci Number!");

    }
    static boolean isPerfectSquare(int x)
    {
        int s = (int) Math.sqrt(x);
        return (s*s == x);
    }
      static boolean isFibonacci(int x)
    {
        return isPerfectSquare(5*x*x + 4) || isPerfectSquare(5*x*x - 4);
      }
    public static void armstrong(int num)
    {
        int temp, n=0;
        double remainder, result=0.0;
        temp = num;
       while (temp != 0)
        {
            temp =temp / 10;
            ++n;
        }
        temp = num;
        while (temp != 0)
        {
            remainder = temp%10;
            result = result + Math.pow(remainder, n);
            temp = temp / 10;
        }
        if(result == num)
            System.out.println("It is an Armstrong Number!");
        else
            System.out.println("It is not an Armstrong Number!");
    }
}
//B4 continued... 2019syllabus Package Program
Steps continued…
Step 4: Type and Save B4.java in C:\Java\Bin folder.
Step 5: Compile main program as: C:\Java\Bin>javac B4.java
Step 6: Run only main program as: C:\Java\Bin>java B4
Code for B4.java:
import java.io.*;
import myPackage.*;
class  B4
{
    public static void main(String args[]) throws IOException
    {
        int num;
        CheckNumber  obj=new  CheckNumber();
        DataInputStream in=new DataInputStream(System.in);
        System.out.println("Enter a number:");
        num=Integer.parseInt(in.readLine());
        obj.prime(num);
        obj.fibonacci(num);
        obj.armstrong(num);
    }
}



B3. Multilevel Inheritance

//B3. (2019 Syllabus) Multilevel Inheritance
import java.io.*;
class  GrandParent
{
    String gname;
    public void show(String gname)
    {
        System.out.println("Name of the GrandParent is: "+gname);
    }
}

class Parent extends GrandParent
{
    String pname;
    public void show1(String pname)
    {
        System.out.println("Name of the Parent is: "+pname);
    }
}
class Child extends Parent
{
    String cname;
    public void show2(String cname)
    {
        System.out.println("Name of the Child is: "+cname);
    }
}
class  B3
{
    public static void main(String args[])throws IOException
    {
        Child obj=new Child();
        DataInputStream in=new DataInputStream(System.in);
        System.out.println("Enter the name of GrandParent:");
        obj.gname=in.readLine();
        System.out.println("Enter the name of Parent:");
        obj.pname=in.readLine();
        System.out.println("Enter the name of Child:");
        obj.cname=in.readLine();
        obj.show(obj.gname);
        obj.show1(obj.pname);
        obj.show2(obj.cname);
    }
}

B2. Declare an interface containing methods float addition(float x, float y) and float subtraction(float x, float y). Declare the classes implementing the interface to perform respective operations

/*B2. (2019Syllabus) Declare an interface containing methods float addition(float x, float y) and float subtraction(float x, float y). Declare the classes implementing the interface to perform respective operations as listed below.
Bank - to carryout deposit and withdrawal operations. In addition to the implementation for the abstract methods, the class should contain additional methods to read and display customer information to perform the respective transaction.
EmployeeSalary - to calculate the gross and net salary. In addition to the implementation for the abstract methods, the class should contain additional methods to read and display employee information, allowance amount and deduction amount to perform the respective transaction.
Main class - which instantiates above two classes and calls respective methods.*/

import java.io.*;
interface myInterface
{
    float addition(float x, float y);
    float subtraction(float x, float y);
}
class Bank implements myInterface
{
    static float balance=0;
    float deposit, withdraw;
    float add,sub;
    String cname;
    DataInputStream in=new DataInputStream(System.in);
    public float addition(float x, float y)
    {
        balance=x+y;
        return balance;
        
    }
    public float subtraction(float x, float y)
    {
        balance = x-y;
        return balance;
    }
    public void read()throws IOException
    {
        
        System.out.println("Enter the name of customer:");
        cname=in.readLine();
        System.out.println("Enter the amount to deposit:");
        add=Float.parseFloat(in.readLine());
        System.out.println("Enter the amount to withdraw:");
        sub=Float.parseFloat(in.readLine());
        deposit=addition(balance, add);
        //System.out.println("Balance1="+add);
        withdraw=subtraction(balance, sub);
        //System.out.println("Balance2="+sub);
    }
    public void display()
    {        
        System.out.println("Details of Customer:");
        System.out.println("Name="+cname+"\nDeposit="+add+"\nWithdraw="+sub+
            "\nBalance="+balance);
                
    }
}
class EmployeeSalary
{
    static float net_sal=0;
    float add,sub;
    float basic_sal=30000;
    float allowance,deduction;
    float gross_sal;
    String ename;
        DataInputStream in=new DataInputStream(System.in);
    float addition(float x, float y)
    {
        gross_sal=x+y;
        return gross_sal;
    }
    float subtraction(float x, float y)
    {
        net_sal=x-y;
        return net_sal;
    }

    public void read()throws IOException
    {
        System.out.println("Enter the name of employee:");
        ename=in.readLine();
        System.out.println("Enter the allowance amount:");
        add=Float.parseFloat(in.readLine());
        System.out.println("Enter the deduction amount:");
        sub=Float.parseFloat(in.readLine());
        gross_sal=addition(basic_sal, add);
        //System.out.println("Balance1="+add);
        net_sal=subtraction(gross_sal, sub);
        //System.out.println("Balance2="+sub);
    }
    public void display()
    {        
        System.out.println("Details of Customer:");
        System.out.println("Name="+ename+"\nAllowance="+add+"\nDeduction="+sub+
            "\nGrossSalary="+gross_sal+"\nNetSalary="+net_sal);
    }  }
class B2
{
    public static void main(String args[])throws IOException
    {
        Bank b1=new Bank();
        EmployeeSalary e1=new EmployeeSalary();
        b1.read();
        b1.display();
        e1.read();
        e1.display();
    }
}

B1. Write a Java program to create a vector, add elements at the end, at specified location onto the vector and display the elements.

/*B1.(2019Syllabus) Write a Java program to create a vector, add elements at the end,  at specified location onto the vector and display the elements. Write an option driven  program using switch…case and also insertion of any type of objects must be possible. Read input as strings and find the type of data and convert them into appropriate objects of appropriate classes. ( Ex: 10 must be converted to object of Integer class, 2.5 into object of Float class etc.). Handle exception while converting the inputs.*/
import java.io.*;
import java.util.*;
class B1
{
    public static void main(String args[])throws IOException
    {
        int choice=0;
        String num="2";
        DataInputStream in=new DataInputStream(System.in);
        Vector list=new Vector();
        for(;;)
        {
            System.out.println("Menu");
            System.out.println("1. Add element at end");
            System.out.println("2. Add element at specified location");
            System.out.println("3. Display");
            System.out.println("4. Exit");
            System.out.println("Enter your choice:");
            choice=Integer.parseInt(in.readLine());
            switch(choice)
            {
                case 1: System.out.println("Enter an element");
                        num=in.readLine();
                        try
                        {
                             list.addElement(Integer.parseInt(num));
                             //System.out.println("number");
                        }
                        catch(NumberFormatException e)
                        {
                            
                            //System.out.println("Not a number");
                            try
                            {
                                                                                       list.addElement(Float.parseFloat(num));
                                System.out.println("Float");
                            }
                            
                        catch (NumberFormatException ne)
                            {
                                list.addElement(num);
                                //System.out.println("NotFloat");
                            }
                        }
                        break;
                case 2: System.out.println("Enter an element and location");
                        num=in.readLine();
                        int loc=Integer.parseInt(in.readLine());
                        try
                        {
                             list.insertElementAt(Integer.parseInt(num),loc-1);
                             System.out.println("Number");
                        }
                        catch(NumberFormatException e)
                        {
                        
                            //System.out.println("Not a number");
                            try
                            {
                    list.insertElementAt(Float.parseFloat(num),loc-1);
                                System.out.println("Float");
                            }
                            catch (NumberFormatException ne)
                            {
                                list.insertElementAt(num,loc-1);
                                //System.out.println("NotFloat");
                            }
                        }
                        break;
                case 3: System.out.println("Elements of Vector are:");
                        int len=list.size();
                        for(int i=0;i<len;i++)
                        {
                            System.out.println(list.elementAt(i));
                        }
                        break;
                case 4: System.exit(0);
            }
        }
    }
}

FREE Hit Counters