Name: B.
Latha Sagar
RegNo: 12204652
Roll No: 42
Section: D2212
Course Code: CAP680
source code:
import java.util.Scanner;
class Person {
protected String name;
protected int age;
static int cur_id;
public Person() {
cur_id++;
}
public void getdata() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
this.name = sc.nextLine();
System.out.print("Enter age: ");
this.age = sc.nextInt();
}
public void putdata() {
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
}
}
class Professor extends Person {
private int publications;
public Professor() {
super();
}
public void getdata() {
super.getdata();
Scanner sc = new Scanner(System.in);
System.out.print("Enter publications: ");
this.publications = sc.nextInt();
}
public void putdata() {
super.putdata();
System.out.println("Publications: " + this.publications);
System.out.println("ID: " + cur_id);
}
}
class Student extends Person {
private int[] marks = new int[6];
public Student() {
super();
}
public void getdata() {
super.getdata();
Scanner sc = new Scanner(System.in);
System.out.print("Enter marks in 6 subjects: ");
for (int i = 0; i < 6; i++) {
marks[i] = sc.nextInt();
}
}
public void putdata() {
super.putdata();
int sum = 0;
for (int i = 0; i < 6; i++) {
sum += marks[i];
}
System.out.println("Total marks: " + sum);
System.out.println("ID: " + cur_id);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of professors: ");
int n = sc.nextInt();
Professor[] professors = new Professor[n];
for (int i = 0; i < n; i++) {
professors[i] = new Professor();
professors[i].getdata();
}
System.out.print("Enter the number of students: ");
n = sc.nextInt();
Student[] students = new Student[n];
for (int i = 0; i < n; i++) {
students[i] = new Student();
students[i].getdata();
}
for (Professor professor : professors) {
professor.putdata();
}
for (Student student : students) {
student.putdata();
}
}
}
Output: