From 363374a6e7512827d9bf8328206589f0f04c3d28 Mon Sep 17 00:00:00 2001 From: Konstantinos Leivaditis Date: Sat, 30 Aug 2025 20:51:25 +0200 Subject: [PATCH 1/2] Lab 1 completed --- lab-java-basics/.gitignore | 38 ++++++++ lab-java-basics/.idea/.gitignore | 8 ++ lab-java-basics/.idea/encodings.xml | 7 ++ lab-java-basics/.idea/misc.xml | 14 +++ lab-java-basics/.idea/vcs.xml | 6 ++ lab-java-basics/pom.xml | 17 ++++ .../src/main/java/org/example/ArrayMath.java | 47 ++++++++++ .../src/main/java/org/example/Employee.java | 70 +++++++++++++++ .../src/main/java/org/example/Intern.java | 21 +++++ .../src/main/java/org/example/Main.java | 90 +++++++++++++++++++ 10 files changed, 318 insertions(+) create mode 100644 lab-java-basics/.gitignore create mode 100644 lab-java-basics/.idea/.gitignore create mode 100644 lab-java-basics/.idea/encodings.xml create mode 100644 lab-java-basics/.idea/misc.xml create mode 100644 lab-java-basics/.idea/vcs.xml create mode 100644 lab-java-basics/pom.xml create mode 100644 lab-java-basics/src/main/java/org/example/ArrayMath.java create mode 100644 lab-java-basics/src/main/java/org/example/Employee.java create mode 100644 lab-java-basics/src/main/java/org/example/Intern.java create mode 100644 lab-java-basics/src/main/java/org/example/Main.java 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..7ad96a0 --- /dev/null +++ b/lab-java-basics/src/main/java/org/example/ArrayMath.java @@ -0,0 +1,47 @@ +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. + + public static int getDiffOfMaxMinFrom(int[] args) { + int max = args[0]; + int min = args[0]; + System.out.println(args.length); + + 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; + } + } + + 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..c2a0e12 --- /dev/null +++ b/lab-java-basics/src/main/java/org/example/Employee.java @@ -0,0 +1,70 @@ +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..e25b281 --- /dev/null +++ b/lab-java-basics/src/main/java/org/example/Main.java @@ -0,0 +1,90 @@ +package org.example; + +import java.util.ArrayList; +import java.util.List; + +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + +// Lab 1 +// int[] numArray = {-1,-20,1,5,6,7,9999,-3000}; +// +// int diff = ArrayMath.getDiffOfMaxMinFrom(numArray); +// System.out.println(diff); + +// Lab 2 +// int[] numArray = {-1,-20,1,5,6,7,9999,-3000}; +// +// ArrayMath.getTwoSmallestNumbersFrom(numArray); + +// Lab 3 +// Employee employee = new Employee( +// "Konstantinos", +// "Leivaditis", +// "example@gmail.com", +// "696114321", +// 28000, +// "Junior S&OP Analyst"); +// +// System.out.println(employee.getInfo()); + +// Lab 4 +// Intern intern = new Intern( +// "Konstantinos", +// "Leivaditis", +// "example@gmail.com", +// "696114321", +// 28000, +// "Junior S&OP Analyst"); +// intern.setSalary(2000000); +// System.out.println(intern.getInfo()); + +// Lab 5 +// +// 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 From 24aef9c7b59c63c2dad169babc3307f522c625a4 Mon Sep 17 00:00:00 2001 From: Konstantinos Leivaditis Date: Sat, 30 Aug 2025 21:13:39 +0200 Subject: [PATCH 2/2] Lab 1 make outputs more readable --- .../src/main/java/org/example/ArrayMath.java | 3 +- .../src/main/java/org/example/Employee.java | 1 + .../src/main/java/org/example/Main.java | 182 ++++++++++-------- 3 files changed, 106 insertions(+), 80 deletions(-) diff --git a/lab-java-basics/src/main/java/org/example/ArrayMath.java b/lab-java-basics/src/main/java/org/example/ArrayMath.java index 7ad96a0..f13becf 100644 --- a/lab-java-basics/src/main/java/org/example/ArrayMath.java +++ b/lab-java-basics/src/main/java/org/example/ArrayMath.java @@ -7,10 +7,10 @@ public class ArrayMath { // 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]; - System.out.println(args.length); if (args.length > 1) { for (int arg : args) { @@ -28,6 +28,7 @@ public static int getDiffOfMaxMinFrom(int[] args) { } } + // Prints in console the min 2 elements of an array public static void getTwoSmallestNumbersFrom(int[] args) { int min = args[0]; int min2 = args[0]; diff --git a/lab-java-basics/src/main/java/org/example/Employee.java b/lab-java-basics/src/main/java/org/example/Employee.java index c2a0e12..abe19ba 100644 --- a/lab-java-basics/src/main/java/org/example/Employee.java +++ b/lab-java-basics/src/main/java/org/example/Employee.java @@ -43,6 +43,7 @@ public void setSalary(double salary) { public void setPosition(String position) { this.position = position; } + // Getters public String getFirstName() { return firstName; diff --git a/lab-java-basics/src/main/java/org/example/Main.java b/lab-java-basics/src/main/java/org/example/Main.java index e25b281..a36757e 100644 --- a/lab-java-basics/src/main/java/org/example/Main.java +++ b/lab-java-basics/src/main/java/org/example/Main.java @@ -1,90 +1,114 @@ package org.example; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; -//TIP To Run code, press or -// click the icon in the gutter. + public class Main { public static void main(String[] args) { -// Lab 1 -// int[] numArray = {-1,-20,1,5,6,7,9999,-3000}; -// -// int diff = ArrayMath.getDiffOfMaxMinFrom(numArray); -// System.out.println(diff); - -// Lab 2 -// int[] numArray = {-1,-20,1,5,6,7,9999,-3000}; -// -// ArrayMath.getTwoSmallestNumbersFrom(numArray); - -// Lab 3 -// Employee employee = new Employee( -// "Konstantinos", -// "Leivaditis", -// "example@gmail.com", -// "696114321", -// 28000, -// "Junior S&OP Analyst"); -// -// System.out.println(employee.getInfo()); - -// Lab 4 -// Intern intern = new Intern( -// "Konstantinos", -// "Leivaditis", -// "example@gmail.com", -// "696114321", -// 28000, -// "Junior S&OP Analyst"); -// intern.setSalary(2000000); -// System.out.println(intern.getInfo()); - -// Lab 5 -// -// 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++; -// } +// 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