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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/lab-java-basics.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added out/production/lab-java-basics/Employee.class
Binary file not shown.
Binary file added out/production/lab-java-basics/Intern.class
Binary file not shown.
Binary file added out/production/lab-java-basics/Main.class
Binary file not shown.
26 changes: 26 additions & 0 deletions src/Employee.java
Original file line number Diff line number Diff line change
@@ -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 + "]";
}
}
34 changes: 34 additions & 0 deletions src/Intern.java
Original file line number Diff line number Diff line change
@@ -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() + "]";
}
}
76 changes: 76 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}