diff --git a/lab-java-basics/.gitignore b/lab-java-basics/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/lab-java-basics/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/lab-java-basics/.idea/.gitignore b/lab-java-basics/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/lab-java-basics/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/lab-java-basics/.idea/encodings.xml b/lab-java-basics/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/lab-java-basics/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/lab-java-basics/.idea/misc.xml b/lab-java-basics/.idea/misc.xml new file mode 100644 index 0000000..eda147d --- /dev/null +++ b/lab-java-basics/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/lab-java-basics/.idea/vcs.xml b/lab-java-basics/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/lab-java-basics/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/lab-java-basics/pom.xml b/lab-java-basics/pom.xml new file mode 100644 index 0000000..ba48704 --- /dev/null +++ b/lab-java-basics/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + lab-java-basics + 1.0-SNAPSHOT + + + 24 + 24 + UTF-8 + + + \ No newline at end of file diff --git a/lab-java-basics/src/main/java/org/example/ArrayMath.java b/lab-java-basics/src/main/java/org/example/ArrayMath.java new file mode 100644 index 0000000..f13becf --- /dev/null +++ b/lab-java-basics/src/main/java/org/example/ArrayMath.java @@ -0,0 +1,48 @@ +package org.example; + +public class ArrayMath { + +// 1) Write a method in Java to get the difference between the largest and smallest values in an array of integers. +// The length of the array must be 1 and above. Use loops and conditionals to develop the algorithm. +// 2) Write a method in Java to find the smallest and second smallest elements of a given array and print it in the console. +// Use loops and conditionals to develop the algorithm. + + // Returns the difference between min and max of an array of Int + public static int getDiffOfMaxMinFrom(int[] args) { + int max = args[0]; + int min = args[0]; + + if (args.length > 1) { + for (int arg : args) { + if (arg > max) { + max = arg; + } else if (arg < min) { + min = arg; + } + } + return (min > 0) ? max - min : max + min; + } + else { + System.out.println("The length of the array must be 1 and above."); + return 0; + } + } + + // Prints in console the min 2 elements of an array + public static void getTwoSmallestNumbersFrom(int[] args) { + int min = args[0]; + int min2 = args[0]; + if (args.length > 2) { + for (int arg : args) { + if (arg < min) { + min2 = min; + min = arg; + } + } + System.out.println("The two smallest numbers are " + min + " and " + min2); + } + else { + System.out.println("The length of the array must be 2 and above."); + } + } +} diff --git a/lab-java-basics/src/main/java/org/example/Employee.java b/lab-java-basics/src/main/java/org/example/Employee.java new file mode 100644 index 0000000..abe19ba --- /dev/null +++ b/lab-java-basics/src/main/java/org/example/Employee.java @@ -0,0 +1,71 @@ +package org.example; + +import java.text.MessageFormat; + +public class Employee { + //Create an Employee class to represent an employee of a company. + // Add all relevant properties and behaviors that you might need but you have to include a salary property. + // Don't forget to add getters and setters. + + private String firstName; + private String lastName; + private String email; + private String phoneNumber; + private double salary; + private String position; + + // Constructor + public Employee(String firstName, String lastName, String email, String phoneNumber, double salary, String position) { + setFirstName(firstName); + setLastName(lastName); + setEmail(email); + setPhoneNumber(phoneNumber); + setSalary(salary); + setPosition(position); + } + + // Setters + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } + public void setEmail(String email) { + this.email = email; + } + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + public void setSalary(double salary) { + this.salary = salary<0 ? 0 : salary; + } + public void setPosition(String position) { + this.position = position; + } + + // Getters + public String getFirstName() { + return firstName; + } + public String getLastName() { + return lastName; + } + public String getEmail() { + return email; + } + public String getPhoneNumber() { + return phoneNumber; + } + public double getSalary() { + return salary; + } + public String getPosition() { + return position; + } + +// Methods + public String getInfo() { + return MessageFormat.format("Name: {0} \nLast Name: {1}\nEmail: {2}\nPhone: {3}\nSalary: {4}\nPosition: {5}", getFirstName(), getLastName(), getEmail(), getPhoneNumber(), getSalary(),getPosition()) ; + } +} diff --git a/lab-java-basics/src/main/java/org/example/Intern.java b/lab-java-basics/src/main/java/org/example/Intern.java new file mode 100644 index 0000000..0aaf775 --- /dev/null +++ b/lab-java-basics/src/main/java/org/example/Intern.java @@ -0,0 +1,21 @@ +package org.example; + +public class Intern extends Employee { +// Create an Intern class that extends from Employee. +// All the Interns have a salary limit of 20000 (constant). +// You must validate if an intern is created (or salary updated) with a bigger salary than the max. +// The max value is set. + + public Intern(String firstName, String lastName, String email, String phoneNumber, double salary, String position) { + super(firstName, + lastName, + email, + phoneNumber, + (salary<20000) ? salary : 20000, + position); + } + + @Override public void setSalary(double salary) { + super.setSalary((salary<20000) ? salary : 20000); + } +} diff --git a/lab-java-basics/src/main/java/org/example/Main.java b/lab-java-basics/src/main/java/org/example/Main.java new file mode 100644 index 0000000..a36757e --- /dev/null +++ b/lab-java-basics/src/main/java/org/example/Main.java @@ -0,0 +1,114 @@ +package org.example; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + +public class Main { + public static void main(String[] args) { + +// Exercise 1 + System.out.println("Exercise 1: Find the difference between the min and max elements of an array"); + + int[] numArray = {-1,-20,1,5,6,7,9999,-3000}; + System.out.println("Array: " + Arrays.toString(numArray)); + + int diff = ArrayMath.getDiffOfMaxMinFrom(numArray); + System.out.println("Diff: " + diff); + + // Add new line + System.out.println("\n"); + +// Exercise 2 + System.out.println("Exercise 2: Print on console the 2 minimum elements of an array"); + int[] numArray2 = {-1,-20,1,5,6,7,9999,-3000}; + System.out.println("Array: " + Arrays.toString(numArray2)); + + ArrayMath.getTwoSmallestNumbersFrom(numArray2); + + // Add new line + System.out.println("\n"); + +// Exercise 3 + System.out.println("Exercise 3: Create an Employee class"); + Employee employeeExample = new Employee( + "Konstantinos", + "Leivaditis", + "example@gmail.com", + "696114321", + 28000, + "Junior S&OP Analyst"); + + System.out.println("Employee: \n" + employeeExample.getInfo()); + + // Add new line + System.out.println("\n"); + +// Exercise 4 + System.out.println("Exercise 4: Create an Intern class extending from Employee. Interns cant get salary more than 20000."); + Intern intern = new Intern( + "Konstantinos", + "Leivaditis", + "example@gmail.com", + "696114321", + 28000, + "Junior S&OP Analyst"); + + // Set salary over max + intern.setSalary(2000000); + + System.out.println("Set intern salary to: " + 2000000); + System.out.println("Intern: \n" + intern.getInfo()); + + // Add new line + System.out.println("\n"); + +// Exercise 5 + System.out.println("Exercise 5: Create 5 employees"); + + List employees = new ArrayList<>(5); + + employees.add(new Employee ("Konstantinos", + "Leivaditis", + "example@gmail.com", + "696114321", + 28000, + "Junior S&OP Analyst")); + + employees.add(new Employee ("Nick", + "Chair", + "example@gmail.com", + "696114321", + 70000, + "S&OP Analyst III")); + + employees.add(new Employee ("Sonia", + "ABA", + "example@gmail.com", + "696114321", + 60000, + "S&OP Analyst II")); + + employees.add(new Employee ("Hsien", + "Conte", + "example@gmail.com", + "696114321", + 60000, + "iOS Developer II")); + + employees.add(new Intern ("Guido", + "Arnau", + "example@gmail.com", + "696114321", + 30000, + "Intern S&OP Analyst")); + + int flag = 1; + for (Employee employee : employees) { + System.out.println("Employee number " + flag + ":\n" + employee.getInfo() + "\n"); + flag++; + } + + } +} \ No newline at end of file