From 449136710d4a3c2d1253d1ac61648b6dbba3342d Mon Sep 17 00:00:00 2001 From: Danial Khan Date: Mon, 1 Sep 2025 14:40:49 +0200 Subject: [PATCH 1/2] Performed all tasks --- src/Task01/solution.java | 53 ++++++++++ src/Task02/solution.java | 52 ++++++++++ src/Task03/Employee.java | 88 ++++++++++++++++ src/Task03/Intern.java | 29 ++++++ src/Task03/ListEmployees.java | 182 ++++++++++++++++++++++++++++++++++ 5 files changed, 404 insertions(+) create mode 100644 src/Task01/solution.java create mode 100644 src/Task02/solution.java create mode 100644 src/Task03/Employee.java create mode 100644 src/Task03/Intern.java create mode 100644 src/Task03/ListEmployees.java diff --git a/src/Task01/solution.java b/src/Task01/solution.java new file mode 100644 index 0000000..840635b --- /dev/null +++ b/src/Task01/solution.java @@ -0,0 +1,53 @@ +package Task01; + +/* +Task: Write a method in Java to get the difference between the largest and smallest values in an array of integers. The length of the array must be 1 and above. Use loops and conditionals to develop the algorithm. + */ + +import java.util.Scanner; + +public class solution { + public static void main(String[] args) { + int n; // number of elements to read + + Scanner sc = new Scanner(System.in); // Scanner to read user input from standard input + System.out.println("Enter the number of elements in the array: "); + n = sc.nextInt(); // read array length + + // Validate that array length is at least 1, otherwise terminate early + if (n < 1) { + System.out.println("The length of the array must be greater than zero"); + return; + } + + int[] numbers = new int[n]; // allocate array with user-specified size + + // Read n integers from the user and populate the array + for (int i = 0; i < n; i++) { + System.out.println("Enter the element " + (i + 1) + ": "); + numbers[i] = sc.nextInt(); + } + + // Initialize both largest and smallest with the first element + int largest = Integer.MIN_VALUE; + int smallest = Integer.MAX_VALUE; + + // Traverse the array to find the true largest and smallest values + for (int i = 0; i < n; i++) { + // Update largest if the current element is greater + if (numbers[i] > largest) { + largest = numbers[i]; + } + + // Update smallest if the current element is smaller + if (numbers[i] < smallest) { + smallest = numbers[i]; + } + } + + // Output the results + System.out.println("The largest number is " + largest); + System.out.println("The smallest number is " + smallest); + System.out.println("The difference between largest and smallest is " + (largest - smallest)); + } +} diff --git a/src/Task02/solution.java b/src/Task02/solution.java new file mode 100644 index 0000000..c77239c --- /dev/null +++ b/src/Task02/solution.java @@ -0,0 +1,52 @@ +package Task02; + +/* +Write a method in Java to find the smallest and second smallest elements of a given array and print it in the console. Use loops and conditionals to develop the algorithm. + */ + +import java.util.Scanner; + +public class solution { + public static void main(String[] args) { + int n; // number of elements to read + + Scanner sc = new Scanner(System.in); // Scanner to read user input from standard input + System.out.println("Enter the number of elements in the array: "); + n = sc.nextInt(); // read array length + + // Validate that array length is at least 1, otherwise terminate early + if (n < 1) { + System.out.println("The length of the array must be greater than zero"); + return; + } + + int[] numbers = new int[n]; // allocate array with user-specified size + + // Read n integers from the user and populate the array + for (int i = 0; i < n; i++) { + System.out.println("Enter the element " + (i + 1) + ": "); + numbers[i] = sc.nextInt(); + } + + int smallest = Integer.MAX_VALUE; + int secondSmallest = Integer.MAX_VALUE; + + // Traverse the array to find the true largest and smallest values + for (int i = 0; i < n; i++) { + // Update the smallest element of array. + if (numbers[i] < smallest) { + secondSmallest = smallest; + smallest = numbers[i]; + } + + // Update the second smallest element of array. + if (numbers[i] < secondSmallest && numbers[i] != smallest) { + secondSmallest = numbers[i]; + } + } + + // Output results + System.out.println("The smallest number is " + smallest); + System.out.println("The secondSmallest number is " + secondSmallest); + } +} diff --git a/src/Task03/Employee.java b/src/Task03/Employee.java new file mode 100644 index 0000000..ac27345 --- /dev/null +++ b/src/Task03/Employee.java @@ -0,0 +1,88 @@ +package Task03; + +/* +1. Create an Employee class to represent an employee of a company. Add all relevant properties and behaviors that you might need but you have to include a salary property. Don't forget to add getters and setters. +2. Create an Intern class that extends from Employee. All the Interns have a salary limit of 20000 (constant). You must validate if an intern is created (or salary updated) with a bigger salary than the max. The max value is set. +3. Write a program that creates 10 Employees and print it all the properties. +*/ + +import java.util.Scanner; +public class Employee { + private int id; + private String empName; + private String designation; + private double salary; + private boolean isIntern = false; + + /** + * Sets the unique identifier for the employee. + */ + public void setId(int id) { + this.id = id; + } + + /** + * Sets the employee's display name. + */ + public void setEmpName(String empName) { + this.empName = empName; + } + + /** + * Sets the job title/role assigned to this employee. + */ + public void setDesignation(String designation) { + this.designation = designation; + } + + /** + * Sets the salary for this employee. + * Note: Intern overrides this method to enforce a max salary. + */ + public void setSalary(double salary) { + this.salary = salary; + } + + /** + * Marks this employee as an intern or not. This flag is informational; actual + * intern-specific constraints are implemented in Intern. + */ + public void setIsIntern(boolean isIntern) { + this.isIntern = isIntern; + } + + /** + * @return the unique identifier of the employee. + */ + public int getId() { + return this.id; + } + + /** + * @return the employee's display name. + */ + public String getEmpName() { + return this.empName; + } + + /** + * @return the role/designation of the employee. + */ + public String getDesignation() { + return this.designation; + } + + /** + * @return the current salary of the employee. + */ + public double getSalary() { + return this.salary; + } + + /** + * @return true if this employee is flagged as an intern; false otherwise. + */ + public boolean getIsIntern() { + return this.isIntern; + } +} diff --git a/src/Task03/Intern.java b/src/Task03/Intern.java new file mode 100644 index 0000000..a6c46d4 --- /dev/null +++ b/src/Task03/Intern.java @@ -0,0 +1,29 @@ +package Task03; + +/* +2. Create an Intern class that extends from Employee. All the Interns have a salary limit of 20000 (constant). You must validate if an intern is created (or salary updated) with a bigger salary than the max. The max value is set. + */ + +public class Intern extends Employee { + private static final double salaryLimit = 20000; + + /** + * Caps the salary to salaryLimit if a higher value is provided. + */ + @Override + public void setSalary(double salary) { + if (salary > salaryLimit) { + salary = salaryLimit; + } + + super.setSalary(salary); + } + + /** + * Ignores the provided designation and forces it to "Intern". + */ + @Override + public void setDesignation(String designation) { + super.setDesignation("Intern"); + } +} diff --git a/src/Task03/ListEmployees.java b/src/Task03/ListEmployees.java new file mode 100644 index 0000000..cfcc741 --- /dev/null +++ b/src/Task03/ListEmployees.java @@ -0,0 +1,182 @@ +package Task03; + +/* +3. Write a program that creates 10 Employees and print it all the properties. + */ + +import java.util.ArrayList; +import java.util.Scanner; + +public class ListEmployees { + static ArrayList employees = new ArrayList<>(); + + public static void generateEmployeeData() { + String[] Names = {"Danial Khan", "Quintin Wise", "Vivian Gonzalez","Marisa Fennell","Katie Morley","Broderick Shumate","Gunnar Alvarado","Anjali Rollins","Jimmy Ott","Brigid Martinez","Joe Wood"}; + String[] Designations = {"Chief","Central","District","Future","Chief","Human","Internal","Forward","International","Internal"}; + + for (int i = 1; i <= 8 ; i++) { + Employee emp = new Employee(); + emp.setId(i); + emp.setEmpName(Names[(int) (Math.random()*10)]); + emp.setDesignation(Designations[(int) (Math.random()*10)]); + emp.setSalary(Math.random()*50000); + + employees.add(emp); + } + + Intern internee = new Intern(); + internee.setId(9); + internee.setEmpName("John Doe"); + internee.setDesignation("internee"); + internee.setSalary(50000); + + employees.add(internee); + + Intern internee2 = new Intern(); + internee2.setId(10); + internee2.setEmpName("John Doe Jr"); + internee2.setDesignation("internee"); + internee2.setSalary(10000); + + employees.add(internee); + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + boolean exit = false; + int choice; + + generateEmployeeData(); + + // Main menu loop: read a choice and execute the corresponding action + do { + System.out.println("-------------MENU-------------"); + System.out.println("1 - Add an employee"); + System.out.println("2 - Update an employee"); + System.out.println("3 - Delete an employee"); + System.out.println("4 - Add an intern"); + System.out.println("------------------------------"); + System.out.println("5 - View all"); + System.out.println("0 - Exit"); + + choice = sc.nextInt(); + + switch (choice) { + case 1 -> addEmployee(false); + case 2 -> { + System.out.println("Enter employee id to update: "); + int empId = sc.nextInt(); + updateEmployee(empId); + } + case 3 -> { + System.out.println("Enter employee id to delete: "); + int empId = sc.nextInt(); + deleteEmployee(empId); + } + case 4 -> addEmployee(true); + case 5 -> listAll(); + case 0 -> exit = true; + } + + } while (!exit); + + sc.close(); + } + + /** + * Prints a simple table with all employees currently stored in ArrayList. + */ + static void listAll() { + System.out.printf("| %-6s | %-15s | %-15s | %-10s%n", "ID", "Name", "Designation", "Salary"); + System.out.println("=========================================================="); + for (Employee e : employees) { + System.out.printf("| %-6s | %-15s | %-15s | %-10s%n", e.getId(), e.getEmpName(), e.getDesignation(), Math.round(e.getSalary())); + } + } + + /** + * Reads fields from the console and adds either a regular Employee or an Intern. + * + * @param isIntern if true, fills the shared Intern instance; otherwise the Employee instance. + * We object via a ternary so we can call common setters + * on the Employee reference. + */ + static void addEmployee(boolean isIntern) { + Scanner sc = new Scanner(System.in); + + // Choose the underlying object based on the flag; both are Employees so + // we can call common getters/setters through the Employee reference. + Employee obj = isIntern ? new Intern() : new Employee(); + + System.out.println("Enter employee ID"); + obj.setId(sc.nextInt()); + sc.nextLine(); + + System.out.println("Enter employee name: "); + obj.setEmpName(sc.nextLine()); + + System.out.println("Enter employee designation: "); + obj.setDesignation(sc.nextLine()); + + System.out.println("Enter employee salary: "); + obj.setSalary(sc.nextDouble()); + + obj.setIsIntern(isIntern); + + System.out.println("Employee added Successfully"); + employees.add(obj); + } + + + /** + * Updates an existing employee by id. If not found, it reports and returns. + */ + static void updateEmployee(int id) { + // Find employee by id and update its fields + Employee _thisEmployee = null; + for (Employee e : employees) { + if (e.getId() == id) { + _thisEmployee = e; + break; + } + } + + if (_thisEmployee == null) { + System.out.println("Employee with id " + id + " not found."); + return; + } + + Scanner sc = new Scanner(System.in); + + System.out.println("Updating employee with id: " + id + ""); + + System.out.print("Name [" + _thisEmployee.getEmpName() + "]: "); + _thisEmployee.setEmpName(sc.nextLine()); + + System.out.print("Designation [" + _thisEmployee.getDesignation() + "]: "); + _thisEmployee.setDesignation(sc.nextLine()); + + System.out.print("Salary [" + _thisEmployee.getSalary() + "]: "); + _thisEmployee.setSalary(sc.nextDouble()); + + System.out.println("Employee: " + id + " updated successfully."); + } + + + /** + * Deletes an employee with the given id from the Array list. + */ + static void deleteEmployee(int id) { + if (employees.isEmpty()) { + System.out.println("Employee list is empty."); + return; + } + boolean removed = employees.removeIf(e -> e.getId() == id); + + if (removed) { + System.out.println("Employee with id " + id + " deleted successfully."); + } else { + System.out.println("Employee with id " + id + " not found."); + } + } +} From 3402ece9478365201274c4ef52708df68fb72014 Mon Sep 17 00:00:00 2001 From: Danial Khan Date: Mon, 1 Sep 2025 14:46:57 +0200 Subject: [PATCH 2/2] Improved generateEmployeeData method --- src/Task03/ListEmployees.java | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/Task03/ListEmployees.java b/src/Task03/ListEmployees.java index cfcc741..fb8a56d 100644 --- a/src/Task03/ListEmployees.java +++ b/src/Task03/ListEmployees.java @@ -14,8 +14,8 @@ public static void generateEmployeeData() { String[] Names = {"Danial Khan", "Quintin Wise", "Vivian Gonzalez","Marisa Fennell","Katie Morley","Broderick Shumate","Gunnar Alvarado","Anjali Rollins","Jimmy Ott","Brigid Martinez","Joe Wood"}; String[] Designations = {"Chief","Central","District","Future","Chief","Human","Internal","Forward","International","Internal"}; - for (int i = 1; i <= 8 ; i++) { - Employee emp = new Employee(); + for (int i = 1; i <= 10 ; i++) { + Employee emp = (i <= 8) ? new Employee() : new Intern(); emp.setId(i); emp.setEmpName(Names[(int) (Math.random()*10)]); emp.setDesignation(Designations[(int) (Math.random()*10)]); @@ -23,22 +23,6 @@ public static void generateEmployeeData() { employees.add(emp); } - - Intern internee = new Intern(); - internee.setId(9); - internee.setEmpName("John Doe"); - internee.setDesignation("internee"); - internee.setSalary(50000); - - employees.add(internee); - - Intern internee2 = new Intern(); - internee2.setId(10); - internee2.setEmpName("John Doe Jr"); - internee2.setDesignation("internee"); - internee2.setSalary(10000); - - employees.add(internee); } public static void main(String[] args) {