diff --git a/src/main/java/Employee.java b/src/main/java/Employee.java new file mode 100644 index 0000000..08aa790 --- /dev/null +++ b/src/main/java/Employee.java @@ -0,0 +1,57 @@ +public class Employee { + // Properties + private String name; + private int age; + private String position; + private double salary; + + // Constructor + public Employee(String name, int age, String position, double salary) { + this.name = name; + this.age = age; + this.position = position; + this.salary = salary; + } + + // Getters + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + public String getPosition() { + return position; + } + + public double getSalary() { + return salary; + } + + // Setters + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void setPosition(String position) { + this.position = position; + } + + public void setSalary(double salary) { + this.salary = salary; + } + + // Method to display employee info + public void printInfo() { + System.out.println("Name: " + name + + ", Age: " + age + + ", Position: " + position + + ", Salary: " + salary); + } +} diff --git a/src/main/java/Intern.java b/src/main/java/Intern.java new file mode 100644 index 0000000..a0925b0 --- /dev/null +++ b/src/main/java/Intern.java @@ -0,0 +1,18 @@ +public class Intern extends Employee { + public static final double MAX_SALARY = 20000; + + public Intern(String name, int age, String position, double salary) { + super(name, age, position, 0); // start with 0, then set salary with validation + setSalary(salary); + } + + @Override + public void setSalary(double salary) { + if (salary > MAX_SALARY) { + System.out.println("Salary exceeds the maximum for interns. Setting to " + MAX_SALARY); + super.setSalary(MAX_SALARY); + } else { + super.setSalary(salary); + } + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..910e7c0 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,25 @@ +public class Main { + public static void main(String[] args) { + // Create an array of Employees + Employee[] employees = new Employee[10]; + + // Employees + employees[0] = new Employee("Alice", 30, "Manager", 50000); + employees[1] = new Employee("Bob", 25, "Developer", 40000); + employees[2] = new Employee("Charlie", 28, "Designer", 35000); + employees[3] = new Employee("Diana", 32, "HR", 30000); + employees[4] = new Employee("Ethan", 29, "Developer", 42000); + + // Interns + employees[5] = new Intern("Frank", 22, "Intern Developer", 18000); + employees[6] = new Intern("Grace", 21, "Intern Designer", 21000); // exceeds max + employees[7] = new Intern("Hannah", 23, "Intern HR", 15000); + employees[8] = new Intern("Ian", 20, "Intern Tester", 20000); + employees[9] = new Intern("Jack", 24, "Intern Developer", 25000); // exceeds max + + // Print all employees + for (Employee emp : employees) { + emp.printInfo(); + } + } +} diff --git a/src/main/java/NrDiff.java b/src/main/java/NrDiff.java new file mode 100644 index 0000000..6b01e9f --- /dev/null +++ b/src/main/java/NrDiff.java @@ -0,0 +1,24 @@ +public class NrDiff { + + public static void main(String[] args) { + int[] numbers = {1, 5, 9, 15}; + + // we create a more robust way check first values of array, in this case both are set to 1 + // now it does not matter what the start value of array is, previous logic assumed the next value in array always increased + + int min = numbers[0]; + int max = numbers[0]; + + // enhanced for-loop, we use num which checks each value of the array and assigns what it finds to min or max (depending on < or >) + + for (int num : numbers) { + if (num < min) min = num; + if (num > max) max = num; + } + + // print out the difference between max and min values + + System.out.println("Difference = " + (max - min)); + } + +} \ No newline at end of file diff --git a/src/main/java/README.md b/src/main/java/README.md new file mode 100644 index 0000000..d309d3d --- /dev/null +++ b/src/main/java/README.md @@ -0,0 +1,23 @@ +# Lab 1 – Java Exercises + +This repository contains solutions for Lab 1. + +## Task 1 – NrDiff.java +Calculates the difference between numbers in an array with specific constraints. + +## Task 2 – SmallestNr.java +Finds the smallest number in a given array using custom logic. + +## Task 3 - Employee.java +Employee class. Each one has properties. Getters and setters. + +## Task 4 - Intern.java +Intern class that extends from Employee. + +## Task 5 - Main.java +Creates 10 Employees and printing properties. + + + + +Each file is self-contained and includes comments explaining the logic. diff --git a/src/main/java/SmallestNr.java b/src/main/java/SmallestNr.java new file mode 100644 index 0000000..79bcba4 --- /dev/null +++ b/src/main/java/SmallestNr.java @@ -0,0 +1,26 @@ +public class SmallestNr { + public static void main(String[] args) { + + + int[] numbers = {4, 7, -20, 15}; + + int absMin = numbers[0]; // absolute minimum value + int secondMin = Integer.MAX_VALUE; // second min value + + for (int num : numbers) { + if (num < absMin) { // if num (which is 4 to start with) is smaller than absolute min value (which is 4 also) + secondMin = absMin; // this mean we have found a number smaller than absMin, update secondMin to reflect current second smallest nr + absMin = num; // update absMin to reflect current smallest nr + + // Otherwise, check if the current number is smaller than our current second smallest + // AND is not equal to the smallest (to avoid duplicates) + + } else if (num < secondMin && num != absMin) { + secondMin = num; + } + } + System.out.println("Smallest = " + absMin); + System.out.println("Second smallest = " + secondMin); + + } +}