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
48 changes: 48 additions & 0 deletions src/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
public class Employee {
// Properties
private String name;
private int id;
private double salary;

// Constructor
public Employee(String name, int id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}

// Getters and Setters
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 double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

// toString method to print properties
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", id=" + id +
", salary=" + salary +
'}';
}
}
31 changes: 31 additions & 0 deletions src/Intern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class Intern extends Employee {
// Salary limit constant
private static final double MAX_SALARY = 20000.0;

public Intern(String name, int id, double salary) {
super(name, id, salary);
// Validate salary on creation
if (salary > MAX_SALARY) {
throw new IllegalArgumentException("Intern salary cannot exceed " + MAX_SALARY);
}
}

// Override setter to validate on update
@Override
public void setSalary(double salary) {
if (salary > MAX_SALARY) {
throw new IllegalArgumentException("Intern salary cannot exceed " + MAX_SALARY);
} else {
super.setSalary(salary);
}
}

@Override
public String toString() {
return "Intern{" +
"name='" + getName() + '\'' +
", id=" + getId() +
", salary=" + getSalary() +
'}';
}
}
87 changes: 87 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
public class Main {

public static void main(String[] args) {
// --- TASK 1: Difference between max and min ---
System.out.println("--- Task 1: Difference Between Max and Min ---");
int[] numbers1 = {5, 10, 2, 8, 15, 1};
int diff = getDifference(numbers1);
System.out.println("Difference between largest and smallest: " + diff);

// --- TASK 2: Smallest and second smallest ---
System.out.println("\n--- Task 2: Smallest and Second Smallest ---");
int[] numbers2 = {12, 3, 5, 1, 9, 2};
findAndPrintSmallest(numbers2);

// --- TASK 5: Create 10 Employees and print ---
System.out.println("\n--- Task 5: List of 10 Employees ---");
createAndPrintEmployees();
}

// TASK 1: Method to get the difference
public static int getDifference(int[] arr) {
if (arr == null || arr.length < 1) {
throw new IllegalArgumentException("Array must have at least 1 element");
}

int max = arr[0];
int min = arr[0];

// Using loops and conditionals
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}

return max - min;
}

// TASK 2: Method to find and print the two smallest elements
public static void findAndPrintSmallest(int[] arr) {
if (arr == null || arr.length < 2) {
System.out.println("Array needs at least 2 elements for this task.");
return;
}

int first = Integer.MAX_VALUE;
int second = Integer.MAX_VALUE;

for (int i = 0; i < arr.length; i++) {
if (arr[i] < first) {
second = first;
first = arr[i];
} else if (arr[i] < second && arr[i] != first) {
second = arr[i];
}
}

if (second == Integer.MAX_VALUE) {
System.out.println("Smallest: " + first + ", Second smallest: Does not exist (equal values)");
} else {
System.out.println("Smallest: " + first + ", Second smallest: " + second);
}
}

// TASK 5: Create 10 employees and print
public static void createAndPrintEmployees() {
Employee[] employees = new Employee[10];

employees[0] = new Employee("Juan Perez", 101, 30000);
employees[1] = new Intern("Ana Gomez", 102, 15000);
employees[2] = new Employee("Carlos Ruiz", 103, 45000);
employees[3] = new Intern("Luis Torres", 104, 18000);
employees[4] = new Employee("Maria Diaz", 105, 32000);
employees[5] = new Intern("Pedro Sanchez", 106, 20000);
employees[6] = new Employee("Sofia Lima", 107, 50000);
employees[7] = new Intern("Jorge Castro", 108, 12000);
employees[8] = new Employee("Elena Nito", 109, 28000);
employees[9] = new Employee("Raul Bo", 110, 35000);

for (int i = 0; i < employees.length; i++) {
System.out.println("Employee " + (i + 1) + ": " + employees[i].toString());
}
}
}