From 229c80a0d42e276737068e17253a6bb472acdb5e Mon Sep 17 00:00:00 2001 From: Prabhat Bhanderi Date: Fri, 3 Apr 2026 23:18:53 +0200 Subject: [PATCH] solution-Java-Basics --- ArrayDifference.java | 32 ++++++++ Employee.java | 177 +++++++++++++++++++++++++++++++++++++++++++ Intern.java | 136 +++++++++++++++++++++++++++++++++ Main.java | 28 +++++++ SmallestTwo.java | 57 ++++++++++++++ 5 files changed, 430 insertions(+) create mode 100644 ArrayDifference.java create mode 100644 Employee.java create mode 100644 Intern.java create mode 100644 Main.java create mode 100644 SmallestTwo.java diff --git a/ArrayDifference.java b/ArrayDifference.java new file mode 100644 index 0000000..7252e88 --- /dev/null +++ b/ArrayDifference.java @@ -0,0 +1,32 @@ +public class ArrayDifference { + + public static int getDifference(int[] arr) { + + int largest = arr[0]; + int smallest = arr[0]; + + + for (int i = 1; i < arr.length; i++) { + if (arr[i] > largest) { + largest = arr[i]; + } + if (arr[i] < smallest) { + smallest = arr[i]; + } + } + + return largest - smallest; + } + + public static void main(String[] args) { + int[] arr1 = {3, 7, 1, 9, 4}; + int[] arr2 = {-5, -1, -8, -3}; + int[] arr3 = {42}; + int[] arr4 = {0, 100, -50, 75}; + + System.out.println(getDifference(arr1)); // 9 - 1 = 8 + System.out.println(getDifference(arr2)); // -1 - (-8) = 7 + System.out.println(getDifference(arr3)); // 42 - 42 = 0 + System.out.println(getDifference(arr4)); // 100 - (-50) = 150 + } +} \ No newline at end of file diff --git a/Employee.java b/Employee.java new file mode 100644 index 0000000..41230fc --- /dev/null +++ b/Employee.java @@ -0,0 +1,177 @@ +public class Employee { + + private int id; + private String firstName; + private String lastName; + private String email; + private String department; + private String jobTitle; + private double salary; + private int yearsOfExperience; + private boolean isActive; + + public Employee(int id, String firstName, String lastName, + String email, String department, + String jobTitle, double salary, int yearsOfExperience) { + + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + this.department = department; + this.jobTitle = jobTitle; + this.salary = salary; + this.yearsOfExperience = yearsOfExperience; + this.isActive = true; // new employees are active by default + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + 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 getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + 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) { + System.out.println("Salary cannot be negative. No changes made."); + return; + } + this.salary = salary; + } + + public int getYearsOfExperience() { + return yearsOfExperience; + } + + public void setYearsOfExperience(int yearsOfExperience) { + if (yearsOfExperience < 0) { + System.out.println("Years of experience cannot be negative. No changes made."); + return; + } + this.yearsOfExperience = yearsOfExperience; + } + + public boolean isActive() { + return isActive; + } + + public void setActive(boolean isActive) { + this.isActive = isActive; + } + + // Returns the employee's full name + public String getFullName() { + return firstName + " " + lastName; + } + + // Applies a raise by percentage (e.g. 10 = 10% raise) + public void applyRaise(double percentage) { + if (percentage <= 0) { + System.out.println("Raise percentage must be positive."); + return; + } + double raise = salary * (percentage / 100); + salary += raise; + System.out.printf("Raise applied! " + getFullName() + "'s new salary: " + salary); + } + + // Transfers the employee to a new department with an optional new job title + public void transferTo(String newDepartment, String newJobTitle) { + System.out.printf( + getFullName() + " transferred from " + department + " to " + newDepartment + " as " + newJobTitle); + this.department = newDepartment; + this.jobTitle = newJobTitle; + } + + // Marks the employee as inactive (resigned / terminated) + public void terminate() { + this.isActive = false; + System.out.println(getFullName() + " has been marked as inactive."); + } + + // Prints a formatted summary of the employee to the console + public void printProfile() { + System.out.println(" Employee Profile "); + System.out.println("ID : " + id); + System.out.println("Name : " + getFullName()); + System.out.println("Email : " + email); + System.out.println("Department : " + department); + System.out.println("Job Title : " + jobTitle); + System.out.println("Salary : " + salary); + System.out.println("Experience : " + yearsOfExperience + " year(s)"); + System.out.println("Status : " + (isActive ? "Active" : "Inactive")); + } + + public static void main(String[] args) { + + // Create an employee + Employee emp = new Employee( + 101, "Alice", "Johnson", "alice@company.com", + "Engineering", "Software Developer", 75000.00, 3); + + // Print initial profile + emp.printProfile(); + + // Apply a 10% raise + emp.applyRaise(10); + + // Transfer to a new department + emp.transferTo("Product", "Product Engineer"); + + // Try setting an invalid salary + emp.setSalary(-5000); + + // Print updated profile + emp.printProfile(); + + // Terminate the employee + emp.terminate(); + System.out.println("Is Active: " + emp.isActive()); + } +} \ No newline at end of file diff --git a/Intern.java b/Intern.java new file mode 100644 index 0000000..ca2e1ab --- /dev/null +++ b/Intern.java @@ -0,0 +1,136 @@ +public class Intern extends Employee { + + public static final double MAX_SALARY = 20000.00; + private String university; + private String internshipField; + private int durationMonths; + + public Intern(int id, String firstName, String lastName, + String email, String department, + double salary, String university, + String internshipField, int durationMonths) { + + // Pass "Intern" as the fixed job title up to Employee + super(id, firstName, lastName, email, department, + "Intern", validateSalary(salary), 0); + + this.university = university; + this.internshipField = internshipField; + this.durationMonths = durationMonths; + } + + private static double validateSalary(double salary) { + if (salary > MAX_SALARY) { + + System.out.println("Warning: Salary " + salary + " cannot exceed intern limit. Salary has been capped at " + + MAX_SALARY + "."); + return MAX_SALARY; + } + if (salary < 0) { + System.out.println("Warning: Salary cannot be negative. "); + return 0; + } + return salary; + } + + @Override + public void setSalary(double salary) { + super.setSalary(validateSalary(salary)); + } + + @Override + public void applyRaise(double percentage) { + if (percentage <= 0) { + System.out.println("Raise percentage must be positive."); + return; + } + double raisedSalary = getSalary() * (1 + percentage / 100); + if (raisedSalary > MAX_SALARY) { + super.setSalary(MAX_SALARY); + System.out.println( + "Raise applied but capped at intern limit. " + getFullName() + "'s new salary: " + MAX_SALARY); + } else { + super.setSalary(raisedSalary); + System.out.println("Raise applied! " + getFullName() + "'s new salary: " + getSalary()); + } + } + + public String getUniversity() { + return university; + } + + public void setUniversity(String university) { + this.university = university; + } + + public String getInternshipField() { + return internshipField; + } + + public void setInternshipField(String internshipField) { + this.internshipField = internshipField; + } + + public int getDurationMonths() { + return durationMonths; + } + + public void setDurationMonths(int durationMonths) { + if (durationMonths <= 0) { + System.out.println("Duration must be at least 1 month. No changes made."); + return; + } + this.durationMonths = durationMonths; + } + + @Override + public void printProfile() { + System.out.println(" Intern Profile "); + System.out.println("ID : " + getId()); + System.out.println("Name : " + getFullName()); + System.out.println("Email : " + getEmail()); + System.out.println("Department : " + getDepartment()); + System.out.println("Field : " + internshipField); + System.out.println("University : " + university); + System.out.println("Duration : " + durationMonths + " month(s)"); + System.out.println("Salary : $" + getSalary()); + System.out.println("Status : " + (isActive() ? "Active" : "Inactive")); + System.out.println("==================================="); + } + + public static void main(String[] args) { + + System.out.println(">>> Creating intern with valid salary ($15000):"); + Intern intern1 = new Intern( + 201, "Bob", "Smith", "bob@company.com", + "Engineering", 15000, + "TU Munich", "Software Engineering", 6); + intern1.printProfile(); + + System.out.println(">>> Creating intern with salary ABOVE the limit ($25000):"); + Intern intern2 = new Intern( + 202, "Sara", "Lee", "sara@company.com", + "Marketing", 25000, + "LMU Munich", "Digital Marketing", 3); + intern2.printProfile(); + + System.out.println(">>> Trying to update salary above limit via setSalary():"); + intern1.setSalary(22000); + System.out.println("Salary after update: $" + intern1.getSalary()); + + System.out.println(">>> Applying a raise that stays within the limit:"); + intern1.setSalary(15000); + intern1.applyRaise(10); // 15000 + 10% = 16500 → within limit + + System.out.println(">>> Applying a raise that exceeds the limit:"); + intern1.setSalary(19000); + intern1.applyRaise(20); // 19000 + 20% = 22800 → capped at 20000 + + System.out.println(">>> Creating intern with negative salary:"); + Intern intern3 = new Intern( + 203, "Tom", "Baker", "tom@company.com", + "HR", -5000, + "University of Augsburg", "HR Management", 4); + System.out.println("Salary after creation: $" + intern3.getSalary()); + } +} \ No newline at end of file diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..ff35126 --- /dev/null +++ b/Main.java @@ -0,0 +1,28 @@ +public class Main { + + public static void main(String[] args) { + + // Create an array of 10 Employees + Employee[] employees = new Employee[10]; + + employees[0] = new Employee(101, "Alice", "Johnson", "alice.johnson@company.com", "Engineering", "Software Developer", 75000.00, 3); + employees[1] = new Employee(102, "Bob", "Smith", "bob.smith@company.com", "Marketing", "Marketing Manager", 65000.00, 7); + employees[2] = new Employee(103, "Clara", "Davis", "clara.davis@company.com", "HR", "HR Specialist", 55000.00, 2); + employees[3] = new Employee(104, "David", "Wilson", "david.wilson@company.com", "Finance", "Financial Analyst", 70000.00, 5); + employees[4] = new Employee(105, "Eva", "Martinez","eva.martinez@company.com", "Engineering", "DevOps Engineer", 80000.00, 6); + employees[5] = new Employee(106, "Frank", "Brown", "frank.brown@company.com", "Sales", "Sales Representative", 48000.00, 1); + employees[6] = new Employee(107, "Grace", "Taylor", "grace.taylor@company.com", "Design", "UX Designer", 62000.00, 4); + employees[7] = new Employee(108, "Henry", "Anderson","henry.anderson@company.com", "Engineering", "Backend Developer", 78000.00, 8); + employees[8] = new Employee(109, "Isla", "Thomas", "isla.thomas@company.com", "Legal", "Legal Counsel", 90000.00, 10); + employees[9] = new Employee(110, "James", "Jackson", "james.jackson@company.com", "Operations", "Operations Manager", 68000.00, 9); + + // Print all employee profiles + for (Employee emp : employees) { + emp.printProfile(); + System.out.println(); + } + + // Summary + System.out.println("Total employees: " + employees.length); + } +} \ No newline at end of file diff --git a/SmallestTwo.java b/SmallestTwo.java new file mode 100644 index 0000000..63f0d00 --- /dev/null +++ b/SmallestTwo.java @@ -0,0 +1,57 @@ +public class SmallestTwo { + + public static void findTwoSmallest(int[] arr) { + // Need at least 2 elements to find a second smallest + if (arr.length < 2) { + System.out.println("Array must have at least 2 elements."); + return; + } + + int smallest = Integer.MAX_VALUE; + int secondSmallest = Integer.MAX_VALUE; + + // Single pass: update smallest and second smallest as we go + for (int i = 0; i < arr.length; i++) { + if (arr[i] < smallest) { + secondSmallest = smallest; // old smallest is pushed down + smallest = arr[i]; + } else if (arr[i] < secondSmallest && arr[i] != smallest) { + secondSmallest = arr[i]; + } + } + + if (secondSmallest == Integer.MAX_VALUE) { + System.out.println("No second smallest found (all elements are equal)."); + } else { + System.out.println("Smallest: " + smallest); + System.out.println("Second Smallest: " + secondSmallest); + } + } + + public static void main(String[] args) { + int[] arr1 = {5, 3, 8, 1, 4}; // Normal case + int[] arr2 = {-4, -1, -9, -2, -6}; // All negatives + int[] arr3 = {7, 7, 7, 7}; // All same → no second smallest + int[] arr4 = {7, 7, 3, 7}; // Duplicates with one different + int[] arr5 = {100, 5}; // Exactly two elements + int[] arr6 = {42}; // Too short + + System.out.println("--- arr1 ---"); + findTwoSmallest(arr1); + + System.out.println("--- arr2 ---"); + findTwoSmallest(arr2); + + System.out.println("--- arr3 ---"); + findTwoSmallest(arr3); + + System.out.println("--- arr4 ---"); + findTwoSmallest(arr4); + + System.out.println("--- arr5 ---"); + findTwoSmallest(arr5); + + System.out.println("--- arr6 ---"); + findTwoSmallest(arr6); + } +} \ No newline at end of file