diff --git a/src/Employee.java b/src/Employee.java new file mode 100644 index 0000000..f78d9cf --- /dev/null +++ b/src/Employee.java @@ -0,0 +1,21 @@ +public class Employee { + private String name; + private double salary; + + public Employee(String name, double salary) { + this.name = name; + this.salary = salary; + } + + // Getters y Setters + public String getName() { return name; } + public void setName(String name) { this.name = name; } + + public double getSalary() { return salary; } + public void setSalary(double salary) { this.salary = salary; } + + @Override + public String toString() { + return "Employee{name='" + name + "', salary=" + salary + "}"; + } +} \ No newline at end of file diff --git a/src/Intern.java b/src/Intern.java new file mode 100644 index 0000000..1da7bed --- /dev/null +++ b/src/Intern.java @@ -0,0 +1,24 @@ +public class Intern extends Employee { + // Constante: el límite de salario para pasantes + private static final double MAX_SALARY = 20000; + + public Intern(String name, double salary) { + // Validamos en el constructor usando una función privada + super(name, validateSalary(salary)); + } + + @Override + public void setSalary(double salary) { + // Validamos también al intentar actualizar el salario + super.setSalary(validateSalary(salary)); + } + + private static double validateSalary(double salary) { + return Math.min(salary, MAX_SALARY); + } + + @Override + public String toString() { + return "Intern (Max 20k){name='" + getName() + "', salary=" + getSalary() + "}"; + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..52527ed --- /dev/null +++ b/src/Main.java @@ -0,0 +1,57 @@ +public class Main { + public static void main(String[] args) { + // --- PRUEBA TAREA 1 & 2 --- + int[] myNumbers = {15, 2, 8, 25, 1, 30}; + + System.out.println("Diferencia Max-Min: " + calculateDifference(myNumbers)); + findSmallestElements(myNumbers); + + System.out.println("\n--- TAREA 5: LISTA DE 10 EMPLEADOS ---"); + + // Creamos un array para almacenar 10 objetos Employee o Intern + Employee[] staff = new Employee[10]; + + for (int i = 0; i < 10; i++) { + if (i % 2 == 0) { + staff[i] = new Employee("Staff_" + i, 35000 + (i * 1000)); + } else { + // Intentamos ponerle 25000, pero el código lo bajará a 20000 automáticamente + staff[i] = new Intern("Intern_" + i, 25000); + } + // Imprimimos todas las propiedades usando el toString() + System.out.println(staff[i]); + } + } + + // TAREA 1: Algoritmo de diferencia + public static int calculateDifference(int[] nums) { + if (nums.length < 1) return 0; + int min = nums[0]; + int max = nums[0]; + + for (int n : nums) { + if (n < min) min = n; + if (n > max) max = n; + } + return max - min; + } + + // TAREA 2: Los dos más pequeños + public static void findSmallestElements(int[] nums) { + if (nums.length < 2) return; + + int smallest = Integer.MAX_VALUE; + int secondSmallest = Integer.MAX_VALUE; + + for (int n : nums) { + if (n < smallest) { + secondSmallest = smallest; + smallest = n; + } else if (n < secondSmallest && n != smallest) { + secondSmallest = n; + } + } + System.out.println("El menor es: " + smallest); + System.out.println("El segundo menor es: " + secondSmallest); + } +} \ No newline at end of file diff --git a/src/Task2.java b/src/Task2.java new file mode 100644 index 0000000..70084cc --- /dev/null +++ b/src/Task2.java @@ -0,0 +1,23 @@ +public class ArrayMethods { + + public static void findSmallestTwo(int[] arr) { + // Inicializamos con el valor máximo posible para que cualquier número del array sea menor + int smallest = Integer.MAX_VALUE; + int secondSmallest = Integer.MAX_VALUE; + + for (int i = 0; i < arr.length; i++) { + if (arr[i] < smallest) { + // El que era el más pequeño pasa a ser el segundo + secondSmallest = smallest; + // El actual se convierte en el más pequeño + smallest = arr[i]; + } else if (arr[i] < secondSmallest && arr[i] != smallest) { + // Si es mayor que el pequeño pero menor que el segundo, actualizamos el segundo + secondSmallest = arr[i]; + } + } + + System.out.println("Smallest element: " + smallest); + System.out.println("Second smallest element: " + secondSmallest); + } +} \ No newline at end of file diff --git a/src/Task3.java b/src/Task3.java new file mode 100644 index 0000000..9f73ea5 --- /dev/null +++ b/src/Task3.java @@ -0,0 +1,22 @@ +public class Employee { + private String name; + private String id; + private double salary; + + // Constructor + public Employee(String name, String id, double salary) { + this.name = name; + this.id = id; + this.salary = salary; + } + + // Getters y Setters + public String getName() { return name; } + public void setName(String name) { this.name = name; } + + public String getId() { return id; } + public void setId(String id) { this.id = id; } + + public double getSalary() { return salary; } + public void setSalary(double salary) { this.salary = salary; } +} \ No newline at end of file diff --git a/src/Task4.java b/src/Task4.java new file mode 100644 index 0000000..6f65800 --- /dev/null +++ b/src/Task4.java @@ -0,0 +1,20 @@ +public class Intern extends Employee { + // Definimos el límite como una constante estática y final + private static final double MAX_SALARY_LIMIT = 20000.0; + + public Intern(String name, String id, double salary) { + // Usamos super() para llamar al constructor de Employee + // Si el salario es mayor al límite, lo bajamos automáticamente a 20000 + super(name, id, salary > MAX_SALARY_LIMIT ? MAX_SALARY_LIMIT : salary); + } + + @Override + public void setSalary(double salary) { + // Volvemos a validar por si intentan cambiar el salario después de creado + if (salary > MAX_SALARY_LIMIT) { + super.setSalary(MAX_SALARY_LIMIT); + } else { + super.setSalary(salary); + } + } +} \ No newline at end of file