Search This Blog

Sunday, 21 August 2022

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

Monday, 25 July 2022

7. Java Program to implement Single Inheritance with method overriding.

 /* 7. 2019Syllabus Consider class person with fields name, address and date of birth and methods read_data() and show() and another class employee inherited form person class with fields emp_id, date of join and salary and methods read() and show(). Write java program to implement the concept of single inheritance with method overriding concepts for the above classes.*/

import java.io.*;
class person
{
    String name, address, dob;
    public void read_data() throws IOException
    {
        DataInputStream in=new DataInputStream(System.in);
        System.out.println("Enter Name, Address and Date of Birth:");
        name=in.readLine();
        address=in.readLine();
        dob=in.readLine();
        System.out.println("Name"+name);
    }
    public void show()
    {
        System.out.println("\nDetails of employee from person class");
        System.out.println("Name:"+name);
        System.out.println("Address:"+address);
        System.out.println("Date of Birth:"+dob);
    }
    
}
class employee extends person
{
    String emp_id,doj;
    float salary;
    public void read()throws IOException
    {
        DataInputStream in=new DataInputStream(System.in);
        System.out.println("Enter Employee ID, Date of Joining and Salary:");
        emp_id=in.readLine();
        doj=in.readLine();
        salary=Float.parseFloat(in.readLine());
    }
    //Comment this method to check for method overriding
    public void show()
    {
        System.out.println("\nDetails of Employee from employee class");
        System.out.println("Emp_ID:"+emp_id);
        System.out.println("Date of Joining:"+doj);
        System.out.println("Salary:"+salary);
    }
}
class Prog71
{
    public static void main(String args[])throws IOException
    {
        employee eobj=new employee();
        eobj.read_data();
        eobj.read();
        eobj.show(); // show() method is overridden.
    }
}

6. Java Program to read Student Information and concatenate the same using delimiter.

/* 6. Write a Java program to read name, register number, date of birth, address, phone number a student.Concatenate these to frame a single content by delimiting each detail with a special symbol, pass it to a method which should separate and display the details of the student. Declare a class containing the following methods:
void getInformation() – to read student information. It should call concatenate(,,,,) by  passing relevant information. void concatenate(String name, string regNo, String dob, String  address, String phoneNo) to join the information to frame a single content. It should call extractInformation(…) by passing the concatenated information. void extractInformation(String joinedInfo) to extracted concatenated contents and to display the information. Declare another class to contain main () method which calls void
getInformation( ).
Sample output:
Student Name: Venkata Krishna
Register Number: BC171128
Date of Birth: 10/05/1996
Address: No. 5, First Cross, Nehru Nagar, Sagar.
Phone Number: 9900990099
Concatenated content:
Venkata Krishna%BC171128%10/05/1996%No. 5, First Cross, Nehru Nagar, Sagar.%9900990099
(Application: This is the way using which collection of information is communicated between
client and server in networked environment)*/
    
import java.io.*;
class   add
{
    
        String name, regno, dob, address, phone;
        public void getInformation()throws IOException
        {
            DataInputStream in=new DataInputStream(System.in);
            System.out.println("Enter your name:");
            name=in.readLine();
            System.out.println("Enter your regno:");
            regno=in.readLine();
            System.out.println("Enter your dob:");
            dob=in.readLine();
            System.out.println("Enter your address:");
            address=in.readLine();
            System.out.println("Enter your phone number:");
            phone=in.readLine();
            concatenate(name,regno,dob,address,phone);
        }
        public void concatenate(String name, String regno, String dob,    
        String address, String phone)
        {
           String joinedInfo=name+"%"+regno+"%"+dob+"%"+address+"%"+phone;
           extractinformation(joinedInfo);
        }
        
        public void extractinformation(String joinedInfo)
        {
            int len=joinedInfo.length();
            int index1=joinedInfo.indexOf("%");  
            String name=joinedInfo.substring(0,index1);
            System.out.println("name="+name);
            int index2=joinedInfo.indexOf("%",index1+1);
            String regno=joinedInfo.substring(index1+1,index2);
            int index3=joinedInfo.indexOf("%",index2+1);
            String dob=joinedInfo.substring(index2+1,index3);
            int index4=joinedInfo.indexOf("%",index3+1);
            String address=joinedInfo.substring(index3+1,index4);
            String phone=joinedInfo.substring(index4+1,len);
            System.out.println("\nDetails of Student:");
            System.out.println("Student Name: "+name);
            System.out.println("Register Number: "+regno);
            System.out.println("Date of Birth: "+dob);
            System.out.println("Address: "+address);
            System.out.println("Phone Number: "+phone);
        }
}
 
class   Prog61
  {
      public  static  void  main(String  args[])throws  IOException
      {
            add obj=new add();
            obj.getInformation();
      }
  }

5. Java Program to generate Registration Number based on Year, Cycle and Serial Number.

/* 5. (2019-20 Syllabus) Assume that an examination authority conducts qualifying examination for candidates twice eachyear. First, in the month of June, second, in the month of December. Before the exam, it opens a registration process so that candidates register themselves. After the end of the registration dates, the authority consolidates the list of candidates and generates the unique register numbers. These numbers are assigned to each candidate. The format of the register numbers is as below.
Each register number should contain  exactly 10 characters. */
/*For example, if year of registration 2018, cycle 2 and there are five candidates registered then, registration numbers are: QE20182001, QE20182002, QE20182003, QE20182004, QE20182005.
The serial numbers should contain exactly 3 digits. To maintain it, prefix zeros as needed. (up to 9 serial number should be prefixed with two zeros, after 9, upto 99 it should be prefixed with single zero and after 99, no zeros). Write a Java program to generate the registration numbers as per the above requirement. */

import java.io.*;
class ProgNw5
{
    public static void main(String args[])throws IOException
    {
        String name[]=new String[50];
        String year[]=new String[50];
        String cycle[]=new String[50];
        String month;
        String count1[]=new String[50];
        System.out.println("Registration of Students for             Exams:");
        DataInputStream in= new DataInputStream(System.in);    
        System.out.println("Enter number of students");
        int n=Integer.parseInt(in.readLine());
        int count=0;
        for(int i=0;i<n;i++)
        {
         System.out.println("Enter the Name, Year and                   Month(June or December)");
            name[i]=in.readLine();
            year[i]=in.readLine();
            //cycle[i]=in.readLine();
            month=in.readLine();
 
        if(month.equals("June") || month.equals("june") ||  
              month.equals("JUNE"))
            {
                cycle[i]="1";
            }
            else
                cycle[i]="2";
            count++;
            if(count<10)
                count1[i]="00"+count;
            else
                count1[i]="0"+count;
            //System.out.println("Regno=QE"+year[0]+cycle[0]+"001");
        }
        System.out.println("Details of Students:");
        System.out.println("Name\t SerialNumber");

        for(int i=0;i<n;i++)
        {
  System.out.println(name[i]+"\\tQE"+year[i]+cycle[i]+count1[i]);
        }    }    }   

Friday, 15 July 2022

6. NEP Java Program to define a class called employee with the name and date of appointment. Create ten employee objects as an array and sort them as per their date of appointment i.e., print them as per their seniority.

6. Program to define a class called employee with the name and date of appointment. Create ten employee objects as an array and sort them as per their date of appointment i.e., print them as per their seniority.

import java.io.*;

import java.util.*;

class employee

{

     String ename;

     Date doa;

     public employee(String ename,Date doa)

     {

          this.ename=ename;

          this.doa=doa;

     }

     public void display()

     {

System.out.println("Employee Name:"+ename+"  Date of Appointment:"+  doa.getDate()+"/" +doa.getMonth()+"/"+doa.getYear());

     }

}

 

class Program61

{

     public static void main(String as[]) throws IOException

     {

          employee eobj[]=new employee[10];

          eobj[0]=new employee("AAA",new Date(2022,05,18));

          eobj[1]=new employee("BBB",new Date(2021,07,20));

          eobj[2]=new employee("CCC",new Date(2018,05,15));

          eobj[3]=new employee("DDD",new Date(2019,02,11));

          eobj[4]=new employee("EEE",new Date(2010,03,12));

          eobj[5]=new employee("FFF",new Date(2008,04,15));

          eobj[6]=new employee("GGG",new Date(2005,06,18));

          eobj[7]=new employee("HHH",new Date(2022,05,21));

          eobj[8]=new employee("III",new Date(2004,05,20));

          eobj[9]=new employee("JJJ",new Date(2005,06,14));

                  

          System.out.println("List of Employees");

          for(int i=0;i<eobj.length;i++)

                   eobj[i].display();

          for(int i=0;i<eobj.length;i++)

          {

              for(int j=0;j<eobj.length;j++)

              {

                   if(!eobj[i].doa.after(eobj[j].doa))

                   {

                        employee t=eobj[i];

                        eobj[i]=eobj[j];

                        eobj[j]=t;

                   }

              }

          }

       System.out.println("List of Employees Seniority-wise");

          for(int i=0;i<eobj.length;i++)

          eobj[i].display();

     }

 

}

FREE Hit Counters