From d7a880d8c15b37fd04a2a5daa478579fa34a635b Mon Sep 17 00:00:00 2001 From: MisCor <61877780+MisCor@users.noreply.github.com> Date: Sat, 6 Sep 2025 02:26:41 +0200 Subject: [PATCH] Lab 1 Java Basics Cori --- Lab 1 Java Basics Cori/.gitignore | 29 +++++ Lab 1 Java Basics Cori/.idea/.gitignore | 8 ++ Lab 1 Java Basics Cori/.idea/misc.xml | 6 + Lab 1 Java Basics Cori/.idea/modules.xml | 8 ++ Lab 1 Java Basics Cori/.idea/vcs.xml | 6 + .../Lab 1 Java Basics Cori.iml | 11 ++ Lab 1 Java Basics Cori/src/Employee.java | 104 ++++++++++++++++++ Lab 1 Java Basics Cori/src/Intern.java | 24 ++++ Lab 1 Java Basics Cori/src/Main.java | 95 ++++++++++++++++ 9 files changed, 291 insertions(+) create mode 100644 Lab 1 Java Basics Cori/.gitignore create mode 100644 Lab 1 Java Basics Cori/.idea/.gitignore create mode 100644 Lab 1 Java Basics Cori/.idea/misc.xml create mode 100644 Lab 1 Java Basics Cori/.idea/modules.xml create mode 100644 Lab 1 Java Basics Cori/.idea/vcs.xml create mode 100644 Lab 1 Java Basics Cori/Lab 1 Java Basics Cori.iml create mode 100644 Lab 1 Java Basics Cori/src/Employee.java create mode 100644 Lab 1 Java Basics Cori/src/Intern.java create mode 100644 Lab 1 Java Basics Cori/src/Main.java diff --git a/Lab 1 Java Basics Cori/.gitignore b/Lab 1 Java Basics Cori/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/Lab 1 Java Basics Cori/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/Lab 1 Java Basics Cori/.idea/.gitignore b/Lab 1 Java Basics Cori/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/Lab 1 Java Basics Cori/.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 1 Java Basics Cori/.idea/misc.xml b/Lab 1 Java Basics Cori/.idea/misc.xml new file mode 100644 index 0000000..a9182a4 --- /dev/null +++ b/Lab 1 Java Basics Cori/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Lab 1 Java Basics Cori/.idea/modules.xml b/Lab 1 Java Basics Cori/.idea/modules.xml new file mode 100644 index 0000000..28590a5 --- /dev/null +++ b/Lab 1 Java Basics Cori/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Lab 1 Java Basics Cori/.idea/vcs.xml b/Lab 1 Java Basics Cori/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Lab 1 Java Basics Cori/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Lab 1 Java Basics Cori/Lab 1 Java Basics Cori.iml b/Lab 1 Java Basics Cori/Lab 1 Java Basics Cori.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Lab 1 Java Basics Cori/Lab 1 Java Basics Cori.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Lab 1 Java Basics Cori/src/Employee.java b/Lab 1 Java Basics Cori/src/Employee.java new file mode 100644 index 0000000..ce8659b --- /dev/null +++ b/Lab 1 Java Basics Cori/src/Employee.java @@ -0,0 +1,104 @@ + +// Exercise 3 +// 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. + + +class Employee { + private String name; + private String surname; + private int age; + private String namePosition; + private int levelInCompany; + private int salary; + private boolean activeWorker; + + public Employee(String name, String surname, int age, String namePosition, int levelInCompany, int salary, boolean activeWorker) { + setName(name); + setSurname(surname); + setAge(age); + setNamePosition(namePosition); + setSalary(salary); + setLevelInCompany(levelInCompany); + setActiveWorker(activeWorker); + } + + public Employee(String name, String surname){ + setName(name); + setSurname(surname); + } + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getNamePosition() { + return namePosition; + } + + public void setNamePosition(String namePosition) { + this.namePosition = namePosition; + } + + public int getLevelInCompany() { + return levelInCompany; + } + + public void setLevelInCompany(int levelInCompany) { + this.levelInCompany = levelInCompany; + } + + public int getSalary() { + return salary; + } + + public void setSalary(int salary) { + this.salary = salary; + } + + public boolean isActiveWorker() { + return activeWorker; + } + + public void setActiveWorker(boolean activeWorker) { + this.activeWorker = activeWorker; + } + + public String getEmployeeDetails(){ + return this.name + " " + this.surname + " is " + this.age + " years old. Works as " + this.namePosition + " level " + this.levelInCompany + " with a salary of " + this.salary + " and it's working status is " + this.activeWorker + "."; + } + + public String getEmployeeFullName(){ + return "The employee full name is " + this.name + " " + this.surname; + } + + public void offEmployee(){ + this.activeWorker = false; + } +} + + + diff --git a/Lab 1 Java Basics Cori/src/Intern.java b/Lab 1 Java Basics Cori/src/Intern.java new file mode 100644 index 0000000..63dd9bf --- /dev/null +++ b/Lab 1 Java Basics Cori/src/Intern.java @@ -0,0 +1,24 @@ +// Exercise 4 +// 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. + +class Intern extends Employee{ + + int maxSalary = 20000; + + public Intern(String name, String surname) { + super(name, surname); + } + + + public void setSalary(int salary){ + if (salary > maxSalary){ + super.setSalary(maxSalary); + System.out.println("New Salary set to: " + maxSalary); + } else { + super.setSalary(salary); + } + } +} \ No newline at end of file diff --git a/Lab 1 Java Basics Cori/src/Main.java b/Lab 1 Java Basics Cori/src/Main.java new file mode 100644 index 0000000..151379c --- /dev/null +++ b/Lab 1 Java Basics Cori/src/Main.java @@ -0,0 +1,95 @@ +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + + + // Exercise 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. + + + int[] differenceLS = {10, 30, 5, 7, 6, 80, 900, 175, 250, 20}; + + int maxNum = differenceLS[0]; + int minNum = differenceLS[0]; + + for(int i = 0; i < differenceLS.length; i++){ + if (maxNum < differenceLS[i]){ + maxNum = differenceLS[i]; + }else if(minNum > differenceLS[i] ){ + minNum = differenceLS[i]; + } + } + + int difference = maxNum - minNum; + + System.out.println("The difference between the largest and smallest values is max " + difference + "."); + + // Exercise 2 + // Write a method in Java to find the smallest and the second smallest elements + // of a given array and print it in the console. + // Use loops and conditionals to develop the algorithm. + + int[] smallestAndSecondSmallest = {510, 200, 400, 500, 350, 150, 680, 251, 925}; + + int smallestNum = smallestAndSecondSmallest[0]; + int secondSmallestNum = smallestAndSecondSmallest[0]; + + for(int i = 0; i < smallestAndSecondSmallest.length; i++){ + if (smallestNum > smallestAndSecondSmallest[i]){ + secondSmallestNum = smallestNum; + smallestNum = smallestAndSecondSmallest[i]; + } else if(secondSmallestNum > smallestAndSecondSmallest[i] && smallestNum != smallestAndSecondSmallest[i]) { + secondSmallestNum = smallestAndSecondSmallest[i]; + } + } + + System.out.println("The smallest number is " + smallestNum + " and the second smallest is " + secondSmallestNum + "."); + + + + + // Exercise 3 in Employee.java + // Exercise 4 in Intern.java + + //Exercise 5 + // Write a program that creates 10 Employees and print it al the properties. + + Employee employee1 = new Employee("Marcus", "Kornelius", 29, "Data Analyst", 3, 28000, true); + Employee employee2 = new Employee("Maria", "BonDia", 32, "Accountant", 4, 36000, true); + Employee employee3 = new Employee("Joana", "Triana", 33, "Software Developer", 5, 45000, true); + Employee employee4 = new Employee("Clara", "Aldana", 25, "Project Manager", 4, 36000, true); + Employee employee5 = new Employee("Laia", "Mana", 40, "HR", 3, 28000, true); + Employee employee6 = new Employee("Kostas", "Lipon", 42, "Customer Support", 2, 24000, true); + Employee employee7 = new Employee("Carme", "Ganga", 22, "Business Analyst", 3, 28000, true); + Employee employee8 = new Employee("Ona", "Vento", 35, "Marketing Specialist", 2, 24000, true); + Employee employee9 = new Employee("Niko", "Panallet", 52, "Operations Manager", 4, 36000, true); + Employee employee10 = new Employee("Petro", "Metrio", 38, "Sales Representative", 2, 24000, true); + + + System.out.println(employee1.getEmployeeDetails()); + System.out.println(employee2.getEmployeeDetails()); + System.out.println(employee3.getEmployeeDetails()); + System.out.println(employee4.getEmployeeDetails()); + System.out.println(employee5.getEmployeeDetails()); + System.out.println(employee6.getEmployeeDetails()); + System.out.println(employee7.getEmployeeDetails()); + System.out.println(employee8.getEmployeeDetails()); + System.out.println(employee9.getEmployeeDetails()); + System.out.println(employee10.getEmployeeDetails()); + + + // Extra + + // Intern intern1 = new Intern("Blanca", "Valeria"); + + // intern1.setSalary(30000); + // employee1.offEmployee(); + // System.out.println(employee1.getEmployeeDetails()); + + + } +} \ No newline at end of file