From 65927838866666edf675c3467bffb2f1cbd847fc Mon Sep 17 00:00:00 2001 From: Ljubi Date: Fri, 5 Sep 2025 16:38:36 +0200 Subject: [PATCH] solution of questions --- .idea/.gitignore | 3 ++ .idea/lab-java-basics.iml | 9 +++++ .idea/misc.xml | 6 ++++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ src/Intern.java | 12 +++++++ src/Main.java | 46 ++++++++++++++++++++++++ src/Question1/Difference.java | 28 +++++++++++++++ src/Question2/Smallestelement.java | 24 +++++++++++++ src/Question3/Employee.java | 58 ++++++++++++++++++++++++++++++ src/Question3/Intern.java | 12 +++++++ src/Question3/Main.java | 45 +++++++++++++++++++++++ 12 files changed, 257 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/lab-java-basics.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 src/Intern.java create mode 100644 src/Main.java create mode 100644 src/Question1/Difference.java create mode 100644 src/Question2/Smallestelement.java create mode 100644 src/Question3/Employee.java create mode 100644 src/Question3/Intern.java create mode 100644 src/Question3/Main.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/lab-java-basics.iml b/.idea/lab-java-basics.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/lab-java-basics.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a9182a4 --- /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/src/Intern.java b/src/Intern.java new file mode 100644 index 0000000..c23a065 --- /dev/null +++ b/src/Intern.java @@ -0,0 +1,12 @@ +import Question3.Employee; + +public class Intern extends Employee { + public static final int MAX_SALARY = 20000; + + public Intern(String fullName, String position, int salary) { + super(fullName, position, salary); + if (salary > MAX_SALARY) { + throw new IllegalArgumentException("Max salary for intern is " + MAX_SALARY); + } + } + } diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..c09d1d4 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,46 @@ +import Question3.Employee; + +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + Employee emp1 = new Employee("Mark", "Manager", 3000); + + emp1.displayInfo(); + emp1.giveRaise(10); + System.out.println("-----"); + System.out.println(emp1 + " After raise: "); + emp1.displayInfo(); + emp1.promote("Senior Manager"); + emp1.setSalary(5000); + System.out.println("After promote: "); + emp1.displayInfo(); + System.out.println("-----"); + + System.out.println("Interns: "); + Intern int2 = new Intern(" David", "Intern", 2000); + int2.displayInfo(); + + Intern int3 = new Intern(" Tom", " Intern" , 2000); + int3.displayInfo(); + System.out.println("-----"); + + System.out.println("Company hired next interns:"); + Intern[] interns = new Intern[10]; + interns[0] = new Intern("Liam" ,"Intern" , 2000); + interns[1] =new Intern("Pam" ,"Intern" , 2000); + interns[2] = new Intern("Nina" ,"Intern" , 2000); + interns[3] = new Intern("Alex" ,"Intern" , 2000); + interns[4] = new Intern("Luka" ,"Intern" , 2000); + interns[5] = new Intern("Manuel" ,"Intern" , 2000); + interns[6] = new Intern("Lara", "Intern" , 2000); + interns[7] = new Intern("Tony", "Intern" , 2000); + interns[8] = new Intern("Sara", "Intern" , 2000); + interns[9] = new Intern("Daniel", "Intern", 2000); + for (Employee compintern : interns){ + compintern.displayInfo(); + } + } + + +} \ No newline at end of file diff --git a/src/Question1/Difference.java b/src/Question1/Difference.java new file mode 100644 index 0000000..9ac2146 --- /dev/null +++ b/src/Question1/Difference.java @@ -0,0 +1,28 @@ +package Question1; + +import java.util.ArrayList; +import java.util.Arrays; + +public class Difference { + public static void main(String[] args) { + + int[] numbers = {5 , 11, 23}; + int max = numbers[0]; + int min = numbers[0]; + + for (int num : numbers) { + if (num > max) { + max = num; + } + if (num < min) { + min = num; + } + } + System.out.println("Max number is "+ max); + System.out.println("Min number is " + min); + + int subtraction = max - min; + System.out.println("Difference is " + subtraction); + + } +} \ No newline at end of file diff --git a/src/Question2/Smallestelement.java b/src/Question2/Smallestelement.java new file mode 100644 index 0000000..59b6226 --- /dev/null +++ b/src/Question2/Smallestelement.java @@ -0,0 +1,24 @@ +package Question2; + +public class Smallestelement { + public static void main(String[] args) { + int[] numbers = {66, 27, 38, 11, 50}; + { + int smallnum = Integer. MAX_VALUE; + int secondsmallnum = Integer.MAX_VALUE; + + for (int num : numbers) { + if (num < smallnum) { + secondsmallnum=smallnum; + smallnum = num; + } + if (secondsmallnum > num && num> smallnum) { + secondsmallnum = num; + } + } + System.out.println("Smallest number is " + smallnum); + System.out.println("Second smallest number is " + secondsmallnum); + + } + } +} \ No newline at end of file diff --git a/src/Question3/Employee.java b/src/Question3/Employee.java new file mode 100644 index 0000000..60f9748 --- /dev/null +++ b/src/Question3/Employee.java @@ -0,0 +1,58 @@ +package Question3; + +import javax.xml.namespace.QName; + +public class Employee { + private String fullName = "Mark"; + private String position = "Manager"; + private int salary = 3000; + + public Employee(String fullName, String position, int salary) { + this.fullName = fullName; + this.position = position; + this.salary = salary; + } + public String toString(){ + return "Full name: " + fullName + + " Position:" + position + + " Salary:" + salary; + } + + public String getName() { + return fullName; + } + + public String getPosition() { + return position; + } + + public void setPosition(String position) { + this.position = position; + } + + public int getSalary() { + return salary; + } + + public void setSalary(int salary) { + if (salary > 0) { + this.salary = salary; + } else { + System.out.println("Salary can not be negative"); + } + } + public void displayInfo () { + System.out.println("Full name: " + fullName); + System.out.println("Position: " + position); + System.out.println("Salary: " + salary); + } + public void giveRaise (int percent){ + if (percent>0){ + salary += salary * percent / 100; + } + } + public void promote (String newPosition){ + this.position = newPosition; + System.out.println(fullName +" has been promoted to "+ newPosition); + } +} \ No newline at end of file diff --git a/src/Question3/Intern.java b/src/Question3/Intern.java new file mode 100644 index 0000000..e2a6491 --- /dev/null +++ b/src/Question3/Intern.java @@ -0,0 +1,12 @@ +package Question3; + +public class Intern extends Employee { + public static final int MAX_SALARY = 20000; + + public Intern(String fullName, String position, int salary) { + super(fullName, position, salary); + if (salary > MAX_SALARY) { + throw new IllegalArgumentException("Max salary for intern is " + MAX_SALARY); + } + } + } diff --git a/src/Question3/Main.java b/src/Question3/Main.java new file mode 100644 index 0000000..5dc8ebf --- /dev/null +++ b/src/Question3/Main.java @@ -0,0 +1,45 @@ +package Question3; + +public class Main { + public static void main(String[] args) { + Employee emp1 = new Employee("Mark Bright", " Manager", 3000); + System.out.println("1. Employee: "); + emp1.displayInfo(); + emp1.giveRaise(10); + System.out.println("-----"); + System.out.println("Mark Bright got raise."); + System.out.println("After raise: "); + emp1.displayInfo(); + emp1.promote("Senior Manager"); + emp1.setSalary(5000); + System.out.println("After promote: "); + emp1.displayInfo(); + System.out.println("-----"); + + System.out.println("Interns: "); + Intern int2 = new Intern(" David Motes", "Intern", 2000); + int2.displayInfo(); + + Intern int3 = new Intern(" Tom Pacy", " Intern" , 2000); + int3.displayInfo(); + System.out.println("-----"); + + System.out.println("Company hired next interns:"); + Intern[] interns = new Intern[10]; + interns[0] = new Intern("Liam Miller" ,"Intern" , 2000); + interns[1] =new Intern("Pam Paulson" ,"Intern" , 2000); + interns[2] = new Intern("Nina Dominguez" ,"Intern" , 2000); + interns[3] = new Intern("Alex Koreny" ,"Intern" , 2000); + interns[4] = new Intern("Luka Littler" ,"Intern" , 2000); + interns[5] = new Intern("Manuel Garcia" ,"Intern" , 2000); + interns[6] = new Intern("Lara Butler", "Intern" , 2000); + interns[7] = new Intern("Tony Park", "Intern" , 2000); + interns[8] = new Intern("Sara Hawknins", "Intern" , 2000); + interns[9] = new Intern("Daniel Clark", "Intern", 2000); + for (Employee compintern : interns){ + compintern.displayInfo(); + } + } + + +} \ No newline at end of file