Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package Week2LabTest;

/**
* Represents a generic employee in a company.
* Demonstrates encapsulation and clean OOP design.
*/
public class Employee {

private int id;
private String name;
private double salary;
private String department;

/**
* Parameterized constructor to initialize employee object.
*/
public Employee(int id, String name, double salary, String department) {
this.id = id;
this.name = name;
this.salary = salary;
this.department = department;
}

// ---------------- GETTERS ----------------

public int getId() {
return id;
}

public String getName() {
return name;
}

public double getSalary() {
return salary;
}

public String getDepartment() {
return department;
}

// ---------------- SETTERS ----------------

public void setId(int id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

/**
* Sets salary. Can be overridden in subclasses for validation.
*/
public void setSalary(double salary) {
this.salary = salary;
}

public void setDepartment(String department) {
this.department = department;
}

/**
* Prints all employee details.
*/
public void printDetails() {
System.out.println("ID: " + id +
", Name: " + name +
", Salary: " + salary +
", Department: " + department);
}
}
29 changes: 29 additions & 0 deletions Intern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Week2LabTest;

/**
* Represents an Intern, which is a specialized type of Employee.
* Enforces a maximum salary constraint.
*/
public class Intern extends Employee {

// Constant salary cap for all interns
public static final double MAX_SALARY = 20000;

public Intern(int id, String name, double salary, String department) {
super(id, name, 0, department); // initialize with safe salary
setSalary(salary); // apply validation
}

/**
* Overrides salary setter to enforce maximum salary rule.
*/
@Override
public void setSalary(double salary) {
if (salary > MAX_SALARY) {
System.out.println("Salary exceeds intern limit. Setting to max: " + MAX_SALARY);
super.setSalary(MAX_SALARY);
} else {
super.setSalary(salary);
}
}
}
64 changes: 64 additions & 0 deletions List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package Week2LabTest;

/**
* Utility class for array-related operations.
* Demonstrates use of loops, conditionals, and defensive programming.
*/
public class List {

/**
* Returns the difference between the largest and smallest values in the array.
*
* @param numbers input array (must contain at least 1 element)
* @return difference (max - min)
*/
public static int getDifference(int[] numbers) {
if (numbers == null || numbers.length < 1) {
throw new IllegalArgumentException("Array must contain at least one element.");
}

int min = numbers[0];
int max = numbers[0];

// Iterate through array to find min and max
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < min) {
min = numbers[i];
}

if (numbers[i] > max) {
max = numbers[i];
}
}

return max - min;
}

/**
* Finds and prints the smallest and second smallest values in the array.
*
* @param numbers input array (must contain at least 2 elements)
*/
public static void printTwoSmallest(int[] numbers) {
if (numbers == null || numbers.length < 2) {
System.out.println("Array must contain at least two elements.");
return;
}

int smallest = Integer.MAX_VALUE;
int secondSmallest = Integer.MAX_VALUE;

for (int number : numbers) {
// Update smallest and second smallest accordingly
if (number < smallest) {
secondSmallest = smallest;
smallest = number;
} else if (number < secondSmallest && number != smallest) {
secondSmallest = number;
}
}

System.out.println("Smallest: " + smallest);
System.out.println("Second Smallest: " + secondSmallest);
}
}
45 changes: 45 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package Week2LabTest;

/**
* Main class to test all functionality.
* Demonstrates:
* - Array algorithms
* - Object creation
* - Inheritance
* - Looping through objects
*/
public class Main {

public static void main(String[] args) {

// ---------------- ARRAY TESTS ----------------
int[] numbers = {5, 2, 9, 1, 7};

System.out.println("Difference between max and min: " + List.getDifference(numbers));

List.printTwoSmallest(numbers);

// ---------------- EMPLOYEE CREATION ----------------
Employee[] employees = new Employee[10];

employees[0] = new Employee(1, "Alice", 30000, "HR");
employees[1] = new Employee(2, "Bob", 35000, "IT");
employees[2] = new Employee(3, "Charlie", 40000, "Finance");
employees[3] = new Employee(4, "David", 45000, "Marketing");
employees[4] = new Employee(5, "Eve", 50000, "IT");
employees[5] = new Employee(6, "Frank", 55000, "HR");
employees[6] = new Employee(7, "Grace", 60000, "Finance");
employees[7] = new Employee(8, "Hank", 65000, "Marketing");
employees[8] = new Employee(9, "Ivy", 70000, "IT");

// Intern with salary exceeding limit (tests validation)
employees[9] = new Intern(10, "Jack", 25000, "Intern");

// ---------------- PRINT ALL EMPLOYEES ----------------
System.out.println("\n--- Employee Details ---");

for (Employee employee : employees) {
employee.printDetails();
}
}
}