diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/lab-java-basics.iml b/.idea/lab-java-basics.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/.idea/lab-java-basics.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4eb4f51 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..505d07d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..8306744 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/out/production/lab-java-basics/Employee.class b/out/production/lab-java-basics/Employee.class new file mode 100644 index 0000000..9d841bc Binary files /dev/null and b/out/production/lab-java-basics/Employee.class differ diff --git a/out/production/lab-java-basics/Intern.class b/out/production/lab-java-basics/Intern.class new file mode 100644 index 0000000..94bc1cb Binary files /dev/null and b/out/production/lab-java-basics/Intern.class differ diff --git a/out/production/lab-java-basics/Main.class b/out/production/lab-java-basics/Main.class new file mode 100644 index 0000000..61f505f Binary files /dev/null and b/out/production/lab-java-basics/Main.class differ diff --git a/src/Employee.java b/src/Employee.java new file mode 100644 index 0000000..d333fb4 --- /dev/null +++ b/src/Employee.java @@ -0,0 +1,26 @@ +public class Employee { + private String name; + private String department; + private double salary; + + public Employee(String name, String department, double salary) { + this.name = name; + this.department = department; + this.salary = salary; + } + + // Getters y Setters + public String getName() { return name; } + public void setName(String name) { this.name = name; } + + public String getDepartment() { return department; } + public void setDepartment(String department) { this.department = department; } + + public double getSalary() { return salary; } + public void setSalary(double salary) { this.salary = salary; } + + @Override + public String toString() { + return "Employee [Name=" + name + ", Dept=" + department + ", Salary=" + salary + "]"; + } +} \ No newline at end of file diff --git a/src/Intern.java b/src/Intern.java new file mode 100644 index 0000000..d3ead4f --- /dev/null +++ b/src/Intern.java @@ -0,0 +1,34 @@ +public class Intern extends Employee { + // Límite constante de salario + public static final double MAX_SALARY = 20000; + + public Intern(String name, String department, double salary) { + // Llamamos al constructor del padre pero validamos el salario antes + super(name, department, validateSalary(salary)); + } + + // Método estático auxiliar para validar en el constructor + private static double validateSalary(double salary) { + if (salary > MAX_SALARY) { + System.out.println("Alerta: El salario excede el límite. Se ajustará al máximo permitido (" + MAX_SALARY + ")."); + return MAX_SALARY; + } + return salary; + } + + // Sobrescribimos el setter para proteger actualizaciones futuras + @Override + public void setSalary(double salary) { + if (salary > MAX_SALARY) { + System.out.println("Alerta de actualización: No se puede asignar " + salary + ". Se asignará " + MAX_SALARY + "."); + super.setSalary(MAX_SALARY); + } else { + super.setSalary(salary); + } + } + + @Override + public String toString() { + return "Intern [Name=" + getName() + ", Dept=" + getDepartment() + ", Salary=" + getSalary() + "]"; + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..055c077 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,76 @@ +public class Main { + public static void main(String[] args) { + + // --- Tarea 1 y 2: Pruebas de los algoritmos de Array --- + int[] numbers = {45, 12, 89, 2, 34, 1, 99, 12}; + + System.out.println("--- Arrays ---"); + System.out.println("Diferencia entre Max y Min: " + getDifference(numbers)); + printSmallestAndSecondSmallest(numbers); + System.out.println(); + + // --- Tarea 5: Crear 10 empleados (mezclando Employees e Interns) --- + System.out.println("--- Lista de Empleados ---"); + Employee[] company = new Employee[10]; + + company[0] = new Employee("Ana", "IT", 55000); + company[1] = new Employee("Luis", "HR", 42000); + company[2] = new Intern("Carlos", "Marketing", 15000); // Salario válido + company[3] = new Intern("Elena", "IT", 25000); // Salario inválido, se limitará a 20000 + company[4] = new Employee("Marta", "Sales", 60000); + company[5] = new Employee("Jorge", "Finance", 70000); + company[6] = new Intern("Pedro", "HR", 18000); + company[7] = new Employee("Sofia", "Legal", 80000); + company[8] = new Employee("Raul", "Sales", 52000); + company[9] = new Intern("Laura", "Design", 22000); // Salario inválido + + // Imprimir las propiedades de todos + for (int i = 0; i < company.length; i++) { + System.out.println(company[i].toString()); + } + } + + // Tarea 1: Diferencia entre el mayor y menor + public static int getDifference(int[] arr) { + if (arr == null || arr.length < 1) return 0; + + int min = arr[0]; + int max = arr[0]; + + for (int i = 1; i < arr.length; i++) { + if (arr[i] < min) { + min = arr[i]; + } + if (arr[i] > max) { + max = arr[i]; + } + } + return max - min; + } + + // Tarea 2: Encontrar el más pequeño y el segundo más pequeño + public static void printSmallestAndSecondSmallest(int[] arr) { + if (arr == null || arr.length < 2) { + System.out.println("El array debe tener al menos 2 elementos."); + return; + } + + int firstSmallest = Integer.MAX_VALUE; + int secondSmallest = Integer.MAX_VALUE; + + for (int i = 0; i < arr.length; i++) { + if (arr[i] < firstSmallest) { + secondSmallest = firstSmallest; + firstSmallest = arr[i]; + } else if (arr[i] < secondSmallest && arr[i] != firstSmallest) { + secondSmallest = arr[i]; + } + } + + if (secondSmallest == Integer.MAX_VALUE) { + System.out.println("No hay un segundo elemento más pequeño (todos son iguales)."); + } else { + System.out.println("Smallest: " + firstSmallest + " | Second Smallest: " + secondSmallest); + } + } +} \ No newline at end of file