0% found this document useful (0 votes)
4 views3 pages

Untitled Document

The document defines a class hierarchy with a base class 'Person' and two derived classes 'Teacher' and 'Student'. Each class has attributes and methods for displaying information, checking maturity based on age, and computing salary or expenses. The 'MainClass' demonstrates the functionality by creating instances of 'Teacher' and 'Student' and invoking their methods.

Uploaded by

usman3761.com
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Untitled Document

The document defines a class hierarchy with a base class 'Person' and two derived classes 'Teacher' and 'Student'. Each class has attributes and methods for displaying information, checking maturity based on age, and computing salary or expenses. The 'MainClass' demonstrates the functionality by creating instances of 'Teacher' and 'Student' and invoking their methods.

Uploaded by

usman3761.com
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

class Person {

String name;
private String nic;
int age;
String qualification;
public Person(String name, String nic, int age, String qualification) {
this.name = name;
this.nic = nic;
this.age = age;
this.qualification = qualification;
}
public void displayNIC() {
System.out.println("NIC: " + nic);
}
public void checkMaturity() {
if (age > 30) {
System.out.println(name + " is Mature.");
} else {
System.out.println(name + " is Unmature.");
}
}
}

class Teacher extends Person {


int grade;

public Teacher(String name, String nic, int age, String qualification, int grade) {
super(name, nic, age, qualification);
this.grade = grade;
}

public void computeSalary() {


int salary;
if (grade == 16) {
salary = 50000 + 5000;
} else {
salary = 45000;
}
displayNIC();
System.out.println("Teacher Salary: " + salary);
}
}
class Student extends Person {
int tuitionFee;
int dailyExpense;

public Student(String name, String nic, int age, String qualification, int tuitionFee, int
dailyExpense) {
super(name, nic, age, qualification);
this.tuitionFee = tuitionFee;
this.dailyExpense = dailyExpense;
}

public void computeExpense() {


int totalExpense = tuitionFee + dailyExpense;
displayNIC();
System.out.println("Total Student Expense: " + totalExpense);
}
}

public class MainClass {


public static void main(String[] args) {
Teacher teacher = new Teacher("Usman", "12345-6789012-3", 19, "Masters", 16);
teacher.checkMaturity();
teacher.computeSalary();

System.out.println();

Student student = new Student("Asad", "98765-4321098-7", 35, "BSCS", 30000, 5000);


student.checkMaturity();
student.computeExpense();
}
}

You might also like