0% found this document useful (0 votes)
6 views9 pages

Advanced Object-Oriented Programming in Java

The document contains Java code for two problems related to object-oriented programming. The first problem involves an employee management system with classes for permanent and contract employees, while the second problem implements a voting system with candidates and voters. Each system includes methods for adding, updating, and retrieving information, demonstrating key OOP principles.

Uploaded by

madhumithak1209
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)
6 views9 pages

Advanced Object-Oriented Programming in Java

The document contains Java code for two problems related to object-oriented programming. The first problem involves an employee management system with classes for permanent and contract employees, while the second problem implements a voting system with candidates and voters. Each system includes methods for adding, updating, and retrieving information, demonstrating key OOP principles.

Uploaded by

madhumithak1209
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

Advanced Object Oriented Programming-AOOP(A)

Course Code-23CS2103A
Name: K. Madhu mitha
ID Number: 2300090088

Skill-Week-5
Question-1:
package Problem1;

import [Link];

import [Link];

interface Employee {

String getId();

String getName();

double getSalary();

void setName(String name);

void setSalary(double salary);

class PermanentEmployee implements Employee {

private String id;

private String name;

private double salary;

private String benefits;

public PermanentEmployee(String id, String name, double salary, String benefits) {

[Link] = id;

[Link] = name;

[Link] = salary;

[Link] = benefits;

public String getId() {


return id;

public String getName() {

return name;

public double getSalary() {

return salary;

public String getBenefits() {

return benefits;

public void setName(String name) {

[Link] = name;

public void setSalary(double salary) {

[Link] = salary;

public void setBenefits(String benefits) {

[Link] = benefits;

class ContractEmployee implements Employee {

private String id;

private String name;

private double salary;

private int contractDuration;


public ContractEmployee(String id, String name, double salary, int contractDuration) {

[Link] = id;

[Link] = name;

[Link] = salary;

[Link] = contractDuration;

public String getId() {

return id;

public String getName() {

return name;

public double getSalary() {

return salary;

public int getContractDuration() {

return contractDuration;

public void setName(String name) {

[Link] = name;

public void setSalary(double salary) {

[Link] = salary;

public void setContractDuration(int contractDuration) {

[Link] = contractDuration;

}
}

class EmployeeManager {

private Map<String, Employee> employees = new HashMap<>();

public void addEmployee(Employee employee) {

[Link]([Link](), employee);

public void removeEmployee(String id) {

[Link](id);

public Employee getEmployee(String id) {

return [Link](id);

public void updateEmployee(String id, String name, double salary, String benefits,
Integer contractDuration) {

Employee employee = [Link](id);

if (employee != null) {

[Link](name);

[Link](salary);

if (employee instanceof PermanentEmployee) {

((PermanentEmployee) employee).setBenefits(benefits);

} else if (employee instanceof ContractEmployee) {

((ContractEmployee) employee).setContractDuration(contractDuration);

public class Main {

public static void main(String[] args) {


EmployeeManager manager = new EmployeeManager();

PermanentEmployee permanentEmployee = new PermanentEmployee("1", "John",


50000, "Health Insurance");

ContractEmployee contractEmployee = new ContractEmployee("2", "Jane", 40000,


12);

[Link](permanentEmployee);

[Link](contractEmployee);

[Link]("1", "John Doe", 55000, "Life Insurance", null);

[Link]("2", "Jane Smith", 45000, null, 18);

[Link]("1");

Employee retrieved = [Link]("2");

if (retrieved != null) {

[Link]([Link]());

} else {

[Link]("Employee not found.");

Output:
Question-2:
package Problem2;

import [Link];

import [Link];

import [Link];

import [Link];

class Candidate {

private String name;

private int votes;

public Candidate(String name) {

[Link] = name;

[Link] = 0;

public String getName() {

return name;

}
public int getVotes() {

return votes;

public void incrementVotes() {

votes++;

class VotingSystem {

private Map<String, Candidate> candidates = new HashMap<>();

private Set<String> voters = new HashSet<>();

public void addCandidate(String name) {

if ([Link](name)) {

throw new IllegalArgumentException("Candidate already exists.");

[Link](name, new Candidate(name));

public void vote(String voterId, String candidateName) {

if ([Link](voterId)) {

throw new IllegalArgumentException("Voter has already voted.");

Candidate candidate = [Link](candidateName);

if (candidate == null) {

throw new IllegalArgumentException("Candidate does not exist.");

[Link]();

[Link](voterId);
}

public int getVotes(String candidateName) {

Candidate candidate = [Link](candidateName);

if (candidate == null) {

throw new IllegalArgumentException("Candidate does not exist.");

return [Link]();

public class Main {

public static void main(String[] args) {

VotingSystem system = new VotingSystem();

[Link]("Alice");

[Link]("Bob");

[Link]("Voter1", "Alice");

[Link]("Voter2", "Bob");

[Link]("Voter3", "Alice");

[Link]("Votes for Alice: " + [Link]("Alice"));

[Link]("Votes for Bob: " + [Link]("Bob"));

try {

[Link]("Voter1", "Bob");

} catch (IllegalArgumentException e) {

[Link]([Link]());

}
try {

[Link]("Voter4", "Charlie");

} catch (IllegalArgumentException e) {

[Link]([Link]());

try {

[Link]("Votes for Charlie: " + [Link]("Charlie"));

} catch (IllegalArgumentException e) {

[Link]([Link]());

Output:

Common questions

Powered by AI

Exception handling is crucial for robust operation in both systems. In EmployeeManager, exceptions are not explicitly mentioned, indicating reliance on correct data inputs; improving this can prevent unforeseen issues during data manipulations. In contrast, VotingSystem explicitly uses exceptions to manage invalid states like duplicate voting or non-existent candidates, enhancing system reliability by preventing state corruption and guiding error recovery. This ensures both systems operate within expected parameters, although EmployeeManager can benefit from more explicit exceptions to handle edge cases.

In EmployeeManager, different employee types are handled using the 'instanceof' operator to cast and modify specific properties, allowing the system to distinguish behavior for PermanentEmployee and ContractEmployee. The VotingSystem, however, handles candidates through a hashmap where candidate names are keys, and uniqueness is managed strictly via exceptions if redundancies or invalid accesses occur. Both systems rely on runtime type checking and exception handling but differ in that EmployeeManager performs type-specific operations while VotingSystem uses data structure integrity checks.

The use of interfaces in EmployeeManager makes the code flexible and easier to maintain by allowing different employee types to be added with minimal changes. As each employee type implements the Employee interface, it guarantees the presence of essential methods such as getId, getName, and getSalary. Therefore, when modifying or extending the program, developers can introduce new employee types without altering existing code structure, following Open-Closed Principle, enhancing modularity and maintainability.

Potential issues include runtime exceptions if null values or incorrect types are passed (e.g., passing benefits for a ContractEmployee). This can be mitigated by implementing overloading in the updateEmployee method for different employee types or by using a more robust design pattern like Visitor or Factory to manage these differences more gracefully, allowing compile-time checks rather than runtime errors.

Encapsulation is applied by keeping employee data such as name, ID, salary, benefits, and contract duration private, accessible only through methods like getId and setName. This restricts direct access to the inner data, preventing unauthorized modifications. Benefits include maintaining integrity and consistency across the program, as changes can be controlled and validated before application, enhancing security and reducing error introduction.

The VotingSystem handles invalid votes by throwing exceptions. If a voter attempts to vote for a non-existent candidate, it throws an IllegalArgumentException stating 'Candidate does not exist.' Similarly, if a voter tries to vote more than once, another exception is thrown. This method of handling invalid votes prevents manipulation of the voting process, but it may also disrupt user experience if exceptions are not handled properly on the front end or caller method.

To handle multiple voting phases, the VotingSystem can be improved by introducing a voting phase identifier alongside the voter ID. Each phase can be tracked separately, allowing voters to participate in each round. Adding a data structure to maintain votes for each phase can help better organize and retrieve results. Additionally, implementing state management for transitions between phases, possibly with Observer or State design patterns, aids in scalability and tracking across election rounds.

The VotingSystem class maintains a set called 'voters' which stores unique IDs of voters who have already voted. Before allowing a vote, the system checks if the voter's ID is present in the set. If it is, the system throws an IllegalArgumentException indicating that the voter has already voted, thus ensuring that each voter can only vote once.

The EmployeeManager class uses the 'instanceof' operator to differentiate between PermanentEmployee and ContractEmployee. During the updateEmployee method, it checks if the employee is an instance of PermanentEmployee, then updates benefits using setBenefits(). If it's an instance of ContractEmployee, it updates contractDuration using setContractDuration(). This allows the method to handle both types of employees appropriately.

To prevent exceptions when adding an existing candidate, the 'addCandidate' method could be modified to check for existing candidates and update some properties or simply ignore the operation instead of throwing an exception. Alternatively, using a more sophisticated data management procedure such as flag-setting to affect voting logic while keeping consistent data states could also help manage the existing candidates more gracefully.

You might also like