From 3eb87c6ab5ce412a1306c508da04511f16d8e84a Mon Sep 17 00:00:00 2001 From: vanessa ozogu Date: Fri, 5 Sep 2025 23:36:49 +0200 Subject: [PATCH 1/2] Final Submission v4 --- lab-1/src/Employee.java | 49 ++++++++++++++++++++++++++++++++++++++ lab-1/src/Intern.java | 30 +++++++++++++++++++++++ lab-1/src/Main.java | 21 ++++++++++++++++ lab-1/src/arrayDiff.java | 23 ++++++++++++++++++ lab-1/src/twoSmallest.java | 31 ++++++++++++++++++++++++ 5 files changed, 154 insertions(+) create mode 100644 lab-1/src/Employee.java create mode 100644 lab-1/src/Intern.java create mode 100644 lab-1/src/Main.java create mode 100644 lab-1/src/arrayDiff.java create mode 100644 lab-1/src/twoSmallest.java diff --git a/lab-1/src/Employee.java b/lab-1/src/Employee.java new file mode 100644 index 0000000..f177ddb --- /dev/null +++ b/lab-1/src/Employee.java @@ -0,0 +1,49 @@ +public class Employee { + private String name; + private int id; + private String department; + private double salary; + private boolean isEmployed; + + public Employee(String name, int id, String department, double salary, boolean isEmployed) { + this.name = name; + this.id = id; + this.department = department; + this.salary = salary; + this.isEmployed = isEmployed(); + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + public String getDepartment() { + return department; + } + public void setDepartment(String department) { + this.department = department; + } + public double getSalary() { + return salary; + } + public void setSalary(double salary) { + this.salary = salary; + } + public Boolean isEmployed() { + return isEmployed; + } + public void setEmployed (boolean employed) { + this.isEmployed = employed; + } + public String toString() { + return "Employee [ID=" + id + ", Name=" + name + ", Department=" + department + + ", Salary=" + salary + ", Employed=" + isEmployed + "]"; + } +} diff --git a/lab-1/src/Intern.java b/lab-1/src/Intern.java new file mode 100644 index 0000000..70036a3 --- /dev/null +++ b/lab-1/src/Intern.java @@ -0,0 +1,30 @@ +public class Intern extends Employee { + private static final double MAX_SALARY = 20000; + + public Intern(String name, int id, String department, double salary, boolean isEmployed) { + super(name, id, department, validateSalary(salary), isEmployed); + super.setEmployed(isEmployed); + } + public Intern(String name, int id, String department, double salary) { + this(name, id, department, salary, true); + } + private static double validateSalary(double salary) { + if (salary > MAX_SALARY) { + System.out.println("Salary exceeds the limit for an Intern. Setting to " + MAX_SALARY); + return MAX_SALARY; + } + return salary; + } + public void setSalary(double salary) { + if (salary > MAX_SALARY) { + System.out.println("Salary exceeds limit for an Intern. Setting to within " + MAX_SALARY); + super.setSalary(MAX_SALARY); + } else { + super.setSalary(salary); + } + } + public String toString() { + return "Intern [ID=" + getId() + ", Name=" + getName() + ", Department=" + getDepartment() + + ", Salary=" + getSalary() + ", Employed=" + isEmployed() + "]"; + } +} diff --git a/lab-1/src/Main.java b/lab-1/src/Main.java new file mode 100644 index 0000000..9f26ed6 --- /dev/null +++ b/lab-1/src/Main.java @@ -0,0 +1,21 @@ +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + Employee[] employees_list = new Employee[10]; + + employees_list[0] = new Employee("Vanessa", 1, "HR", 50000, true); + employees_list[1] = new Employee("Vanessa_2", 2, "HR", 70000, true); + employees_list[2] = new Employee("Vanessa_3", 3, "HR", 45000, true); + employees_list[3] = new Employee("Vanessa_4", 4, "HR", 52000, true); + employees_list[4] = new Employee("Vanessa_5", 5, "HR", 50000, true); + employees_list[5] = new Employee("Vanessa_6", 6, "HR", 50000, true); + employees_list[6] = new Employee("Vanessa_7", 7, "HR", 50000, false); + employees_list[7] = new Employee("Vanessa_8", 8, "HR", 50000, false); + employees_list[8] = new Intern("Vanessa_9", 9, "HR", 21000, true); + employees_list[9] = new Intern("Audrey", 10, "HR", 15000, true); + } + for (Employee emp : employees_list) { + System.out.println(emp); + } +} \ No newline at end of file diff --git a/lab-1/src/arrayDiff.java b/lab-1/src/arrayDiff.java new file mode 100644 index 0000000..e8e62e1 --- /dev/null +++ b/lab-1/src/arrayDiff.java @@ -0,0 +1,23 @@ +public class arrayDiff { + + public static int getDiff (int[] nums) { + int minimum = nums[0]; + int maximum = nums[0]; + + for (int i = 1; i < nums.length; i++) { + if (nums[i] < minimum) { + minimum = nums[i]; + } else if (nums[i] > maximum) { + maximum = nums[i]; + } + } + + return maximum - minimum; + } + + public static void main(String[] args) { + int[] numbers = {-3, -2, -2, -1, 0, 1, 2, 3, 3, 4}; + int difference = getDiff(numbers); + System.out.println("The difference between the largest and smallest integer in the array is: " +difference); + } +} diff --git a/lab-1/src/twoSmallest.java b/lab-1/src/twoSmallest.java new file mode 100644 index 0000000..4c72521 --- /dev/null +++ b/lab-1/src/twoSmallest.java @@ -0,0 +1,31 @@ +public class twoSmallest { + + public static void findTwoSmallest(int[] nums) { + int smallest = Integer.MAX_VALUE; + int secondSmallest = Integer.MAX_VALUE; + + for (int i = 0; i < nums.length; i++) { + int current = nums[i]; + + if (current < smallest) { + secondSmallest = smallest; + smallest = current; + } else if (current < secondSmallest && current != smallest) { + secondSmallest = current; + } + } + if (secondSmallest == Integer.MAX_VALUE) { + System.out.println("Array does not have a second smallest element"); + } else { + System.out.println("Smallest: " + smallest); + System.out.println("Second Smallest: "+ secondSmallest); + } + } + + public static void main(String[] args) { + int[] numbers = {-3, -2, -2, -1, 0, 1, 2, 3, 3, 4}; + int[] numbers_2 = {1, 2, 3, 4, 5}; + findTwoSmallest(numbers); + findTwoSmallest(numbers_2); + } +} From 7392c5c6a203591aeec60dd932a809a69908c54a Mon Sep 17 00:00:00 2001 From: vanessa ozogu Date: Sat, 6 Sep 2025 10:08:48 +0200 Subject: [PATCH 2/2] Fixed errors in my Employee class and position of the for loop in the main class --- lab-1/src/Employee.java | 2 +- lab-1/src/Main.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lab-1/src/Employee.java b/lab-1/src/Employee.java index f177ddb..aa94611 100644 --- a/lab-1/src/Employee.java +++ b/lab-1/src/Employee.java @@ -10,7 +10,7 @@ public Employee(String name, int id, String department, double salary, boolean i this.id = id; this.department = department; this.salary = salary; - this.isEmployed = isEmployed(); + this.isEmployed = isEmployed; } public String getName() { return name; diff --git a/lab-1/src/Main.java b/lab-1/src/Main.java index 9f26ed6..95a0c20 100644 --- a/lab-1/src/Main.java +++ b/lab-1/src/Main.java @@ -14,8 +14,9 @@ public static void main(String[] args) { employees_list[7] = new Employee("Vanessa_8", 8, "HR", 50000, false); employees_list[8] = new Intern("Vanessa_9", 9, "HR", 21000, true); employees_list[9] = new Intern("Audrey", 10, "HR", 15000, true); - } + for (Employee emp : employees_list) { - System.out.println(emp); + System.out.println(emp); + } } } \ No newline at end of file