From efdff43fd9a0d5484f6186268972442609580bf4 Mon Sep 17 00:00:00 2001 From: hollu Date: Sat, 29 Mar 2025 13:55:23 +0100 Subject: [PATCH] Finish lab --- src/Employee.java | 116 +++++++++++++++++++++++++++++++ src/ExerciseFiveApplication.java | 43 ++++++++++++ src/ExerciseFour.java | 24 +++++++ src/ExerciseOneApplication.java | 31 +++++++++ src/ExerciseTwoApplication.java | 37 ++++++++++ src/Intern.java | 18 +++++ 6 files changed, 269 insertions(+) create mode 100644 src/Employee.java create mode 100644 src/ExerciseFiveApplication.java create mode 100644 src/ExerciseFour.java create mode 100644 src/ExerciseOneApplication.java create mode 100644 src/ExerciseTwoApplication.java create mode 100644 src/Intern.java diff --git a/src/Employee.java b/src/Employee.java new file mode 100644 index 0000000..0cb07e3 --- /dev/null +++ b/src/Employee.java @@ -0,0 +1,116 @@ +public class Employee { + + // Private instance variables + private int employeeId; + private String firstName; + private String lastName; + private String email; + private String phoneNumber; + private String jobTitle; + private double salary; + + // Constructor + public Employee(int employeeId, String firstName, String lastName, String email, + String phoneNumber, String jobTitle, double salary) { + this.employeeId = employeeId; + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + this.phoneNumber = phoneNumber; + this.jobTitle = jobTitle; + setSalary(salary); // Using setter to apply validation + } + + // Getter and Setter methods + public int getEmployeeId() { + return employeeId; + } + + public void setEmployeeId(int employeeId) { + this.employeeId = employeeId; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public String getJobTitle() { + return jobTitle; + } + + public void setJobTitle(String jobTitle) { + this.jobTitle = jobTitle; + } + + public double getSalary() { + return salary; + } + + public void setSalary(double salary) { + if (salary >= 0) { + this.salary = salary; + } else { + throw new IllegalArgumentException("Salary must be non-negative."); + } + } + + // Behaviors + public void work() { + System.out.println(firstName + " " + lastName + " is working as a " + jobTitle + "."); + } + + public double getAnnualSalary() { + return salary * 12; + } + + public void raiseSalary(double percentage) { + if (percentage > 0) { + salary += salary * (percentage / 100); + } else { + throw new IllegalArgumentException("Raise percentage must be positive."); + } + } + + @Override + public String toString() { + return "Employee{" + + "employeeId=" + employeeId + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", email='" + email + '\'' + + ", phoneNumber='" + phoneNumber + '\'' + + ", jobTitle='" + jobTitle + '\'' + + ", salary=" + salary + + '}'; + } +} + + diff --git a/src/ExerciseFiveApplication.java b/src/ExerciseFiveApplication.java new file mode 100644 index 0000000..669fd55 --- /dev/null +++ b/src/ExerciseFiveApplication.java @@ -0,0 +1,43 @@ +public class ExerciseFiveApplication { + + public static void main(String[] args) { + Intern intern= new Intern(146, "Sten", "Mabel", "sten@gmail.com", "+2387878434", "IT-Specialist Intern", 40000); + Intern intern2= new Intern(2445, "Sara", "Kie", "kie@hotmail.com", "0492323457", "HR-Specialist Intern", 35000); + Intern intern3= new Intern(3555, "Luca", "kelvin", "luca@yahoo.com", "+99876345", "Marketing Specialist Intern", 42000); + System.out.println(intern); + System.out.println(intern2); + System.out.println(intern3); + // Array to hold 10 Employee objects + Employee[] employees = new Employee[10]; + + // Initialize each Employee object with sample data + employees[0] = new Employee(101, "Alice", "Smith", "alice.smith@example.com", + "123-456-7890", "Software Engineer", 70000); + employees[1] = new Employee(102, "Bob", "Johnson", "bob.johnson@example.com", + "234-567-8901", "Data Analyst", 65000); + employees[2] = new Employee(103, "Charlie", "Williams", "charlie.williams@example.com", + "345-678-9012", "Product Manager", 80000); + employees[3] = new Employee(104, "David", "Brown", "david.brown@example.com", + "456-789-0123", "UX Designer", 72000); + employees[4] = new Employee(105, "Eva", "Davis", "eva.davis@example.com", + "567-890-1234", "Quality Assurance", 68000); + employees[5] = new Employee(106, "Frank", "Miller", "frank.miller@example.com", + "678-901-2345", "DevOps Engineer", 75000); + employees[6] = new Employee(107, "Grace", "Wilson", "grace.wilson@example.com", + "789-012-3456", "HR Manager", 71000); + employees[7] = new Employee(108, "Hannah", "Moore", "hannah.moore@example.com", + "890-123-4567", "Marketing Specialist", 69000); + employees[8] = new Employee(109, "Ian", "Taylor", "ian.taylor@example.com", + "901-234-5678", "Sales Executive", 67000); + employees[9] = new Employee(110, "Jane", "Anderson", "jane.anderson@example.com", + "012-345-6789", "Customer Support", 64000); + + // Iterate through the array and print details of each employee + for (Employee emp : employees) { + System.out.println(emp); + System.out.println("-------------------------------"); + } + } + } + + diff --git a/src/ExerciseFour.java b/src/ExerciseFour.java new file mode 100644 index 0000000..5baeb88 --- /dev/null +++ b/src/ExerciseFour.java @@ -0,0 +1,24 @@ +public class ExerciseFour { + public static void main(String[] args) { + // Creating an Employee object + Employee emp = new Employee(1010, "Olu", "Tosin", "tosin@gmail.com", + "015212099501", "Operational Lead", 45000); + + // Displaying employee details + System.out.println("Employee ID: " + emp.getEmployeeId()); + System.out.println("Name: " + emp.getFirstName() + " " + emp.getLastName()); + System.out.println("Email: " + emp.getEmail()); + System.out.println("Phone: " + emp.getPhoneNumber()); + System.out.println("Job Title: " + emp.getJobTitle()); + System.out.println("Monthly Salary: $" + emp.getSalary()); + System.out.println("Annual Salary: $" + emp.getAnnualSalary()); + + // Simulating work + emp.work(); + + // Giving a raise + emp.raiseSalary(35); + System.out.println("New Monthly Salary after 35% raise: $" + emp.getSalary()); + + } +} diff --git a/src/ExerciseOneApplication.java b/src/ExerciseOneApplication.java new file mode 100644 index 0000000..9b890e8 --- /dev/null +++ b/src/ExerciseOneApplication.java @@ -0,0 +1,31 @@ +public class ExerciseOneApplication { + public static int findDiff(int[] numbers) { + // Validate that the array has at least one element + if (numbers == null || numbers.length == 0) { + System.out.println("list of values"); ; + } + + // Initialize min and max with the first element of the array + int min = numbers[0]; + int max = numbers[0]; + + // Iterate through the array to find the minimum and maximum values + for (int i = 1; i < numbers.length; i++) { + if (numbers[i] < min) { + min = numbers[i]; + } else if (numbers[i] > max) { + max = numbers[i]; + } + } + + // Return the difference between the maximum and minimum values + return max - min; + } + + public static void main(String[] args) { + int[] array = {18, 56, 27, 99, 53}; + int difference = findDiff(array); + System.out.println("Difference between values: " + difference); + } +} + diff --git a/src/ExerciseTwoApplication.java b/src/ExerciseTwoApplication.java new file mode 100644 index 0000000..0619d38 --- /dev/null +++ b/src/ExerciseTwoApplication.java @@ -0,0 +1,37 @@ +public class ExerciseTwoApplication { + public static void findValue(int[] array) { + // Check if the array has at least two elements + if (array == null || array.length < 2) { + System.out.println("Array must contain at least two elements."); + return; + } + + // Initialize the smallest and second smallest with maximum possible values + int smallest = Integer.MAX_VALUE; + int secondSmallest = Integer.MAX_VALUE; + + // Iterate through the array to find the smallest and second smallest elements + for (int num : array) { + if (num < smallest) { + secondSmallest = smallest; + smallest = num; + } else if (num > smallest && num < secondSmallest) { + secondSmallest = num; + } + } + + // Check if a valid second smallest element was found + if (secondSmallest == Integer.MAX_VALUE) { + System.out.println("No distinct second smallest element found."); + } else { + System.out.println("The smallest element is " + smallest + + " and the second smallest element is " + secondSmallest + "."); + } + } + + public static void main(String[] args) { + int[] array = {52, 77, -8, 12, 14, 31}; + findValue(array); + } +} + diff --git a/src/Intern.java b/src/Intern.java new file mode 100644 index 0000000..ea72c49 --- /dev/null +++ b/src/Intern.java @@ -0,0 +1,18 @@ +public class Intern extends Employee{ + + private final double MAX_SALARY = 20_000.0; + + public Intern(int employeeId, String firstName, String lastName, String email, String phoneNumber, String jobTitle, double salary) { + super(employeeId, firstName, lastName, email, phoneNumber, jobTitle, salary); + if (salary > MAX_SALARY){ + this.setSalary(MAX_SALARY); + } + } + + @Override + public String toString() { + return "Intern{" + + "MAX_SALARY=" + MAX_SALARY + + "} " + super.toString(); + } +}