[Go to site: main page, start]

0% found this document useful (0 votes)
7 views10 pages

Java College Management System Lab

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

Java College Management System Lab

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA INHERITANCE LAB-2

[Link] a small application in java for College management system

i)A class Teacher contains two fields Name and Qualification. Extends the class to
department it contains dept. no and Dept Name.

ii)An interface named as college


a) it contains one field name of the college
b ) one abstract method getDetails, and showDetails
c) one default and static method which displays general message.
iii)Override base class and interface methods in derived class
Ask department name from user and check it must be either “IT dept” or Management
dept” .If department name does not match print “not a valid Record”
iv) For IT department department number is 10 and for management dept no is 20 .
v) Using the above classes and interface get the appropriate information and display it.
vi)Create a class “CMS “ having main method where all the above functions are called.

SOL: package ASSIGN2INHERITANCE;

import [Link];

interface College {

String collegeName = "ABC College";

void getDetails();

void showDetails();

default void generalMessage() {

[Link]("Welcome to " + collegeName + "!");

static void staticMessage() {

[Link]("This is a College Management System.");

}
class Teacher {

String name;

String qualification;

Teacher(String name, String qualification) {

[Link] = name;

[Link] = qualification;

class Department extends Teacher implements College {

int deptNo;

String deptName;

Department(String name, String qualification, String deptName) {

super(name, qualification);

[Link] = deptName;

setDeptNo(deptName);

private void setDeptNo(String deptName) {

if ([Link]("IT dept")) {

[Link] = 10;

} else if ([Link]("Management dept")) {

[Link] = 20;

} else {

[Link] = -1;
}

public void getDetails() {

Scanner sc = new Scanner([Link]);

[Link]("Enter Teacher Name:");

name = [Link]();

[Link]("Enter Qualification:");

qualification = [Link]();

[Link]("Enter Department Name (IT dept or Management dept):");

deptName = [Link]();

setDeptNo(deptName);

[Link]();

public void showDetails() {

if (deptNo == -1) {

[Link]("Not a valid Record");

return;

[Link]("\n--- Teacher Details ---");

[Link]("College Name: " + [Link]);

[Link]("Teacher Name: " + name);

[Link]("Qualification: " + qualification);

[Link]("Department: " + deptName);

[Link]("Department No: " + deptNo);

}
public class CMS {

public static void main(String[] args) {

Department dept = new Department("", "", "");

[Link]();

[Link]();

[Link]();

[Link]();

OUTPUT: Welcome to ABC College!

This is a College Management System.

Enter Teacher Name:

ABCD

Enter Qualification:

PHD

Enter Department Name (IT dept or Management dept):

IT dept

--- Teacher Details ---

College Name: ABC College

Teacher Name: ABCD

Qualification: PHD

Department: IT dept

Department No: 10
Q2. Create a Java program with the following components and a corresponding UML Class
Diagram:Abstract Class Employee:

 Attributes: String name, int employeeId, double baseSalary.

 Constructor to initialize these attributes.

 Abstract method: public abstract double calculateSalary().

 Concrete method: public void displayInfo() that prints the employee's name, ID, and
base salary.

Interface IProjectAssignable:

 Method signatures: public void assignToProject(String project), public List<String>


getAssignedProjects().

Concrete Class Developer:

 Extends Employee and implements IProjectAssignable.

 Additional attribute: double bonus.

 Override calculateSalary() to return baseSalary + bonus.

 Implement assignToProject() and getAssignedProjects() to manage a list of projects.

Concrete Class Manager:

 Extends Employee.

 Additional attributes: double projectBonus, int managedTeamSize.

 Override calculateSalary() to return baseSalary + (projectBonus * managedTeamSize).

 Implement an additional method specific to managers, e.g., public void


conductPerformanceReview().

Final Class HRStaff:

 Extends Employee.

 Override calculateSalary() to simply return baseSalary, as they don't receive


performance-based bonuses.

 Add a unique attribute String department.

Main Class (Main):

 Create objects of Developer, Manager, and HRStaff.

 Demonstrate polymorphism by calling calculateSalary() on each of them.


 Show how IProjectAssignable is used by calling assignToProject() on the Developer
object but not on the Manager or HRStaff objects, showcasing the power of
interfaces for specific functionalities.

 Call displayInfo() on all objects.

SOL: package ASSIGN2INHERITANCE;

import [Link];

import [Link];

// Abstract class Employee

abstract class Employee {

String name;

int employeeId;

double baseSalary;

public Employee(String name, int employeeId, double baseSalary) {

[Link] = name;

[Link] = employeeId;

[Link] = baseSalary;

public abstract double calculateSalary();

public void displayInfo() {

[Link]("\n--- Employee Info ---");

[Link]("Name: " + name);

[Link]("Employee ID: " + employeeId);

[Link]("Base Salary: $" + baseSalary);

// Interface

interface IProjectAssignable {

void assignToProject(String project);

List<String> getAssignedProjects();
}

// Developer Class

class Developer extends Employee implements IProjectAssignable {

double bonus;

List<String> projects = new ArrayList<>();

public Developer(String name, int id, double baseSalary, double bonus) {

super(name, id, baseSalary);

[Link] = bonus;

public double calculateSalary() {

return baseSalary + bonus;

public void assignToProject(String project) {

[Link](project);

public List<String> getAssignedProjects() {

return projects;

// Manager Class

class Manager extends Employee {

double projectBonus;

int managedTeamSize;

public Manager(String name, int id, double baseSalary, double projectBonus, int
teamSize) {

super(name, id, baseSalary);

[Link] = projectBonus;

[Link] = teamSize;
}

public double calculateSalary() {

return baseSalary + (projectBonus * managedTeamSize);

public void conductPerformanceReview() {

[Link](name + " is conducting a performance review.");

// Final HRStaff Class

final class HRStaff extends Employee {

String department;

public HRStaff(String name, int id, double baseSalary, String department) {

super(name, id, baseSalary);

[Link] = department;

public double calculateSalary() {

return baseSalary;

public class UML {

public static void main(String[] args) {

Developer dev = new Developer("Alice", 101, 50000, 10000);

[Link]("Banking App");

[Link]("AI Assistant");

Manager mgr = new Manager("Bob", 102, 60000, 3000, 5);

HRStaff hr = new HRStaff("Carol", 103, 45000, "Recruitment");

// Display Information

[Link]();
[Link]("Salary: $" + [Link]());

[Link]("Projects: " + [Link]());

[Link]();

[Link]("Salary: $" + [Link]());

[Link]();

[Link]();

[Link]("Salary: $" + [Link]());

OUTPUT: --- Employee Info ---

Name: SHIKHAR

Employee ID: 101

Base Salary: RS.50000.0

Salary: RS.60000.0

Projects: [Banking App, AI Assistant]

--- Employee Info ---

Name: VIRAT

Employee ID: 102

Base Salary: RS.60000.0

Salary: RS.75000.0

VIRAT is conducting a performance review.

--- Employee Info ---

Name: ROHIT

Employee ID: 103

Base Salary: RS.45000.0

Salary: RS.45000.0

Common questions

Powered by AI

Implementing multiple inheritance through interfaces allows the Java College management system to separate functionalities across different constructs. The 'Department' class inherits attributes from 'Teacher' while also implementing methods from the 'College' interface, securing both class and interface benefits. Interfaces enable the Department class to implement college-specific methods, like 'getDetails' and display functions, promoting a modular design without the complexity of class-based multiple inheritance .

The 'Developer' class calculates its salary by adding a bonus to the base salary, specifically using the formula baseSalary + bonus. In contrast, the 'Manager' class calculates its salary by adding the product of projectBonus and managedTeamSize to the base salary, according to the formula baseSalary + (projectBonus * managedTeamSize).

The 'Department' class in the Java College management system extends the abstract abilities of Java through its implementation of the 'College' interface, which contains abstract methods 'getDetails' and 'showDetails'. The class must provide concrete implementations for these methods, so it effectively uses abstraction to separate what an object can do from how it does it. Additionally, the department number setting logic encapsulates specific behavior, achieving abstraction of direct department assignment .

The 'CMS' class uses the 'Department' class to handle invalid department names. During initialization, if a provided department name does not match 'IT dept' or 'Management dept', the department number is set to -1. When 'showDetails' is called, it checks if `deptNo` is -1, and if so, prints out 'Not a valid Record' .

Interfaces play a crucial role in managing project assignments, especially with the 'Developer' class implementing the 'IProjectAssignable' interface, which provides method signatures for 'assignToProject' and 'getAssignedProjects'. This allows the 'Developer' class to maintain a project list and handle assignments dynamically. The 'Manager' and 'HRStaff' classes do not implement this interface, demonstrating selective capacity to be assigned projects, thereby encapsulating specific roles into interfaces .

The Java College management system uses the 'setDeptNo' method within the 'Department' class to ensure valid department names. This method checks if the input department name matches 'IT dept' or 'Management dept' using an 'if-else' statement. If it matches, it assigns appropriate department numbers (10 or 20, respectively); otherwise, it sets the department number to -1, logging 'Not a valid Record' to ensure input fidelity .

Polymorphism is demonstrated in the program by creating an array of the base class type 'Employee' which holds objects of 'Developer', 'Manager', and 'HRStaff'. Each of these objects overrides the 'calculateSalary' method to provide a class-specific implementation, allowing the same method call to behave differently based on the object type. Additionally, the 'displayInfo' method is called on each object showing their customized implementations .

The 'HRStaff' class might be declared final to prevent other classes from extending it, ensuring a fixed and consistent implementation of HR roles within the application. This decision stabilizes the core logic associated with HR salary calculations, which is simple (baseSalary only), and no performance-based bonuses are added. Consequently, the architecture is safeguarded against changes that could introduce unexpected behaviors through subclassing .

Default methods in interfaces enhance design flexibility by allowing new methods to be added to interfaces without breaking existing implementations. In the Java College management system, the 'College' interface defines 'generalMessage' as a default method, enabling any implementing class like 'Department' to inherit and use this method without needing to override it. This ensures backward compatibility and provides the opportunity for future extensions with minimal code changes .

The 'getDetails' method uses a Scanner object to solicit user input for teacher's name, qualification, and department name. While functional, it does not handle invalid inputs or exceptions, such as misspelled department names, imposing a constraint on user input. An improvement could involve implementing more robust input validation and exception handling to provide real-time feedback for incorrect inputs, ensuring better usability and error resistance .

You might also like