Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions ArrayDifference.java
Original file line number Diff line number Diff line change
@@ -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
}
}
177 changes: 177 additions & 0 deletions Employee.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
136 changes: 136 additions & 0 deletions Intern.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
28 changes: 28 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading