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..31e1ebc --- /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..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..ce2d5ab --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,207 @@ + + + + + + + + + + + + { + "lastFilter": { + "state": "OPEN", + "assignee": "smlcolinDEV" + } +} + { + "selectedUrlAndAccountId": { + "url": "https://github.com/smlcolinDEV/lab-java-basics.git", + "accountId": "b480d773-6a55-4125-9d5c-0e9a6f8f7126" + } +} + + + { + "customColor": "", + "associatedIndex": 0 +} + + + + { + "keyToString": { + "Application.Basics.executor": "Run", + "Application.Company.executor": "Run", + "RunOnceActivity.ShowReadmeOnStart": "true", + "RunOnceActivity.git.unshallow": "true", + "git-widget-placeholder": "main", + "kotlin-language-version-configured": "true", + "last_opened_file_path": "C:/Users/smlco/Documents/study/java/Week-3-Day-1", + "project.structure.last.edited": "Modules", + "project.structure.proportion": "0.0", + "project.structure.side.proportion": "0.0" + } +} + + + + + + + + + + 1743208028728 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Basics.java b/src/Basics.java new file mode 100644 index 0000000..894f2dd --- /dev/null +++ b/src/Basics.java @@ -0,0 +1,75 @@ +public class Basics { + public static void main(String[] args) { + // Declare and initialize a list of integers. + int[] list = {12, 90, 56, 1, 2, 78}; + + // Call methods to calculate the difference between the largest and smallest element, + // and to find the two smallest elements in the list. + diffLargestAndSmallest(list); + firstAndSecondSmallest(list); + } + + /** + * Method to calculate the difference between the largest and smallest element in an array. + * @param list The array of integers. + */ + public static void diffLargestAndSmallest(int[] list) { + // Initialize variables to store the smallest and largest elements. + int smallest = list[0]; + int largest = list[0]; + + // Check that the list is not empty. + if (list.length > 0) { + // Iterate through each element in the list. + for (int j : list) { + // Update the smallest element if the current element is smaller. + if (smallest > j) { + smallest = j; + } + // Update the largest element if the current element is larger. + if (largest < j) { + largest = j; + } + } + } + + // Calculate the difference between the largest and smallest elements. + int diff = largest - smallest; + + // Print the result. + System.out.println("The difference between the smallest (" + smallest + ") and largest (" + largest + ") integer in this list is: " + diff); + } + + /** + * Method to find the two smallest elements in an array. + * @param list The array of integers. + */ + public static void firstAndSecondSmallest(int[] list) { + // Check that the list contains at least two elements. + if (list.length < 2) { + System.out.println("Array must have at least two elements."); + return; + } + + // Initialize variables to store the two smallest elements. + int firstSmallest = Integer.MAX_VALUE; + int secondSmallest = Integer.MAX_VALUE; + + // Iterate through each element in the array. + for (int i : list) { + // Update the first smallest element if the current element is smaller. + if (firstSmallest > i) { + secondSmallest = firstSmallest; + firstSmallest = i; + } + // Update the second-smallest element if the current element is smaller than the second smallest but larger than the first smallest. + else if (secondSmallest > i) { + secondSmallest = i; + } + } + + // Print the two smallest elements. + System.out.println("First smallest: " + firstSmallest); + System.out.println("Second smallest: " + secondSmallest); + } +} diff --git a/src/Company.java b/src/Company.java new file mode 100644 index 0000000..20c1823 --- /dev/null +++ b/src/Company.java @@ -0,0 +1,24 @@ +public class Company { + public static void main(String[] args) { + // Create an array to hold 10 Employee objects. + Employee[] team = new Employee[10]; + + // Initialize each element of the array with an Intern object. + team[0] = new Intern("Salvatore", 18, 20000); + team[1] = new Intern("Céline", 22, 3400); + team[2] = new Intern("Alex", 24, 4200); + team[3] = new Intern("Elodie", 19, 6500); + team[4] = new Intern("Rosca", 27, 1500); + team[5] = new Intern("Ahmad", 33, 15000); + team[6] = new Intern("Cesar", 45, 16000); + team[7] = new Intern("Elise", 24, 2000); + team[8] = new Intern("Tino", 50, 13000); + team[9] = new Intern("Sébastien", 26, 10000); + + // Iterate through each Employee in the team array. + for (Employee member : team) { + // Print the string representation of each Employee. + System.out.println(member.toString()); + } + } +} diff --git a/src/Employee.java b/src/Employee.java new file mode 100644 index 0000000..74eba49 --- /dev/null +++ b/src/Employee.java @@ -0,0 +1,53 @@ +public class Employee { + // Private fields to store the employee's name, age, and salary. + private String name; + private int age; + private int salary; + + // Constructor to initialize the Employee object with name, age, and salary. + public Employee(String name, int age, int salary) { + this.name = name; + this.age = age; + this.salary = salary; + } + + // Getter method for the name field. + public String getName() { + return name; + } + + // Setter method for the name field. + public void setName(String name) { + this.name = name; + } + + // Getter method for the age field. + public int getAge() { + return age; + } + + // Setter method for the age field. + public void setAge(int age) { + this.age = age; + } + + // Getter method for the salary field. + public int getSalary() { + return salary; + } + + // Setter method for the salary field. + public void setSalary(int salary) { + this.salary = salary; + } + + // Override the toString method to provide a string representation of the Employee object. + @Override + public String toString() { + return "Employee{" + + "name='" + name + '\'' + + ", age=" + age + + ", salary=" + salary + + '}'; + } +} diff --git a/src/Intern.java b/src/Intern.java new file mode 100644 index 0000000..eadd3d2 --- /dev/null +++ b/src/Intern.java @@ -0,0 +1,28 @@ +public class Intern extends Employee { + // Define a constant for the maximum salary an intern can have. + private static final int maxSalary = 20000; + + // Constructor for the Intern class. + public Intern(String name, int age, int salary) { + // Call the constructor of the superclass (Employee) with the provided name, age, and salary. + super(name, age, salary); + + // Check if the provided salary exceeds the maximum allowed salary. + if (salary > maxSalary) { + // Throw an IllegalArgumentException if the salary is too high. + throw new IllegalArgumentException("You cannot set a salary that exceeds " + maxSalary + " for " + name); + } + } + + // Method to update the salary of the intern. + public void updateSalary(int newSalary) { + // Check if the new salary is less than the maximum allowed salary. + if (newSalary < 20000) { + // Update the salary using the superclass's setSalary method. + super.setSalary(newSalary); + } else { + // Throw an IllegalArgumentException if the new salary is too high. + throw new IllegalArgumentException("You cannot set a salary that exceeds " + maxSalary + " for " + super.getName()); + } + } +}