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
38 changes: 38 additions & 0 deletions lab-java-basics/.gitignore
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions lab-java-basics/.idea/.gitignore

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

7 changes: 7 additions & 0 deletions lab-java-basics/.idea/encodings.xml

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

14 changes: 14 additions & 0 deletions lab-java-basics/.idea/misc.xml

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

6 changes: 6 additions & 0 deletions lab-java-basics/.idea/vcs.xml

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

17 changes: 17 additions & 0 deletions lab-java-basics/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>lab-java-basics</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>24</maven.compiler.source>
<maven.compiler.target>24</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
48 changes: 48 additions & 0 deletions lab-java-basics/src/main/java/org/example/ArrayMath.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
71 changes: 71 additions & 0 deletions lab-java-basics/src/main/java/org/example/Employee.java
Original file line number Diff line number Diff line change
@@ -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()) ;
}
}
21 changes: 21 additions & 0 deletions lab-java-basics/src/main/java/org/example/Intern.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
114 changes: 114 additions & 0 deletions lab-java-basics/src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -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<Employee> 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++;
}

}
}