Saturday, 4 November 2023

Create a package TYBBACA with two classes as class Student (Rno, SName, Per) with a method disp() to display details of N Students and class Teacher (TID, TName, Subject) with a method disp() to display the details of teacher who is teaching Java subject. (Make use of finalize() method and array of Object)-Core java slip24

 

Create a package TYBBACA with two classes as class Student (Rno, SName, Per) with a method disp() to display details of N Students and class Teacher (TID, TName, Subject) with a method disp() to display the details of teacher who is teaching Java subject. (Make use of finalize() method and array of Object)

Student.java

package bca;

public class Student

{

 

protected int Rno;

   protected String SName;

    protected double Per;

           

public Student()        {        }

public Student(int R, String S1,double P)

{   Rno=R;

    SName=S1;

    Per=P;   

   }

public void display()

{

System.out.println(Rno);

System.out.println(SName);

System.out.println(Per);

 

}

 

protected void finalize()

{

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

}}

Teacher.java

package bca;

public class Teacher

{

private int TID;

    private String TName;

    private String Subject;

public Teacher(){     }

 

public Teacher(int T, String TN, String Sub) {

        TID = T;

        TName = TN;

        Subject = Sub;

    }

 

 

public void display()

{

 

 System.out.println(TID);

System.out.println(TName);

System.out.println(Subject);

 

 }

protected void finalize()

{System.out.println("Teacher Details..."); }

}

Test.java

import bca.*;

public class Test

{

public static void main(String args[])

{

Student[] students = new Student[3];

students[0] = new Student(1, "Alice", 90.5);

students[1] = new Student(2, "Bob", 85.2);

students[2] = new Student(3, "Charlie", 78.0);

 

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

 

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

students[i].display();

 

System.out.println("Teacher Details...");

 

Teacher[] teachers = new Teacher[2];

        teachers[0] = new Teacher(101, "Mr. Smith", "Java");

        teachers[1] = new Teacher(102, "Ms. Johnson", "Math");

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

 teachers[i].display();

}

}

/*

C:\TYBBACA\bca>javac Teacher.java

 

C:\TYBBACA\bca>javac Student.java

 

C:\TYBBACA\bca>cd..

 

C:\TYBBACA>javac Test.java

 

C:\TYBBACA>java Test

Student Details...

1

Alice

90.5

2

Bob

85.2

3

Charlie

78.0

Teacher Details...

101

Mr. Smith

Java

102

Ms. Johnson

Math

*/

C:\TYBBACA>