From e52a1ecf8b53d298b2a977507b1df1dafe346532 Mon Sep 17 00:00:00 2001 From: Chigumira Date: Wed, 1 Apr 2026 12:12:24 +0200 Subject: [PATCH] This is Week2 Java Basic mandatory lab task --- Employee.java | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ Intern.java | 29 +++++++++++++++++++++ List.java | 64 +++++++++++++++++++++++++++++++++++++++++++++ Main.java | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+) create mode 100644 Employee.java create mode 100644 Intern.java create mode 100644 List.java create mode 100644 Main.java diff --git a/Employee.java b/Employee.java new file mode 100644 index 0000000..e5b3f74 --- /dev/null +++ b/Employee.java @@ -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); + } +} \ No newline at end of file diff --git a/Intern.java b/Intern.java new file mode 100644 index 0000000..edeff56 --- /dev/null +++ b/Intern.java @@ -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); + } + } +} \ No newline at end of file diff --git a/List.java b/List.java new file mode 100644 index 0000000..2a53035 --- /dev/null +++ b/List.java @@ -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); + } +} \ No newline at end of file diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..00154d6 --- /dev/null +++ b/Main.java @@ -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(); + } + } +} \ No newline at end of file