Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

JavaLabassign4

The document contains a Java program that defines a class hierarchy for managing information about persons, specifically professors and students. It allows for input of personal data, including name, age, publications for professors, and marks for students, and outputs this data along with a unique ID. The program utilizes inheritance and encapsulation principles in object-oriented programming.

Uploaded by

NIKLAUS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JavaLabassign4

The document contains a Java program that defines a class hierarchy for managing information about persons, specifically professors and students. It allows for input of personal data, including name, age, publications for professors, and marks for students, and outputs this data along with a unique ID. The program utilizes inheritance and encapsulation principles in object-oriented programming.

Uploaded by

NIKLAUS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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:

You might also like