Search This Blog

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

     }

 

}

5. NEP Java Program to define a class with the following attributes Name of the class (BCA, BCom, BSc), Name of the staff No of the students in the class, Array of students in the class.

 //5.In a college first year class are having the following attributes Name of the class (BCA, BCom, BSc), Name of the staff No of the students in the class, Array of students in the class. Define a class called first year with above attributes and define a suitable constructor. Also write a method called best Student() which process a first-year object and return the student with the highest total mark. In the main method define a first-year object and find the best student of this class

import java.io.*;

class Program51

{

     public String cname, sname;

     public static int big,count=-1;

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

    {

          int n;  // Total number of students.

          System.out.println("How many students?");

          DataInputStream in= new DataInputStream(System.in);

          n=Integer.parseInt(in.readLine());

         FirstYear  student[] = new FirstYear[n];

          //System.out.println("n="+n);

         

          for(int i=0;i<n;i++)

          {

System.out.println("Enter the Class Name, Student Name and Total Marks");

              student[i] = new FirstYear("abc","xyz");

                                 student[i].read(in.readLine(),in.readLine(),Integer.parseInt(in.readLine()));

          }

       

          for(int i=0;i<n;i++)

          {

              student[i].BestStudent();

          }

          System.out.println("Best Student Details:");

    System.out.println("StudentName: "+student[count].sname+

"\nClass: "+student[count].cname+"\nTotalMarks: "+big);

     }

}

class FirstYear

{

          public int total=0;

          public String cname, sname;

         

          Program51 obj=new Program51();

          FirstYear(String cname, String sname)

          {

              this.cname = cname;

              this.sname = sname;

          }

       public void read(String cname, String sname,int total )

          {

              this.cname=cname;

              this.sname=sname;

              this.total=total;

         

          }

    

          public void BestStudent()

          {

              if(this.total>obj.big)

              {

                   obj.big=this.total;

                   obj.count++;

               }

          }

}

4. NEP Java Program to create a student class with the following attributes; Enrollment No: Name, Mark of sub1, Mark of sub2, mark of sub3, Total Marks.

 4. Program to create a student class with the following attributes; Enrollment No: Name, Mark of sub1, Mark of sub2, mark of sub3, Total Marks. Total of the three marks must be calculated only when the student passes in all three subjects. The pass mark for each subject is 50. If a candidate fails in any one of the subjects his total mark must be declared as zero. Using this condition write a constructor for this class. Write separate functions for accepting and displaying student details. In the main method create an array of n student objects and display the details.

import java.io.*;

class Program4

{

 

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

    {

        int n;

        System.out.println("How many students?");

        DataInputStream in= new DataInputStream(System.in);

        n=Integer.parseInt(in.readLine());

        Student arr[] = new Student[n];

        System.out.println("n="+n);

        for(int i=0;i<n;i++)

        {

        System.out.println("Enter the ID, name, marks in sub1,

          sub2 and sub3 for Student-"+(i+1));

          arr[i] = new Student(0,"");

                       arr[i].read(Integer.parseInt(in.readLine()),in.readLine(),

Integer.parseInt(in.readLine()),Integer.parseInt(in.readLine()),Integer.parseInt(in.readLine()));

             

          }

          for(int i=0;i<n;i++)

          {

              arr[i].display();

          }

    }

    

}

class Student

{

          public int id,sub1,sub2,sub3;

          public float totalmarks;

          public String name;

          Student(int id, String name)

          {

              this.id = id;

              this.name = name;

          }

public void read(int id, String name,int sub1, int sub2, int sub3 )

{

              this.id=id;

              this.name=name;

              this.sub1=sub1;

              this.sub2=sub2;

              this.sub3=sub3;

              if(this.sub1<50 || this.sub2<50 || this.sub3<50)

              {   

                   this.totalmarks=0.0F;

              }

              else

              {

                                                                              this.totalmarks=this.sub1+this.sub2+this.sub3;

              }

 

}

public void display()

{

     System.out.println("Student id: " + id + " "+ "name: "+      name);

     System.out.println("totalmarks:"+totalmarks);

     System.out.println();

}

}

3. NEP Java Program with class variable that is available for all instances of a class. Use static variable declaration. Observe the changes that occur in object’s member variable values.

3. Program with class variable that is available for all instances of a class. Use static variable declaration. Observe the changes that occur in object’s member variable values.

import  java.io.*;

class Prog3

{

     static int num1; //Class Variable

     int num2;    

     public static void  main(Stringargs[])

     {

          Prog3.num1=30;

          Prog3 obj1=new Prog3();

          Prog3 obj2=new Prog3();

          obj1.num2=25;

          obj2.num2=50;

          System.out.println("Assigning Prog3.num1=30, obj1.num2=25 and

obj2.num2=50");

          System.out.println("Prog3.num1="+Prog3.num1);

          System.out.println("obj1.num2="+obj1.num2);

          System.out.println("obj2.num2="+obj2.num2);

          obj1.num1=45;

          obj1.num2=67;

          System.out.println("\nAfter changing class variable with

obj1.num1=45 and obj1.num2=67");

          System.out.println("Prog3.num1="+Prog3.num1);

          System.out.println("obj1.num1="+obj1.num1);

          System.out.println("obj2.num1="+obj2.num1);

          System.out.println("obj1.num2="+obj1.num2);

          System.out.println("obj2.num2="+obj2.num2);

     }

}

FREE Hit Counters