personList = new ArrayList<>();
16 | for (int i = 1; i <= n; i++) {
17 | String data = scanner.nextLine();
18 | if (Integer.parseInt(data.split(" ")[1]) > 30) {
19 |
20 | Person person = new Person(data.split(" ")[0], Integer.parseInt(data.split(" ")[1]));
21 | personList.add(person);
22 | }
23 | }
24 | for (Person person : personList){
25 | System.out.println(person.getName() + " - " + person.getAge());
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/BasicSyntaxConditionalStatmentsAndLoops/Exercise/Orders.java:
--------------------------------------------------------------------------------
1 | package BasicSyntaxConditionalStatmentsAndLoops.Exercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Orders {
6 |
7 | private static double sum;
8 | public static void main(String[] args) {
9 | Scanner scanner = new Scanner(System.in);
10 |
11 |
12 | int amountOfOrders = Integer.parseInt(scanner.nextLine());
13 |
14 | while (amountOfOrders > 0) {
15 | double price = Double.parseDouble(scanner.nextLine());
16 | int days = Integer.parseInt(scanner.nextLine());
17 | int amountOFCapsules = Integer.parseInt(scanner.nextLine());
18 |
19 | double cal = (amountOFCapsules * price) * days;
20 | sum += cal;
21 | System.out.printf("The price for the coffee is: $%.2f%n", cal);
22 | amountOfOrders--;
23 | }
24 | System.out.printf("Total: $%.2f%n", sum);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/DataTypeAndVariables/Exercises/TriplesOfLatinLetters.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.DataTypeAndVariables.Exercises.MoreExercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class TriplesOfLatinLetters {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | int number = Integer.parseInt(scanner.nextLine());
11 |
12 | threeCalc(number);
13 | }
14 |
15 | public static void threeCalc(int num){
16 | for (int i = 0; i < num; i++) {
17 | for (int j = 0; j < num; j++) {
18 | for (int k = 0; k < num; k++) {
19 | char firstChar = (char) ('a' + i);
20 | char secondChar = (char) ('a' + j);
21 | char thirdChar = (char) ('a' + k);
22 | System.out.printf("%c%c%c%n", firstChar, secondChar, thirdChar);
23 | }
24 | }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Methods/Exercises/AddAndSubtract.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods.Exercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class AddAndSubtract {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | int firstNum = Integer.parseInt(scanner.nextLine());
10 | int secondNum = Integer.parseInt(scanner.nextLine());
11 | int thirdNum = Integer.parseInt(scanner.nextLine());
12 |
13 | int addedSum = sumNum(firstNum, secondNum);
14 | int subtractionResult = subtractNum(addedSum, thirdNum);
15 |
16 | System.out.println(subtractionResult);
17 |
18 | }
19 |
20 | public static int sumNum(int first, int second){
21 |
22 | int result = first + second;
23 |
24 | return result;
25 | }
26 |
27 | public static int subtractNum(int summedNum, int subtractionNum){
28 |
29 | return summedNum - subtractionNum;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/TextProcessing/Exercise/More Exercise/HTML.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.TextProcessing.Exercise.MoreExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class HTML {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 |
11 | String title = scanner.nextLine();
12 | System.out.println("");
13 | System.out.println(" " + title);
14 | System.out.println("
");
15 |
16 | String article = scanner.nextLine();
17 |
18 | System.out.println("");
19 | System.out.println(" " + article);
20 | System.out.println("");
21 |
22 | String comment;
23 |
24 | while (!"end of comments".equals(comment = scanner.nextLine())) {
25 | System.out.println("");
26 | System.out.println(" " + comment);
27 | System.out.println("
");
28 |
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/ObjectAndClasses/Exercise/orderByAge/Main.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.OOP.Exercise.orderByAge;
2 |
3 | import java.util.*;
4 |
5 | public class Main {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | String command = scanner.nextLine();
10 | List orderList = new ArrayList<>();
11 | while (!command.equals("End")) {
12 | String[] currentCommand = command.split(" ");
13 | String name = currentCommand[0];
14 | String id = currentCommand[1];
15 | int age = Integer.parseInt(currentCommand[2]);
16 | Order order = new Order(name, id, age);
17 | orderList.add(order);
18 |
19 | command = scanner.nextLine();
20 | }
21 | orderList.sort(Comparator.comparing(Order::getAge));
22 | for (Order current : orderList){
23 | System.out.println(current);
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/ObjectAndClasses/Exercise/students/Main.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.OOP.Exercise.students;
2 |
3 | import java.util.*;
4 |
5 | public class Main {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | int n = Integer.parseInt(scanner.nextLine());
11 | List studentList = new ArrayList<>();
12 | for (int i = 1; i <= n; i++) {
13 | String currentStudent = scanner.nextLine();
14 | Student student = new Student(
15 | currentStudent.split(" ")[0],
16 | currentStudent.split(" ")[1],
17 | Double.parseDouble(currentStudent.split(" ")[2]));
18 |
19 | studentList.add(student);
20 | }
21 |
22 | studentList.sort(Comparator.comparing(Student::getGrade).reversed());
23 | for (Student student : studentList){
24 | System.out.println(student);
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/DataTypeAndVariables/Exercises/PokeMon2.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.DataTypeAndVariables.Exercises.MoreExercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class PokeMon2 {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | int pokePower = Integer.parseInt(scanner.nextLine());
10 | int distance = Integer.parseInt(scanner.nextLine());
11 | int exhaustionFactor = Integer.parseInt(scanner.nextLine());
12 |
13 | double initialPokePower = pokePower;
14 |
15 | int pokedTargets = 0;
16 |
17 | while (pokePower >= distance) {
18 | pokedTargets++;
19 | pokePower -= distance;
20 |
21 | if (pokePower == initialPokePower * 0.5 && exhaustionFactor > 0) {
22 | pokePower /= exhaustionFactor;
23 | }
24 | }
25 | System.out.printf("%d%n", pokePower);
26 | System.out.println(pokedTargets);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Methods/Exercises/SmallestOfThreeNumbers.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods.Exercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class SmallestOfThreeNumbers {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | int first = Integer.parseInt(scanner.nextLine());
10 | int second = Integer.parseInt(scanner.nextLine());
11 | int third = Integer.parseInt(scanner.nextLine());
12 |
13 | int result = smallest(first, second, third);
14 | System.out.println(result);
15 | }
16 |
17 | public static int smallest(int first, int second, int third){
18 | int smallest = Integer.MAX_VALUE;
19 | if (first < smallest){
20 | smallest = first;
21 | }
22 | if (second < smallest){
23 | smallest = second;
24 | }
25 | if (third < smallest){
26 | smallest = third;
27 | }
28 | return smallest;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Methods/Exercises/VowelsCount.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods.Exercises;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class VowelsCount {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 | String input = scanner.nextLine();
11 |
12 | int result = calcVowels(input);
13 | System.out.println(result);
14 | }
15 |
16 | public static int calcVowels(String text){
17 | char[] vowels = new char[] {'A','a','O','o','U','u','E','e','I','i','Q','q'};
18 | int count = 0;
19 | for (int i = 0; i < vowels.length; i++) {
20 | for (int j = 0; j < text.length(); j++) {
21 | char vowelsChar = vowels[i];
22 | char textChar = text.charAt(j);
23 | if (vowelsChar == textChar){
24 | count++;
25 | }
26 |
27 | }
28 | }
29 | return count;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/ObjectAndClasses/Exercise/MoreExercise/TeamworkProject/Team.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.OOP.Exercise.MoreExercise.TeamworkProject;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class Team {
7 | String name;
8 | String creator;
9 | List members;
10 |
11 | public Team() {
12 | setMembers(new ArrayList<>());
13 | }
14 |
15 | public List getMembers() {
16 | return members;
17 | }
18 |
19 | public String getName() {
20 | return name;
21 | }
22 |
23 | public String getCreator() {
24 | return creator;
25 | }
26 |
27 | public void setName(String value) {
28 | name = value;
29 | }
30 |
31 | public void setCreator(String value) {
32 | creator = value;
33 | }
34 |
35 | public void setMembers(List value) {
36 | members = value;
37 | }
38 |
39 | public int numberOfMembers() {
40 | return members.size();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/Methods/Orders.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Orders {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | String type = scanner.nextLine();
10 | int num = Integer.parseInt(scanner.nextLine());
11 |
12 | System.out.printf("%.2f", calcNum(type, num));
13 |
14 |
15 | }
16 |
17 | public static Double calcNum(String type,int number){
18 |
19 | double price = 0;
20 | switch (type){
21 | case "coffee":
22 | price = 1.50;
23 | break;
24 | case "water":
25 | price = 1.00;
26 | break;
27 | case "coke":
28 | price = 1.40;
29 | break;
30 | case "snacks":
31 | price = 2.00;
32 | break;
33 | }
34 | price = number * price;
35 | return price;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/TextProcessing/Exercise/ReplaceRepeatingChars.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.TextProcessing.Exercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class ReplaceRepeatingChars {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | String input = scanner.nextLine();
11 | StringBuilder sb = new StringBuilder();
12 |
13 | for (int i = 0; i < input.length(); i++) {
14 | if (i == input.length() - 1 && input.charAt(i) == input.charAt(i - 1)) {
15 | sb.append(input.charAt(i));
16 | break;
17 | } else if (i == input.length() - 1 && input.charAt(i) != input.charAt(i - 1)) {
18 | sb.append(input.charAt(i));
19 | } else {
20 | if (input.charAt(i) != input.charAt(i+1)) {
21 | sb.append(input.charAt(i));
22 | }
23 | }
24 | }
25 |
26 | System.out.println(sb);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/AssociativeArrays/WordsSynonyms.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.AssociativeArrays;
2 |
3 | import java.util.*;
4 |
5 | public class WordsSynonyms {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | int rotation = Integer.parseInt(scanner.nextLine());
10 |
11 | LinkedHashMap> wordsMap = new LinkedHashMap<>();
12 | List synonyms = new ArrayList<>();
13 | for (int i = 1; i <= rotation; i++) {
14 | String word = scanner.nextLine();
15 | String synonym = scanner.nextLine();
16 |
17 | if (!wordsMap.containsKey(word)){
18 | wordsMap.put(word, new ArrayList<>());
19 | }
20 | wordsMap.get(word).add(synonym);
21 | }
22 |
23 | for (Map.Entry> entry : wordsMap.entrySet()) {
24 |
25 | System.out.println(entry.getKey() + " - " + String.join(", ", entry.getValue()));
26 | }
27 |
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/ObjectAndClasses/Exercise/groomingSalon/Pet.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.OOP.Exercise.groomingSalon;
2 |
3 | public class Pet {
4 |
5 | private String name;
6 | private int age;
7 | private String owner;
8 |
9 |
10 |
11 | public Pet(String name, int age, String owner){
12 | this.name = name;
13 | this.age = age;
14 | this.owner = owner;
15 | }
16 |
17 | public String getName() {
18 | return this.name;
19 | }
20 |
21 | public void setName(String name) {
22 | this.name = name;
23 | }
24 |
25 | public int getAge() {
26 | return this.age;
27 | }
28 |
29 | public void setAge(int age) {
30 | this.age = age;
31 | }
32 |
33 | public String getOwner() {
34 | return this.owner;
35 | }
36 |
37 | public void setOwner(String owner) {
38 | this.owner = owner;
39 | }
40 |
41 | @Override
42 | public String toString(){
43 | return String.format("%s %d - (%s)", this.name, this.age, this.owner);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/DataTypeAndVariables/Exercises/MoreExercise/FloatingEquality.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.DataTypeAndVariables.Exercises.MoreExercises.MoreMore;
2 |
3 | import java.util.Scanner;
4 |
5 | public class FloatingEquality {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | double first = Double.parseDouble(scanner.nextLine());
10 | double second = Double.parseDouble(scanner.nextLine());
11 | double difference = 0;
12 | if (first < 0 && second < 0){
13 | difference = Math.abs(first - second);
14 | } else if (first < 0){
15 | difference = first + second;
16 | } else if (second < 0){
17 | difference = second + first;
18 | } else {
19 | difference = Math.abs(first - second);
20 | }
21 | double eps = 0.0000001;
22 |
23 | if (difference < eps){
24 | System.out.println("True");
25 | } else {
26 | System.out.println("False");
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Methods/PrintingTriangles.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods;
2 |
3 | import java.util.Scanner;
4 |
5 | public class PrintingTriangles {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | int number = Integer.parseInt(scanner.nextLine());
10 |
11 | printTriangle(number);
12 |
13 | }
14 |
15 | public static void printTriangle(int num){
16 |
17 | for (int i = 1; i <= num; i++) {
18 | increment(1, i);
19 | }
20 | for (int i = num -1; i >= 1; i--) {
21 | decrement(i);
22 | }
23 | }
24 |
25 | public static void increment(int start, int end){
26 |
27 | for (int i = start; i <= end; i++) {
28 | System.out.print(i + " ");
29 | }
30 | System.out.println();
31 | }
32 |
33 | public static void decrement(int end){
34 |
35 | for (int i = 1; i <= end; i++) {
36 | System.out.print(i + " ");
37 | }
38 | System.out.println();
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/TextProcessing/DigitsLettersOthers.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.TextProcessing;
2 |
3 | import java.util.Scanner;
4 |
5 | public class DigitsLettersOthers {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | StringBuilder digits = new StringBuilder();
11 | StringBuilder letters = new StringBuilder();
12 | StringBuilder others = new StringBuilder();
13 |
14 | String inputData = scanner.nextLine();
15 |
16 | for (int i = 0; i < inputData.length(); i++) {
17 | if (Character.isDigit(inputData.charAt(i))) {
18 | digits.append(inputData.charAt(i));
19 | } else if (Character.isLetter(inputData.charAt(i))) {
20 | letters.append(inputData.charAt(i));
21 | } else {
22 | others.append(inputData.charAt(i));
23 | }
24 | }
25 |
26 | System.out.println(digits);
27 | System.out.println(letters);
28 | System.out.println(others);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/DataTypeAndVariables/RefactorSpecialNumbers.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.DataTypeAndVariables.Exercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class RefactorSpecialNumbers {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | int num = Integer.parseInt(scanner.nextLine());
11 |
12 | int lastNum = 0;
13 |
14 | int firstNum = 0;
15 |
16 | boolean special = false;
17 |
18 | for (int i = 1; i <= num; i++) {
19 |
20 | firstNum = i;
21 |
22 | while (i > 0) {
23 | lastNum += i % 10;
24 | i = i / 10;
25 | }
26 | special = (lastNum == 5) || (lastNum == 7) || (lastNum == 11);
27 | if (special) {
28 | System.out.printf("%d -> True\n", firstNum);
29 | } else {
30 | System.out.printf("%d -> False%n", firstNum);
31 | }
32 | lastNum = 0;
33 | i = firstNum;
34 | }
35 | }
36 | }
37 |
38 |
--------------------------------------------------------------------------------
/List/ListOfProducts.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.List;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collections;
5 | import java.util.List;
6 | import java.util.Scanner;
7 | import java.util.stream.Collectors;
8 |
9 | public class ListOfProducts {
10 | public static void main(String[] args) {
11 | Scanner scanner = new Scanner(System.in);
12 |
13 | int products = Integer.parseInt(scanner.nextLine());
14 |
15 | List productsList = new ArrayList<>();
16 | for (int i = 1; i <= products; i++) {
17 | String currentProduct = scanner.nextLine();
18 | productsList.add(currentProduct);
19 | }
20 | List sortedList = productsList.stream().sorted().collect(Collectors.toList());
21 |
22 | int article = 0;
23 | for (String product : sortedList){
24 | article++;
25 | System.out.printf("%d.%s%n", article, product);
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Arrays/CondenseArrayToNumber.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Arrays;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class CondenseArrayToNumber {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 |
11 | int[] nums = Arrays
12 | .stream(scanner.nextLine().split(" "))
13 | .mapToInt(Integer::parseInt)
14 | .toArray();
15 |
16 | int[] condensed = new int[nums.length - 1];
17 |
18 | for (int i = 0; i < nums.length; i++) {
19 | if (nums.length == 1){
20 | break;
21 | }
22 |
23 | if (i == nums.length - 1){
24 | int[] condensedNew = new int[condensed.length - 1];
25 | i = -1;
26 | nums = condensed;
27 | condensed = condensedNew;
28 | } else {
29 | condensed[i] = nums[i] + nums[i + 1];
30 | }
31 | }
32 | System.out.println(nums[0]);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/DataTypeAndVariables/Exercises/PokeMon.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.DataTypeAndVariables.Exercises.MoreExercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class PokeMon {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | int pokePower = Integer.parseInt(scanner.nextLine());
10 | double originalValue = pokePower;
11 | int distanceBetweenTargets = Integer.parseInt(scanner.nextLine());
12 | int exhaustionFactor = Integer.parseInt(scanner.nextLine());
13 | int pokes = 0;
14 | double percent = ((originalValue / 2) / originalValue) * 100;
15 |
16 | while (pokePower >= distanceBetweenTargets) {
17 | pokePower -= distanceBetweenTargets;
18 | pokes++;
19 |
20 | if (percent == (pokePower / originalValue) * 100 && exhaustionFactor != 0) {
21 | pokePower /= exhaustionFactor;
22 | }
23 | }
24 | System.out.println(pokePower);
25 | System.out.println(pokes);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Arrays/Exercise/TopIntegers.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Arrays.Exercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class TopIntegers {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 |
11 | int[] number = Arrays
12 | .stream(scanner.nextLine().split(" "))
13 | .mapToInt(Integer::parseInt)
14 | .toArray();
15 |
16 | int max = Integer.MIN_VALUE;
17 |
18 | for (int i = 0; i < number.length; i++) {
19 | int currentNumber = number[i];
20 | for (int j = i + 1; j < number.length; j++) {
21 |
22 | if (max < number[j]){
23 | max = number[j];
24 | }
25 | }
26 | if (i == number.length - 1){
27 | System.out.print(currentNumber);
28 | } else if (currentNumber > max){
29 | System.out.print(currentNumber + " ");
30 | }
31 | max = 0;
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Methods/Exercises/MoreExercise/TribonacciSequence.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods.Exercises.MoreExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class TribunacciSequenceWeb {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | int count = Integer.parseInt(scanner.nextLine());
10 | int[] numbersToPrint = numbersToPrint(count);
11 | for (int i = 0; i < count; i++) {
12 | System.out.print(numbersToPrint[i] + " ");
13 | }
14 |
15 | }
16 |
17 | private static int[] numbersToPrint(int count) {
18 | int[] numbersArray = new int[count];
19 | for (int i = 0; i < count; i++) {
20 | if (i == 0 || i == 1) {
21 | numbersArray[i] = 1;
22 | } else if (i == 2) {
23 | numbersArray[i] = 2;
24 | } else if (i > 1) {
25 | numbersArray[i] = numbersArray[i - 1] + numbersArray[i - 2] + numbersArray[i - 3];
26 | }
27 | }
28 | return numbersArray;
29 | }
30 | }
31 |
32 |
--------------------------------------------------------------------------------
/RegularExpression/Exercise/MoreExercise/RageQuit.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Regex.Exercise.MoreExercise;
2 |
3 | import java.util.Scanner;
4 | import java.util.regex.Matcher;
5 | import java.util.regex.Pattern;
6 |
7 | public class RageQuit {
8 | public static void main(String[] args) {
9 | Scanner scanner = new Scanner(System.in);
10 |
11 |
12 |
13 | String input = scanner.nextLine().trim();
14 | String regex = "(?[\\D]+)(?[\\d]+)";
15 | Pattern pattern = Pattern.compile(regex);
16 | Matcher matcher = pattern.matcher(input);
17 |
18 | StringBuilder sb = new StringBuilder();
19 | while (matcher.find()) {
20 | String letters = matcher.group("letters").toUpperCase();
21 | int n = Integer.parseInt(matcher.group("numbers"));
22 | for (int i = 0; i < n; i++) {
23 | sb.append(letters);
24 | }
25 | }
26 | System.out.printf("Unique symbols used: %d\n", sb.chars().distinct().count());
27 | System.out.println(sb.toString());
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/BasicSyntaxConditionalStatmentsAndLoops/Exercise/Division.java:
--------------------------------------------------------------------------------
1 | package BasicSyntaxConditionalStatmentsAndLoops.Exercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Division {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | int inputNumber = Integer.parseInt(scanner.nextLine());
11 | int maxDivision = Integer.MIN_VALUE;
12 |
13 | if (inputNumber % 2 == 0) {
14 | maxDivision = 2;
15 | }
16 | if (inputNumber % 3 == 0) {
17 | maxDivision = 3;
18 | }
19 | if (inputNumber % 6 == 0) {
20 | maxDivision = 6;
21 | }
22 | if (inputNumber % 7 == 0) {
23 | maxDivision = 7;
24 | }
25 | if (inputNumber % 10 == 0) {
26 | maxDivision = 10;
27 | }
28 | if (maxDivision == Integer.MIN_VALUE) {
29 | System.out.println("Not divisible");
30 | } else {
31 | System.out.println("The number is divisible by " + maxDivision);
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Methods/Exercises/CharactersInRange.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods.Exercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class CharactersInRange {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | String first = scanner.nextLine();
10 | String second = scanner.nextLine();
11 | char firstChar = first.charAt(0);
12 | char secondChar = second.charAt(0);
13 |
14 | charSequence(firstChar, secondChar);
15 | }
16 |
17 | public static void charSequence(char first, char second){
18 |
19 | while (first != second){
20 | if (second < first){
21 | second++;
22 | if (second == first){
23 | break;
24 | }
25 | System.out.print(second + " ");
26 | } else {
27 | first++;
28 | if (first == second) {
29 | break;
30 | }
31 | System.out.print(first + " ");
32 | }
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/BasicSyntaxConditionalStatmentsAndLoops/Exercise/Login.java:
--------------------------------------------------------------------------------
1 | package BasicSyntaxConditionalStatmentsAndLoops.Exercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Login {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | String userName = scanner.nextLine();
11 | String password = "";
12 | for (int i = userName.length() - 1; i >= 0; i--) {
13 | password += userName.charAt(i);
14 | }
15 | String inputPassword = scanner.nextLine();
16 | int count = 0;
17 | while (!inputPassword.equals(password)) {
18 | count++;
19 | if (count == 4) {
20 | System.out.printf("User %s blocked!%n", userName);
21 | break;
22 | }
23 | System.out.printf("Incorrect password. Try again.%n");
24 | inputPassword = scanner.nextLine();
25 | }
26 |
27 | if (inputPassword.equals(password)) {
28 | System.out.printf("User %s logged in.", userName);
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/AssociativeArrays/Exercise/MinerTask.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.AssociativeArrays.Exercise;
2 |
3 | import java.util.LinkedHashMap;
4 | import java.util.Scanner;
5 |
6 | public class MinerTask {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 |
11 | LinkedHashMap resourceList = new LinkedHashMap<>();
12 | String command = scanner.nextLine();
13 | while (!command.equals("stop")){
14 |
15 | String resource = command;
16 | String quantity = scanner.nextLine();
17 | if (resourceList.containsKey(resource)){
18 | int addedQuantity = Integer.parseInt(quantity);
19 | resourceList.put(resource, resourceList.get(resource) + addedQuantity);
20 | } else {
21 | resourceList.put(resource, Integer.parseInt(quantity));
22 | }
23 | command = scanner.nextLine();
24 | }
25 |
26 | resourceList.entrySet().forEach(item -> System.out.printf("%s -> %d%n", item.getKey(), item.getValue()));
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/BasicSyntaxConditionalStatmentsAndLoops/Exercise/RageExpenses.java:
--------------------------------------------------------------------------------
1 | package BasicSyntaxConditionalStatmentsAndLoops.Exercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class RageExpenses {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | int lostGameCount = Integer.parseInt(scanner.nextLine());
11 | double priceHeadset = Double.parseDouble(scanner.nextLine());
12 | double mousePrice = Double.parseDouble(scanner.nextLine());
13 | double keyboardPrice = Double.parseDouble(scanner.nextLine());
14 | double displayPrice = Double.parseDouble(scanner.nextLine());
15 |
16 | double calcHeadset = (lostGameCount / 2) * priceHeadset;
17 | double calcMouse = (lostGameCount / 3) * mousePrice;
18 | double calcKeyboard = (lostGameCount / 6) * keyboardPrice;
19 | double calcDisplay = ((lostGameCount /6) / 2) * displayPrice;
20 | double totalSum = calcHeadset + calcMouse + calcKeyboard + calcDisplay;
21 | System.out.printf("Rage expenses: %.2f lv.%n", totalSum);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/ObjectAndClasses/Exercise/articles/Article.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.OOP.Exercise.articles;
2 |
3 | public class Article {
4 |
5 | private String title;
6 | private String content;
7 | private String author;
8 |
9 |
10 | public Article(String title, String content, String author) {
11 | this.title = title;
12 | this.content = content;
13 | this.author = author;
14 | }
15 |
16 | public void edit(String newContent){
17 | this.content = newContent;
18 | }
19 |
20 | public void changeAuthor(String newAuthor){
21 | this.author = newAuthor;
22 | }
23 |
24 | public void rename(String newTitle){
25 | this.title = newTitle;
26 | }
27 |
28 | public String getTitle() {
29 | return this.title;
30 | }
31 |
32 | public String getContent() {
33 | return this.content;
34 | }
35 |
36 | public String getAuthor() {
37 | return this.author;
38 | }
39 |
40 | @Override
41 | public String toString() {
42 | return this.title + " - " + this.content + ": " + this.author;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/ExamPreparation/Numbers.java:
--------------------------------------------------------------------------------
1 | package ExamPreparation;
2 |
3 | import java.util.*;
4 | import java.util.stream.Collectors;
5 |
6 | public class Numbers {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 | List numberArr = Arrays.stream(scanner.nextLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList());
11 |
12 | int sum = 0;
13 | for (int i = 0; i < numberArr.size(); i++) {
14 | sum += numberArr.get(i);
15 | }
16 | double average = 1.0 * sum / numberArr.size();
17 | numberArr.sort(Collections.reverseOrder());
18 |
19 | int count = 0;
20 | for (int i = 0; i < numberArr.size(); i++) {
21 | if (numberArr.get(i) > average) {
22 | count++;
23 | System.out.printf("%d ", numberArr.get(i));
24 | } else if (i == numberArr.size() -1 && count == 0) {
25 | System.out.println("No");
26 | }
27 | if (count == 5) {
28 | break;
29 | }
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/ExamPreparation/BlackFlag.java:
--------------------------------------------------------------------------------
1 | package ExamPreparation;
2 |
3 | import java.util.Scanner;
4 |
5 | public class BlackFlag {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | int plunderDays = Integer.parseInt(scanner.nextLine());
10 | int dailyPlunder = Integer.parseInt(scanner.nextLine());
11 | double expectedPlunder = Double.parseDouble(scanner.nextLine());
12 | double sumPlunder = 0;
13 |
14 | for (int i = 1; i <= plunderDays; i++) {
15 | if (i % 3 == 0) {
16 | sumPlunder += dailyPlunder * 1.50;
17 | } else {
18 | sumPlunder += dailyPlunder;
19 | }
20 |
21 | if (i % 5 == 0) {
22 | sumPlunder -= sumPlunder * 0.30;
23 | }
24 | }
25 | if (expectedPlunder <= sumPlunder) {
26 | System.out.printf("Ahoy! %.2f plunder gained.%n", sumPlunder);
27 | } else {
28 | System.out.printf("Collected only %.2f%% of the plunder.", sumPlunder / expectedPlunder * 100);
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/ObjectAndClasses/Exercise/articles/Main.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.OOP.Exercise.articles;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Main {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | String articleData = scanner.nextLine();
10 | String[] articleArr = articleData.split(", ");
11 | Article article = new Article(articleArr[0], articleArr[1], articleArr[2]);
12 |
13 |
14 | int rotation = Integer.parseInt(scanner.nextLine());
15 | for (int i = 1; i <= rotation; i++) {
16 | String command = scanner.nextLine();
17 | if (command.contains("Rename")) {
18 | article.rename(command.split(": ")[1]);
19 | } else if (command.contains("Edit")) {
20 | article.edit(command.split(": ")[1]);
21 | } else if (command.contains("ChangeAuthor")) {
22 | article.changeAuthor(command.split(": ")[1]);
23 | }
24 | }
25 | System.out.printf("%s - %s: %s", article.getTitle(), article.getContent(), article.getAuthor());
26 | }
27 | }
--------------------------------------------------------------------------------
/Arrays/Exercise/ArrayRotation.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Arrays.Exercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class ArrayRotation {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | String[] originalArr = scanner.nextLine().split(" ");
11 | int rotations = Integer.parseInt(scanner.nextLine());
12 | String[] secondArr = new String[originalArr.length];
13 |
14 | for (int k = 0; k < rotations; k++) {
15 |
16 | for (int i = 0; i < secondArr.length; i++) {
17 |
18 | if (i == 0){
19 | secondArr[0] = originalArr[1];
20 | } else if (i == secondArr.length - 1){
21 | secondArr[i] = originalArr[0];
22 | } else {
23 | secondArr[i] = originalArr[i + 1];
24 | }
25 | }
26 | originalArr = secondArr;
27 | secondArr = new String[originalArr.length];
28 | }
29 |
30 | for (String s : originalArr) {
31 | System.out.print(s + " ");
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/DataTypeAndVariables/Exercises/MoreExercise/DataTypeFinder.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.DataTypeAndVariables.Exercises.MoreExercises.MoreMore;
2 |
3 | import java.util.Scanner;
4 |
5 | public class DataTypeFinder {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | String input = scanner.nextLine();
10 |
11 | while (!input.equals("END")){
12 | Scanner type = new Scanner(input);
13 | if (type.hasNextBoolean()){
14 | System.out.println(input + " is boolean type");
15 | } else if (type.hasNextInt()){
16 | System.out.println(input + " is integer type");
17 | } else if (type.hasNextDouble()){
18 | System.out.println(input + " is floating point type");
19 | } else if (input.length() == 1){
20 | System.out.println(input + " is character type");
21 | } else if (type.hasNextLine()) {
22 | System.out.println(input + " is string type");
23 | }
24 |
25 | input = scanner.nextLine();
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/DataTypeAndVariables/Exercises/MoreExercise/FromLeftToTheRight_02.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.DataTypeAndVariables.Exercises.Exercises.MoreExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class FromLeftToTheRight_02 {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | int inputLines = Integer.parseInt(scanner.nextLine());
10 |
11 | for (int i = 0; i < inputLines; i++) {
12 | String[] inputArray = scanner.nextLine().split(" ");
13 | long firstNumber = Long.parseLong(inputArray[0]);
14 | long secondNumber = Long.parseLong(inputArray[1]);
15 |
16 | long currentNumber = Math.abs(Math.max(firstNumber, secondNumber));
17 | long sumOfDigits = 0;
18 | long currentDigit;
19 |
20 | while (currentNumber > 0) {
21 | currentDigit = (currentNumber % 10);
22 | sumOfDigits = sumOfDigits + currentDigit;
23 |
24 | currentNumber = currentNumber / 10;
25 | }
26 | System.out.println(sumOfDigits);
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/AssociativeArrays/OddOccurrences.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.AssociativeArrays;
2 |
3 | import java.util.ArrayList;
4 | import java.util.LinkedHashMap;
5 | import java.util.Map;
6 | import java.util.Scanner;
7 |
8 | public class OddOccurrences {
9 | public static void main(String[] args) {
10 | Scanner scanner = new Scanner(System.in);
11 |
12 |
13 | String[] inputArr = scanner.nextLine().split(" ");
14 | LinkedHashMap wordsMap = new LinkedHashMap<>();
15 |
16 | for (String word : inputArr){
17 |
18 | word = word.toLowerCase();
19 | if (!wordsMap.containsKey(word)){
20 | wordsMap.put(word, 1);
21 | } else {
22 | wordsMap.put(word, wordsMap.get(word) + 1);
23 | }
24 | }
25 |
26 | ArrayList keyList = new ArrayList<>();
27 | for (Map.Entry entry : wordsMap.entrySet()) {
28 | if (!(entry.getValue() % 2 == 0)){
29 | keyList.add(entry.getKey());
30 | }
31 | }
32 | System.out.println(String.join(", ", keyList));
33 |
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/BasicSyntaxConditionalStatmentsAndLoops/Exercise/StrongNumber.java:
--------------------------------------------------------------------------------
1 | package BasicSyntaxConditionalStatmentsAndLoops.Exercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class StrongNumber {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | String inputNumber = scanner.nextLine();
11 | int number = Integer.parseInt(inputNumber);
12 | int factorialSum = 0;
13 | int totalSum = 0;
14 | for (int i = 0; i < inputNumber.length(); i++) {
15 | int current = Integer.parseInt(String.valueOf(inputNumber.charAt(i)));
16 | factorialSum = 0;
17 | if (current == 0) {
18 | totalSum += 1;
19 | continue;
20 | }
21 | for (int j = current; j >= 1; j--) {
22 | if (j == current) {
23 | factorialSum = j;
24 | } else {
25 | factorialSum *= j;
26 | }
27 | }
28 | totalSum += factorialSum;
29 | }
30 | System.out.println((number == totalSum) ? "yes" : "no");
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/DataTypeAndVariables/Exercises/SpiceMustFlow.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.DataTypeAndVariables.Exercises.MoreExercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class SpiceMustFlow {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | int yield = Integer.parseInt(scanner.nextLine());
11 | int remainingSpice = yield;
12 | int workedDays = 0;
13 | int workersDailyEat = 26;
14 |
15 |
16 | while (yield >= 100){
17 |
18 | workedDays++;
19 | if (workedDays > 1) {
20 | remainingSpice += yield - workersDailyEat;
21 | } else {
22 | remainingSpice -= workersDailyEat;
23 | }
24 | if (remainingSpice < 0){
25 | remainingSpice = 0;
26 | }
27 |
28 |
29 | yield -= 10;
30 | }
31 | if (workedDays == 0){
32 | remainingSpice = 0;
33 | } else {
34 | remainingSpice -= 26;
35 | }
36 |
37 | System.out.println(workedDays);
38 | System.out.println(remainingSpice);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/Methods/Exercises/MoreExercise/CenterPoint.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods.Exercises.MoreExercise;
2 |
3 | import java.text.DecimalFormat;
4 | import java.util.Scanner;
5 |
6 | public class CenterPoint {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 | double x1 = Double.parseDouble(scanner.nextLine());
11 | double y1 = Double.parseDouble(scanner.nextLine());
12 | double x2 = Double.parseDouble(scanner.nextLine());
13 | double y2 = Double.parseDouble(scanner.nextLine());
14 | CenterPoint(x1, y1, x2, y2);
15 | }
16 |
17 | static void CenterPoint(double x1, double y1, double x2, double y2){
18 |
19 | DecimalFormat df = new DecimalFormat("0.####");
20 | double firstPointDistance = Math.sqrt((x1 * x1) + (y1 * y1));
21 | double secondPointDistance = Math.sqrt((x2 * x2) + (y2 * y2));
22 | if (firstPointDistance > secondPointDistance){
23 | System.out.printf("(%s, %s)", df.format(x2), df.format(y2));
24 | } else {
25 | System.out.printf("(%s, %s)", df.format(x1), df.format(y1));
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/Exams/MidExamRetake02/CounterStrike.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class CounterStrike {
4 | public static void main(String[] args) {
5 | Scanner scanner = new Scanner(System.in);
6 |
7 | int energy = Integer.parseInt(scanner.nextLine());
8 |
9 | String command = scanner.nextLine();
10 | int counter = 0;
11 | boolean endOfEnergy = false;
12 | while (!"End of battle".equals(command)) {
13 |
14 | int distance = Integer.parseInt(command);
15 |
16 | if (energy - distance < 0) {
17 | endOfEnergy = true;
18 | break;
19 | }
20 | energy -= distance;
21 | counter++;
22 | if (counter % 3 == 0) {
23 | energy += counter;
24 | }
25 |
26 |
27 | command = scanner.nextLine();
28 | }
29 |
30 | if (endOfEnergy) {
31 | System.out.printf("Not enough energy! Game ends with %d won battles and %d energy", counter, energy);
32 | } else {
33 | System.out.printf("Won battles: %d. Energy left: %d%n", counter, energy);
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/TextProcessing/Exercise/ValidUsername.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.TextProcessing.Exercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.Scanner;
6 | import java.util.regex.Matcher;
7 | import java.util.regex.Pattern;
8 | import java.util.stream.Collectors;
9 |
10 | public class ValidUsername {
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 |
14 |
15 | List inputData = Arrays.stream(scanner.nextLine().split(", ")).collect(Collectors.toList());
16 |
17 |
18 | for (String item : inputData) {
19 | if (isValid(item)) {
20 | System.out.println(item);
21 | }
22 | }
23 |
24 | }
25 |
26 | private static boolean isValid(String item) {
27 | boolean length = item.length() >= 3 && item.length() <= 16;
28 | boolean checker = true;
29 | for (char current : item.toCharArray()) {
30 | if (!Character.isLetterOrDigit(current) && current != '-' && current != '_') {
31 | checker = false;
32 | }
33 | }
34 | return length && checker;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Arrays/Exercise/MaxSequence.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Arrays.Exercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class MaxSequence {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 |
11 | int[] numbers = Arrays
12 | .stream(scanner.nextLine().split(" "))
13 | .mapToInt(Integer::parseInt)
14 | .toArray();
15 |
16 | int length = 1;
17 | int bestLength = 0;
18 | int endIndex = -1;
19 |
20 | for (int i = 0; i < numbers.length - 1; i++) {
21 | int currentNum = numbers[i];
22 | int secondNum = numbers[i + 1];
23 |
24 | if (currentNum == secondNum) {
25 | length++;
26 | if (length > bestLength) {
27 | bestLength = length;
28 | endIndex = i + 1;
29 | }
30 | } else {
31 | length = 1;
32 | }
33 | }
34 | for (int i = endIndex; i > endIndex - bestLength ; i--) {
35 | System.out.print(numbers[i] + " ");
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Arrays/Exercise/EqualSums2.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Arrays.Exercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class EqualSums2 {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 |
11 | int[] numsArr = Arrays
12 | .stream(scanner.nextLine().split(" "))
13 | .mapToInt(Integer::parseInt)
14 | .toArray();
15 | boolean isEqual = false;
16 |
17 | for (int i = 0; i < numsArr.length; i++) {
18 |
19 | int currentElement = numsArr[i];
20 | int leftSum = 0;
21 | int rightSum = 0;
22 |
23 | for (int j = 0; j < i; j++) {
24 | leftSum += numsArr[j];
25 | }
26 | for (int j = numsArr.length - 1; j > i; j--) {
27 | rightSum += numsArr[j];
28 | }
29 |
30 | if (leftSum == rightSum){
31 | System.out.println(i);
32 | isEqual = true;
33 | break;
34 | }
35 | }
36 | if (!isEqual){
37 | System.out.println("no");
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/List/SumAdjacentEqualNumbers.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.List;
2 |
3 | import java.text.DecimalFormat;
4 | import java.util.ArrayList;
5 | import java.util.Arrays;
6 | import java.util.List;
7 | import java.util.Scanner;
8 | import java.util.stream.Collectors;
9 |
10 | public class SumAdjacentEqualNumbers {
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 |
14 | List numList = Arrays.stream(scanner.nextLine().split(" "))
15 | .map(Double::parseDouble).collect(Collectors.toList());
16 |
17 | for (int i = 0; i < numList.size() - 1; i++) {
18 | double currentNum = numList.get(i);
19 | double nextNum = numList.get(i + 1);
20 | if (currentNum == nextNum){
21 | double sum = currentNum + nextNum;
22 | numList.set(i, currentNum + nextNum);
23 | numList.remove(i+1);
24 | i = -1;
25 | }
26 | }
27 | DecimalFormat df = new DecimalFormat("0.####");
28 | for (int i = 0; i < numList.size(); i++) {
29 | System.out.print(df.format(numList.get(i)) + " ");
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/ExamPreparation2/CounterStrike.java:
--------------------------------------------------------------------------------
1 | package ExamPreparation2;
2 |
3 | import java.util.Scanner;
4 |
5 | public class CounterStrike {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | int energy = Integer.parseInt(scanner.nextLine());
10 |
11 | String command = scanner.nextLine();
12 | int counter = 0;
13 | boolean endOfEnergy = false;
14 | while (!"End of battle".equals(command)) {
15 |
16 | int distance = Integer.parseInt(command);
17 |
18 | if (energy - distance < 0) {
19 | endOfEnergy = true;
20 | break;
21 | }
22 | energy -= distance;
23 | counter++;
24 | if (counter % 3 == 0) {
25 | energy += counter;
26 | }
27 |
28 |
29 | command = scanner.nextLine();
30 | }
31 |
32 | if (endOfEnergy) {
33 | System.out.printf("Not enough energy! Game ends with %d won battles and %d energy", counter, energy);
34 | } else {
35 | System.out.printf("Won battles: %d. Energy left: %d%n", counter, energy);
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Exams/FinalExamRetake/AdAstra.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Exams.FinalExamRetake;
2 |
3 | import java.util.Scanner;
4 | import java.util.regex.Matcher;
5 | import java.util.regex.Pattern;
6 |
7 | public class AdAstra {
8 | public static void main(String[] args) {
9 | Scanner scanner = new Scanner(System.in);
10 |
11 |
12 | String inputData = scanner.nextLine();
13 |
14 | String regex = "(?[\\|\\#]{1})(?- [A-z\\s+]+)\\1(?[0-9]{2}[\\/]{1}[0-9]{2}[\\/]{1}[0-9]{2})\\1(?\\d{1,5})\\1";
15 | Pattern pattern = Pattern.compile(regex);
16 | Matcher matcher = pattern.matcher(inputData);
17 | Matcher matcher2 = pattern.matcher(inputData);
18 |
19 | int days = 0;
20 | while (matcher.find()) {
21 | int current = Integer.parseInt(matcher.group("calories"));
22 | days += current;
23 | }
24 | System.out.printf("You have food to last you for: %d days!%n", days / 2000);
25 |
26 | while (matcher2.find()) {
27 | System.out.printf("Item: %s, Best before: %s, Nutrition: %s%n", matcher2.group("item"), matcher2.group("date"), matcher2.group("calories"));
28 | }
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/ExamPreparation23.03.2023/AdAstra.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Exams.FinalExamRetake;
2 |
3 | import java.util.Scanner;
4 | import java.util.regex.Matcher;
5 | import java.util.regex.Pattern;
6 |
7 | public class AdAstra {
8 | public static void main(String[] args) {
9 | Scanner scanner = new Scanner(System.in);
10 |
11 |
12 | String inputData = scanner.nextLine();
13 |
14 | String regex = "(?[\\|\\#]{1})(?
- [A-z\\s+]+)\\1(?[0-9]{2}[\\/]{1}[0-9]{2}[\\/]{1}[0-9]{2})\\1(?\\d{1,5})\\1";
15 | Pattern pattern = Pattern.compile(regex);
16 | Matcher matcher = pattern.matcher(inputData);
17 | Matcher matcher2 = pattern.matcher(inputData);
18 |
19 | int days = 0;
20 | while (matcher.find()) {
21 | int current = Integer.parseInt(matcher.group("calories"));
22 | days += current;
23 | }
24 | System.out.printf("You have food to last you for: %d days!%n", days / 2000);
25 |
26 | while (matcher2.find()) {
27 | System.out.printf("Item: %s, Best before: %s, Nutrition: %s%n", matcher2.group("item"), matcher2.group("date"), matcher2.group("calories"));
28 | }
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/ObjectAndClasses/Exercise/vehicleCatalogue/Catalogue.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.OOP.Exercise.vehicleCatalogue;
2 |
3 | public class Catalogue {
4 |
5 |
6 | private String type;
7 | private String model;
8 | private String color;
9 | private int horsePower;
10 |
11 |
12 |
13 | public Catalogue(String type, String model, String color, int horsePower){
14 |
15 | this.type = type;
16 | this.model = model;
17 | this.color = color;
18 | this.horsePower = horsePower;
19 | }
20 |
21 | public String getType() {
22 | return type;
23 | }
24 |
25 | public void setType(String type) {
26 | this.type = type;
27 | }
28 |
29 | public String getModel() {
30 | return model;
31 | }
32 |
33 | public void setModel(String model) {
34 | this.model = model;
35 | }
36 |
37 | public String getColor() {
38 | return color;
39 | }
40 |
41 | public void setColor(String color) {
42 | this.color = color;
43 | }
44 |
45 | public int getHorsePower() {
46 | return horsePower;
47 | }
48 |
49 | public void setHorsePower(int horsePower) {
50 | this.horsePower = horsePower;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/Exams/MidExam/Numbers.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Exams.MidExam;
2 |
3 | import java.util.Arrays;
4 | import java.util.Collections;
5 | import java.util.List;
6 | import java.util.Scanner;
7 | import java.util.stream.Collectors;
8 |
9 | public class Numbers {
10 | public static void main(String[] args) {
11 | Scanner scanner = new Scanner(System.in);
12 |
13 |
14 | List numberArr = Arrays.stream(scanner.nextLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList());
15 |
16 | int sum = 0;
17 | for (int i = 0; i < numberArr.size(); i++) {
18 | sum += numberArr.get(i);
19 | }
20 | double average = 1.0 * sum / numberArr.size();
21 | numberArr.sort(Collections.reverseOrder());
22 |
23 | int count = 0;
24 | for (int i = 0; i < numberArr.size(); i++) {
25 | if (numberArr.get(i) > average) {
26 | count++;
27 | System.out.printf("%d ", numberArr.get(i));
28 | } else if (i == numberArr.size() -1 && count == 0) {
29 | System.out.println("No");
30 | }
31 | if (count == 5) {
32 | break;
33 | }
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/ObjectAndClasses/Exercise/groomingSalon/Main.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.OOP.Exercise.groomingSalon;
2 |
3 | public class Main {
4 | public static void main(String[] args) {
5 | // Initialize the repository
6 | GroomingSalon salon = new GroomingSalon(20);
7 |
8 | // Initialize entity
9 | Pet dog = new Pet("Ellias", 5, "Tim");
10 |
11 | // Print Pet
12 | System.out.println(dog); // Ellias 5 - (Tim)
13 |
14 | // Add Pet
15 | salon.add(dog);
16 |
17 | // Remove Pet
18 | System.out.println(salon.remove("Ellias")); // true
19 | System.out.println(salon.remove("Pufa")); // false
20 |
21 | Pet cat = new Pet("Bella", 2, "Mia");
22 | Pet bunny = new Pet("Zak", 4, "Jon");
23 |
24 | salon.add(cat);
25 | salon.add(bunny);
26 |
27 | // Get Pet
28 | Pet pet = salon.getPet("Bella", "Mia");
29 | System.out.println(pet); // Bella 2 - (Mia)
30 |
31 | // Count
32 | System.out.println(salon.getCount()); // 2
33 | System.out.println(salon.remove("Brian"));
34 |
35 | // Get Statistics
36 | System.out.println(salon.getStatistics());
37 | // The grooming salon has the following clients:
38 | //Bella Mia
39 | //Zak Jon
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/DataTypeAndVariables/Exercises/BeerKegs.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.DataTypeAndVariables.Exercises.MoreExercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class BeerKegs {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | int input = Integer.parseInt(scanner.nextLine());
11 |
12 | String winner = "";
13 | float max = 0;
14 | for (int i = 1; i <= input; i++) {
15 | String model = scanner.nextLine();
16 | String radius = scanner.nextLine();
17 | String height = scanner.nextLine();
18 | float current = bigger(radius, height);
19 | if (current > max){
20 | max = current;
21 | winner = model;
22 | }
23 | }
24 | System.out.println(winner);
25 | }
26 |
27 | public static float bigger(String radius, String height){
28 |
29 | float convertedRadius = Float.parseFloat(radius);
30 | int convertedHeight = Integer.parseInt(height);
31 |
32 | float volume = (Float.parseFloat(String.valueOf(Math.PI))) * (Float.parseFloat(String.valueOf(Math.pow(convertedRadius, 2)))) * convertedHeight;
33 | return volume;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Methods/MathOperation.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods;
2 |
3 | import java.text.DecimalFormat;
4 | import java.util.Scanner;
5 |
6 | public class MathOperation {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 | int firstNum = Integer.parseInt(scanner.nextLine());
11 | String operator = scanner.nextLine();
12 | int secondNum = Integer.parseInt(scanner.nextLine());
13 |
14 | double result = operation(firstNum, operator, secondNum);
15 | DecimalFormat df = new DecimalFormat("0.####");
16 | System.out.println(df.format(result));
17 | }
18 |
19 | public static double operation(int first, String operator, int second){
20 | double result = 0;
21 | switch (operator){
22 | case "/":
23 | result = (1.0 * first) / second;
24 | break;
25 | case "*":
26 | result = (1.0 * first) * second;
27 | break;
28 | case "+":
29 | result = first + second;
30 | break;
31 | case "-":
32 | result = Math.abs(first-second);
33 | break;
34 | }
35 | return result;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Arrays/EqualArrays.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Arrays;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class EqualArrays {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 |
11 | int[] firstArr = Arrays
12 | .stream(scanner.nextLine().split(" "))
13 | .mapToInt(Integer::parseInt)
14 | .toArray();
15 |
16 | int[] secondArr = Arrays
17 | .stream(scanner.nextLine().split(" "))
18 | .mapToInt(Integer::parseInt)
19 | .toArray();
20 |
21 | boolean different = false;
22 | int index = 0;
23 | int sum = 0;
24 | for (int i = 0; i < firstArr.length; i++) {
25 | if (firstArr[i] != secondArr[i]){
26 | different = true;
27 | index = i;
28 | break;
29 | } else {
30 | sum += firstArr[i];
31 | }
32 | }
33 | if (different){
34 | System.out.printf("Arrays are not identical. Found difference at %d index.", index);
35 | } else {
36 | System.out.printf("Arrays are identical. Sum: %d", sum);
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/Methods/Exercises/PalindromeIntegers.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods.Exercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class PalindromeIntegers {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | String input = scanner.nextLine();
11 |
12 | while (!input.equals("END")){
13 |
14 | int number = Integer.parseInt(input);
15 | boolean palindromeChecker = palindromeResult(number);
16 | if (palindromeChecker){
17 | System.out.println(palindromeChecker);
18 | } else {
19 | System.out.println(palindromeChecker);
20 | }
21 | input = scanner.nextLine();
22 | }
23 | }
24 |
25 | public static boolean palindromeResult(int number){
26 | String unReversedNumber = String.valueOf(number);
27 | String reversedNumber = "";
28 | while (number != 0){
29 | int currentNumber = number % 10;
30 | reversedNumber += String.valueOf(currentNumber);
31 | number = number / 10;
32 | }
33 | if (unReversedNumber.equals(reversedNumber)){
34 | return true;
35 | } else {
36 | return false;
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/Methods/Exercises/MoreExercise/DataTypes.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods.Exercises.MoreExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class DataTypes {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | String input =scanner.nextLine();
11 |
12 | String value = scanner.nextLine();
13 | Value(input, value);
14 |
15 | }
16 |
17 | public static void Value(String input, String value){
18 |
19 | switch (input){
20 | case "int":
21 | int currentNumber = Integer.parseInt(value);
22 | IsInt(currentNumber);
23 | break;
24 | case "real":
25 | double currentDouble = Double.parseDouble(value);
26 | IsDouble(currentDouble);
27 | break;
28 | case "string":
29 | IsString(value);
30 | break;
31 | }
32 | }
33 | public static void IsInt(int number){
34 | System.out.println(number * 2);
35 | }
36 | public static void IsDouble(double number){
37 | System.out.printf("%.2f", number * 1.5);
38 | }
39 | public static void IsString(String text){
40 | System.out.printf("$%s$", text);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/Exams/MidExam-February2023/CookingMasterclass.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class CookingMasterclass {
4 | public static void main(String[] args) {
5 | Scanner scanner = new Scanner(System.in);
6 |
7 |
8 | double budget = Double.parseDouble(scanner.nextLine());
9 | int students = Integer.parseInt(scanner.nextLine());
10 | double priceOfFlour = Double.parseDouble(scanner.nextLine());
11 | double priceOfSingleEgg = Double.parseDouble(scanner.nextLine());
12 | double priceOfSingleApron = Double.parseDouble(scanner.nextLine());
13 |
14 | int countPackage = 0;
15 | for (int i = 1; i <= students; i++) {
16 | if (i % 5 == 0) {
17 | countPackage++;
18 | }
19 | }
20 |
21 | double calcApron = priceOfSingleApron * Math.ceil(students * 1.20);
22 | double calcEggs = students * (10 * priceOfSingleEgg);
23 | double calcFreePackage = priceOfFlour * (students - countPackage);
24 | double sum = calcApron + calcEggs + calcFreePackage;
25 |
26 | if (budget >= sum) {
27 | System.out.printf("Items purchased for %.2f$.\n", sum);
28 | } else {
29 | System.out.printf("%.2f$ more needed.\n", Math.abs(budget - sum));
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/AssociativeArrays/Exercise/Courses.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.AssociativeArrays.Exercise;
2 |
3 | import java.util.*;
4 |
5 | public class Courses {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | String command =scanner.nextLine();
11 |
12 | LinkedHashMap> coursesList = new LinkedHashMap<>();
13 | while (!command.equals("end")){
14 | String[] commandArr = command.split(" : ");
15 |
16 | String course = commandArr[0];
17 | String student = commandArr[1];
18 |
19 | if (!coursesList.containsKey(course)){
20 | coursesList.put(course, new ArrayList<>());
21 | coursesList.get(course).add(student);
22 | } else {
23 | coursesList.get(course).add(student);
24 | }
25 |
26 |
27 | command = scanner.nextLine();
28 | }
29 |
30 | for (Map.Entry> entry : coursesList.entrySet()) {
31 | System.out.printf("%s: %d%n", entry.getKey(), entry.getValue().size());
32 | for (int i = 0; i < entry.getValue().size(); i++) {
33 | System.out.printf("-- %s%n", entry.getValue().get(i));
34 | }
35 | }
36 |
37 |
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/DataTypeAndVariables/Exercises/SnowBall.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.DataTypeAndVariables.Exercises.MoreExercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class SnowBall {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | int input = Integer.parseInt(scanner.nextLine());
11 |
12 | int snowballSnow = 0;
13 | int snowballTime = 0;
14 | int snowballQuality = 0;
15 | double calc = Double.MIN_VALUE;
16 |
17 | for (int i = 1; i <= input; i++) {
18 | int currentSnowballSnow = Integer.parseInt(scanner.nextLine());
19 | int currentSnowballTime = Integer.parseInt(scanner.nextLine());
20 | int currentSnowballQuality = Integer.parseInt(scanner.nextLine());
21 |
22 | double currentCalc = Math.pow((double)(currentSnowballSnow / currentSnowballTime), (double)currentSnowballQuality);
23 | if (currentCalc > calc){
24 | snowballSnow = currentSnowballSnow;
25 | snowballTime = currentSnowballTime;
26 | snowballQuality = currentSnowballQuality;
27 | calc = currentCalc;
28 | }
29 | }
30 | System.out.printf("%d : %d = %.0f (%d)", snowballSnow, snowballTime, calc, snowballQuality);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Arrays/Exercise/ZigZagArray.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Arrays.Exercise;
2 |
3 | import java.io.PrintStream;
4 | import java.util.Scanner;
5 |
6 | public class ZigZagArray {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 |
11 | int number = Integer.parseInt(scanner.nextLine());
12 | int[] firstArr = new int[number];
13 | int[] secondArr = new int[number];
14 |
15 | for (int i = 0; i < number; i++) {
16 |
17 | String[] currentNum = scanner.nextLine().split(" ");
18 | if (i % 2 == 0) {
19 | firstArr[i] = Integer.parseInt(currentNum[0]);
20 | secondArr[i] = Integer.parseInt(currentNum[1]);
21 | } else {
22 | firstArr[i] = Integer.parseInt(currentNum[1]);
23 | secondArr[i] = Integer.parseInt(currentNum[0]);
24 | }
25 | }
26 | printArrs(firstArr, secondArr);
27 | }
28 |
29 |
30 | public static void printArrs(int[] first, int[] second){
31 |
32 | for (int i = 0; i < first.length; i++) {
33 | System.out.print(first[i] + " ");
34 | }
35 | System.out.println();
36 | for (int i = 0; i < second.length; i++) {
37 | System.out.print(second[i] + " ");
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/RegularExpression/Exercise/SoftUniBar.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Regex.Exercise;
2 |
3 | import java.util.Scanner;
4 | import java.util.regex.Matcher;
5 | import java.util.regex.Pattern;
6 |
7 | public class SoftUniBar {
8 | public static void main(String[] args) {
9 | Scanner scanner = new Scanner(System.in);
10 |
11 |
12 |
13 | String input = scanner.nextLine();
14 | String regex = "%(?[A-Z][a-z]*)%[^|'$%.]*<(?
- \\w+)>[^|'$%.]*\\|(?[0-9]+)\\|[^|'$%.]*?(?[0-9]+\\.?[0-9]*)\\$";
15 | double totalSum = 0;
16 | Pattern pattern = Pattern.compile(regex);
17 |
18 | while (!input.equals("end of shift")){
19 | Matcher matcher = pattern.matcher(input);
20 |
21 | if (matcher.find()){
22 | String name = matcher.group("name");
23 | String item = matcher.group("item");
24 | int count = Integer.parseInt(matcher.group("quantity"));
25 | double price = Double.parseDouble(matcher.group("price"));
26 | double sum = count * price;
27 | totalSum += sum;
28 | System.out.printf("%s: %s - %.2f%n", name, item, sum);
29 | }
30 |
31 | input = scanner.nextLine();
32 | }
33 | System.out.printf("Total income: %.2f%n", totalSum);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/List/Exercise/HouseParty.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.List.Exercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 | import java.util.List;
6 | import java.util.Scanner;
7 |
8 | public class HouseParty {
9 | public static void main(String[] args) {
10 | Scanner scanner = new Scanner(System.in);
11 |
12 |
13 | List namesOfGuests = new ArrayList<>();
14 | int rotations = Integer.parseInt(scanner.nextLine());
15 |
16 | for (int i = 1; i <= rotations; i++) {
17 | String[] currentCommand = scanner.nextLine().split(" ");
18 | if (currentCommand.length == 3){
19 | if (namesOfGuests.contains(currentCommand[0])){
20 | System.out.printf("%s is already in the list!%n", currentCommand[0]);
21 | } else {
22 | namesOfGuests.add(currentCommand[0]);
23 | }
24 | } else {
25 | if (!namesOfGuests.contains(currentCommand[0])){
26 | System.out.printf("%s is not in the list!%n", currentCommand[0]);
27 | } else {
28 | namesOfGuests.remove(currentCommand[0]);
29 | }
30 | }
31 |
32 | }
33 | for (String item : namesOfGuests){
34 | System.out.printf("%s%n", item);
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/List/MergingLists.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.List;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 | import java.util.List;
6 | import java.util.Scanner;
7 | import java.util.stream.Collectors;
8 |
9 | public class MergingLists {
10 | public static void main(String[] args) {
11 | Scanner scanner = new Scanner(System.in);
12 |
13 |
14 | List firstList = Arrays.stream(scanner.nextLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList());
15 | List secondList = Arrays.stream(scanner.nextLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList());
16 | List lastList = new ArrayList<>();
17 | boolean full = false;
18 | int count = -1;
19 |
20 | while (!full){
21 | count++;
22 | if (!(count < firstList.size()) && !(count < secondList.size())){
23 | full = true;
24 | } else {
25 | if (count < firstList.size()) {
26 | lastList.add(firstList.get(count));
27 | }
28 | if (count < secondList.size()) {
29 | lastList.add(secondList.get(count));
30 | }
31 | }
32 | }
33 | for (Integer element : lastList){
34 | System.out.print(element + " ");
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/ExamPreparation/TheLift.java:
--------------------------------------------------------------------------------
1 | package ExamPreparation;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class TheLift {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 |
11 | int people = Integer.parseInt(scanner.nextLine());
12 | int[] lift = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
13 | String[] outPut = new String[lift.length];
14 | boolean full = true;
15 |
16 | for (int i = 0; i < lift.length; i++) {
17 | for (int j = lift[i]; j < 4; j++) {
18 | if (people == 0) {
19 | full = false;
20 | break;
21 | } else {
22 | lift[i] += 1;
23 | people -= 1;
24 | }
25 | }
26 | outPut[i] = String.valueOf(lift[i]);
27 | }
28 | if (full && people == 0) {
29 | System.out.println(String.join(" ", outPut));
30 | } else if (!full) {
31 | System.out.println("The lift has empty spots!");
32 | System.out.println(String.join(" ", outPut));
33 | } else {
34 | System.out.printf("There isn't enough space! %d people in a queue!%n", people);
35 | System.out.println(String.join(" ", outPut));
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Exams/FinalExam/DestinationMapper.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Exams.FinalExam;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.Scanner;
6 | import java.util.regex.Matcher;
7 | import java.util.regex.Pattern;
8 |
9 | public class DestinationMapper {
10 | public static void main(String[] args) {
11 | Scanner scanner = new Scanner(System.in);
12 |
13 |
14 | String input = scanner.nextLine();
15 | String regex = "(=|/)(?[A-Z][a-zA-Z]{2,})(\\1)";
16 | Pattern pattern = Pattern.compile(regex);
17 | Matcher matcher = pattern.matcher(input);
18 | List list = new ArrayList<>();
19 | int sum = 0;
20 | while (matcher.find()) {
21 | String current = "";
22 | if (matcher.group().charAt(0) == matcher.group().charAt(matcher.group().length() - 1)) {
23 |
24 |
25 | for (int i = 0; i < matcher.group().length(); i++) {
26 | if (Character.isLetter(matcher.group().charAt(i))) {
27 | current += matcher.group().charAt(i);
28 | }
29 | }
30 | list.add(current);
31 | sum += current.length();
32 | }
33 | }
34 | System.out.println("Destinations: " + String.join(", ", list));
35 | System.out.println("Travel Points: " + sum);
36 |
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/List/Exercise/AppendArrays.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.List.Exercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.Scanner;
6 |
7 | public class AppendArrays {
8 | public static void main(String[] args) {
9 | Scanner scanner = new Scanner(System.in);
10 |
11 | String[] input = scanner.nextLine().split("\\|");
12 |
13 | List outputList = new ArrayList<>();
14 |
15 | for (int i = 0; i < input.length; i++) {
16 | String[] currentArray = input[i].split("\\s+");
17 |
18 | for (int j = currentArray.length - 1; j >= 0; j--) {
19 | if (!currentArray[j].equals("")) {
20 | outputList.add(0, currentArray[j]);
21 | }
22 | }
23 | }
24 | outputList.forEach(e -> System.out.print(e + " "));
25 | }
26 | }
27 | // List input = Arrays.stream(scanner.nextLine().split("\\|")).collect(Collectors.toList());
28 | // for (int i = 0; i < input.size(); i++) {
29 | // String currentItem = input.get(i);
30 | // if (currentItem.contains(" ")) {
31 | // currentItem = currentItem.replaceAll("\\s+", "");
32 | // }
33 | // for (int j = currentItem.length() - 1; j >= 0; j--) {
34 | // outputList.add(0, String.valueOf(currentItem.charAt(j)));
35 | // }
36 | // }
--------------------------------------------------------------------------------
/DataTypeAndVariables/Exercises/MoreExercise/BalancedBrackets.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.DataTypeAndVariables.Exercises.MoreExercises.MoreMore;
2 |
3 | import java.util.Scanner;
4 |
5 | public class BalancedBrackets {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | int rotations = Integer.parseInt(scanner.nextLine());
11 | boolean opening = false;
12 | boolean closing = false;
13 | boolean balanced = false;
14 |
15 | for (int i = 1; i <= rotations ; i++) {
16 |
17 | String input = scanner.nextLine();
18 | if (input.charAt(0) == ')'){
19 | if (!opening){
20 | balanced = true;
21 | } else {
22 | closing = true;
23 | opening = false;
24 | }
25 | }
26 | if (input.charAt(0)== '('){
27 | if (opening){
28 | balanced = true;
29 | } else {
30 | if (closing){
31 | closing = false;
32 | }
33 | opening = true;
34 | }
35 | }
36 | }
37 | if (balanced || opening){
38 | System.out.println("UNBALANCED");
39 | } else {
40 | System.out.println("BALANCED");
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/RegularExpression/Exercise/MoreExercise/WinningTicket.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Regex.Exercise.MoreExercise;
2 |
3 | import java.util.Scanner;
4 | import java.util.regex.Matcher;
5 | import java.util.regex.Pattern;
6 |
7 | public class WinningTicket {
8 | private static final String REGEX = "(?=.{20}).*?(?=(?[@#$^]))(?\\k{6,}).*(?<=.{10})\\k.*";
9 | private static final Pattern PATTERN = Pattern.compile(REGEX);
10 | private static final Pattern SEPARATOR = Pattern.compile("\\s*,\\s*");
11 |
12 | public static void main(String[] args) {
13 | Scanner scan = new Scanner(System.in);
14 | String[] tickets = SEPARATOR.split(scan.nextLine().trim());
15 |
16 | for (String ticket : tickets) {
17 | if (ticket.length() != 20) {
18 | System.out.println("invalid ticket");
19 | } else {
20 | Matcher matcher = PATTERN.matcher(ticket);
21 | if (matcher.matches()) {
22 | String match = matcher.group("match");
23 | System.out.printf("ticket \"%s\" - %d%s%s%n",
24 | ticket, match.length(), match.charAt(0),
25 | (match.length() == 10) ? " Jackpot!" : "");
26 | } else {
27 | System.out.printf("ticket \"%s\" - no match%n", ticket);
28 | }
29 | }
30 | }
31 | }
32 | }
--------------------------------------------------------------------------------
/BasicSyntaxConditionalStatmentsAndLoops/MoreExercise/EnglishNameOfTheLastDigit.java:
--------------------------------------------------------------------------------
1 | package BasicSyntaxConditionalStatmentsAndLoops.MoreExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class EnglishNameOfTheLastDigit {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | int number = Integer.parseInt(scanner.nextLine()) % 10;
11 |
12 | switch (number) {
13 | case 0:
14 | System.out.println("zero");
15 | break;
16 | case 1:
17 | System.out.println("one");
18 | break;
19 | case 2:
20 | System.out.println("two");
21 | break;
22 | case 3:
23 | System.out.println("three");
24 | break;
25 | case 4:
26 | System.out.println("four");
27 | break;
28 | case 5:
29 | System.out.println("five");
30 | break;
31 | case 6:
32 | System.out.println("six");
33 | break;
34 | case 7:
35 | System.out.println("seven");
36 | break;
37 | case 8:
38 | System.out.println("eight");
39 | break;
40 | case 9:
41 | System.out.println("nine");
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/BasicSyntaxConditionalStatmentsAndLoops/Exercise/PadawanEquipment.java:
--------------------------------------------------------------------------------
1 | package BasicSyntaxConditionalStatmentsAndLoops.Exercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class PadawanEquipment {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | double availableMoney = Double.parseDouble(scanner.nextLine());
11 | int countOfStudents = Integer.parseInt(scanner.nextLine());
12 | double priceOfLightSabers = Double.parseDouble(scanner.nextLine());
13 | double priceOfRobes = Double.parseDouble(scanner.nextLine());
14 | double priceOfBelts = Double.parseDouble(scanner.nextLine());
15 |
16 | double calcLightSabersPrice = Math.ceil(countOfStudents + (countOfStudents * 0.10));
17 |
18 | double calcLightSabers = calcLightSabersPrice * priceOfLightSabers;
19 | int countByDividing = countOfStudents / 6;
20 | double calcBelts = (countOfStudents - countByDividing) * priceOfBelts;
21 | double calcRobes = countOfStudents * priceOfRobes;
22 | double totalSum = calcBelts + calcRobes + calcLightSabers;
23 |
24 | if (totalSum <= availableMoney) {
25 | System.out.printf("The money is enough - it would cost %.2flv.", totalSum);
26 | } else {
27 | System.out.printf("George Lucas will need %.2flv more.", Math.abs(totalSum - availableMoney));
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Exams/More.Final.Exam/FancyBarcode.java:
--------------------------------------------------------------------------------
1 | package FinalExam;
2 |
3 | import java.util.Scanner;
4 | import java.util.regex.Matcher;
5 | import java.util.regex.Pattern;
6 |
7 | public class FancyBarcode {
8 | public static void main(String[] args) {
9 | Scanner scanner = new Scanner(System.in);
10 |
11 |
12 | int products = Integer.parseInt(scanner.nextLine());
13 | Pattern pattern = Pattern.compile("@#+(?[A-Z][A-Za-z0-9*]{4,}[A-Z])@#+");
14 |
15 | for (int i = 1; i <= products; i++) {
16 | String currentProduct = scanner.nextLine();
17 | Matcher matcher = pattern.matcher(currentProduct);
18 |
19 | if (matcher.find()) {
20 | currentProduct = matcher.group("product");
21 | System.out.println("Product group: " + productBarcode(currentProduct));
22 | } else {
23 | System.out.println("Invalid barcode");
24 | }
25 | }
26 | }
27 |
28 | private static String productBarcode(String currentProduct) {
29 | String barcode = "";
30 | boolean containsDigits = false;
31 | for (int i = 0; i < currentProduct.length(); i++) {
32 | if (Character.isDigit(currentProduct.charAt(i))) {
33 | barcode += currentProduct.charAt(i);
34 | containsDigits = true;
35 | }
36 | }
37 | return (containsDigits) ? barcode : "00";
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/Methods/Calculations.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Calculations {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | String function = scanner.nextLine();
10 | int first = Integer.parseInt(scanner.nextLine());
11 | int second = Integer.parseInt(scanner.nextLine());
12 |
13 | switch (function) {
14 | case "add":
15 | add(first, second);
16 | break;
17 | case "multiply":
18 | multiply(first, second);
19 | break;
20 | case "subtract":
21 | subtract(first, second);
22 | break;
23 | case "divide":
24 | divide(first, second);
25 | break;
26 | }
27 |
28 | }
29 |
30 | public static void add(int firstNumber, int secondNumber){
31 | System.out.println(Math.abs(firstNumber + secondNumber));
32 | }
33 | public static void multiply(int firstNumber, int secondNumber){
34 | System.out.println(firstNumber * secondNumber);
35 | }
36 | public static void subtract(int firstNumber, int secondNumber){
37 | System.out.println(Math.abs(firstNumber - secondNumber));
38 | }
39 | public static void divide(int firstNumber, int secondNumber){
40 | System.out.println(firstNumber / secondNumber);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/TextProcessing/Exercise/More Exercise/MorseCodeTranslator.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.TextProcessing.Exercise.MoreExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class MorseCodeTranslator {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | String[] morseCode = scanner.nextLine().split(" ");
10 |
11 | String[] alpha = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
12 | "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
13 | "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8",
14 | "9", "0", "!", ",", "?", ".", "'", " "};
15 | String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
16 | "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
17 | "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
18 | "-.--", "--..", ".----", "..---", "...--", "....-", ".....",
19 | "-....", "--...", "---..", "----.", "-----", "-.-.--", "--..--", "..--..", ".-.-.-", ".----.", "|"};
20 |
21 |
22 | for (int index = 0; index <= morseCode.length - 1; index++) {
23 | for (int index1 = 0; index1 <= morse.length - 1; index1++) {
24 |
25 | if (morseCode[index].equals(morse[index1])) {
26 | System.out.print(alpha[index1]);
27 | }
28 | }
29 | }
30 |
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/RegularExpression/Exercise/Furniture.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Regex.Exercise;
2 |
3 | import java.util.*;
4 | import java.util.regex.Matcher;
5 | import java.util.regex.Pattern;
6 |
7 | public class Furniture {
8 | public static void main(String[] args) {
9 | Scanner scanner = new Scanner(System.in);
10 |
11 |
12 | List furnitureList = new ArrayList<>();
13 | String input = scanner.nextLine();
14 | String regex = ">>(?[A-Za-z]+)<<(?[0-9]+\\.?[0-9]*)!(?[0-9]+)";
15 |
16 | double totalSum = 0;
17 |
18 |
19 | while (!input.equals("Purchase")){
20 |
21 | Pattern pattern = Pattern.compile(regex);
22 | Matcher matcher = pattern.matcher(input);
23 |
24 | if (matcher.find()){
25 | String furniture = matcher.group("furniture");
26 | double price = Double.parseDouble(matcher.group("price"));
27 | int quantity = Integer.parseInt(matcher.group("quantity"));
28 |
29 | furnitureList.add(furniture);
30 | totalSum += price * quantity;
31 | }
32 |
33 | input = scanner.nextLine();
34 | }
35 |
36 | System.out.println("Bought furniture:");
37 | for (int i = 0; i < furnitureList.size(); i++) {
38 | System.out.println(furnitureList.get(i));
39 | }
40 | System.out.printf("Total money spend: %.2f", totalSum);
41 |
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/ObjectAndClasses/Exercise/groomingSalon/GroomingSalon.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.OOP.Exercise.groomingSalon;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class GroomingSalon {
7 |
8 | private int capacity;
9 | private List data;
10 |
11 |
12 | public GroomingSalon(int capacity) {
13 | this.data = new ArrayList<>();
14 | this.capacity = capacity;
15 |
16 | }
17 |
18 | public void add(Pet pet) {
19 | if (this.data.size() < this.capacity) {
20 | data.add(pet);
21 | }
22 | }
23 |
24 | public boolean remove(String name) {
25 | return data.removeIf(item -> item.getName().equals(name));
26 | }
27 |
28 | public Pet getPet(String name, String owner) {
29 | return this.data.stream()
30 | .filter(pet -> pet.getName().equals(name) && pet.getOwner().equals(owner))
31 | .findFirst().orElse(null);
32 | }
33 |
34 | public int getCount() {
35 | return this.data.size();
36 | }
37 |
38 | public String getStatistics() {
39 | StringBuilder sb = new StringBuilder();
40 | sb.append("The grooming salon has the following clients:").append(System.lineSeparator());
41 | for (Pet pet : data) {
42 | sb.append(pet.getName());
43 | sb.append(" ");
44 | sb.append(pet.getOwner());
45 | sb.append(System.lineSeparator());
46 | }
47 | return sb.toString();
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/AssociativeArrays/CountRealNumber.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.AssociativeArrays;
2 |
3 | import java.text.DecimalFormat;
4 | import java.util.*;
5 |
6 | public class CountRealNumber {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 |
11 |
12 | double[] number = Arrays.stream(scanner.nextLine().split(" ")).mapToDouble(Double::parseDouble).toArray();
13 | TreeMap numberOfOccurrences = new TreeMap<>();
14 |
15 | for (int i = 0; i < number.length; i++) {
16 | double currentNumber = number[i];
17 | if (numberOfOccurrences.containsKey(currentNumber)){
18 | for (Map.Entry doubleIntegerEntry : numberOfOccurrences.entrySet()) {
19 | if (doubleIntegerEntry.getKey().equals(currentNumber)){
20 | int oldValue = doubleIntegerEntry.getValue();
21 | doubleIntegerEntry.setValue(oldValue+1);
22 | }
23 | }
24 | } else {
25 | numberOfOccurrences.put(currentNumber, 1);
26 | }
27 | }
28 |
29 | DecimalFormat df = new DecimalFormat("0.####");
30 | for (Map.Entry doubleIntegerEntry : numberOfOccurrences.entrySet()) {
31 | double current = doubleIntegerEntry.getKey();
32 | System.out.printf("%s -> %d%n", df.format(current), doubleIntegerEntry.getValue());
33 |
34 | }
35 |
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Methods/MultiplyEvensByOdds.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods;
2 |
3 | import java.util.Scanner;
4 |
5 | public class MultiplyEvensByOdds {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | int number = Integer.parseInt(scanner.nextLine());
10 |
11 | int even = evenSum(Math.abs(number));
12 | int odds = oddSum(Math.abs(number));
13 | int total = even * odds;
14 | System.out.println(total);
15 | }
16 |
17 | public static int evenSum (int num){
18 | String toString = String.valueOf(num);
19 | int even = 0;
20 | int odds = 0;
21 | for (int i = 0; i < toString.length(); i++) {
22 | int currentNum = Integer.parseInt(String.valueOf(toString.charAt(i)));
23 | if (currentNum % 2 == 0){
24 | even += currentNum;
25 | } else {
26 | odds += currentNum;
27 | }
28 | }
29 | return even;
30 | }
31 |
32 | public static int oddSum (int num){
33 | String toString = String.valueOf(num);
34 | int even = 0;
35 | int odds = 0;
36 | for (int i = 0; i < toString.length(); i++) {
37 | int currentNum = Integer.parseInt(String.valueOf(toString.charAt(i)));
38 | if (currentNum % 2 == 0){
39 | even += currentNum;
40 | } else {
41 | odds += currentNum;
42 | }
43 | }
44 | return odds;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/AssociativeArrays/Exercise/StudentAcademy.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.AssociativeArrays.Exercise;
2 |
3 | import java.util.*;
4 |
5 | public class StudentAcademy {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | int number = Integer.parseInt(scanner.nextLine());
11 | LinkedHashMap> studentsList = new LinkedHashMap<>();
12 |
13 | for (int i = 1; i <= number; i++) {
14 |
15 | String studentName = scanner.nextLine();
16 | Double grade = Double.parseDouble(scanner.nextLine());
17 | if (!studentsList.containsKey(studentName)) {
18 | studentsList.put(studentName, new ArrayList<>());
19 | }
20 | studentsList.get(studentName).add(grade);
21 | }
22 |
23 | LinkedHashMap average = new LinkedHashMap<>();
24 | for (Map.Entry> entry : studentsList.entrySet()) {
25 |
26 | double sum = getAverage(entry.getValue());
27 | if (sum >= 4.50){
28 | average.put(entry.getKey(), sum);
29 | }
30 | }
31 |
32 | average.forEach((key, value) -> System.out.printf("%s -> %.2f%n", key, value));
33 |
34 | }
35 |
36 | private static double getAverage(List grades) {
37 | double sum = 0;
38 | for (int i = 0; i < grades.size(); i++) {
39 | sum += grades.get(i);
40 | }
41 | return sum / grades.size();
42 | }
43 | }
--------------------------------------------------------------------------------
/RegularExpression/Exercise/MoreExercise/SantaSecretHelper.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Regex.Exercise.MoreExercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.Scanner;
6 | import java.util.regex.Matcher;
7 | import java.util.regex.Pattern;
8 |
9 | public class SantaSecretHelper {
10 | public static void main(String[] args) {
11 | Scanner scanner = new Scanner(System.in);
12 |
13 |
14 |
15 | int key = Integer.parseInt(scanner.nextLine());
16 |
17 | String command;
18 | String regex = "\\@(?[A-Za-z]+)[^\\@\\-\\!\\:\\>]+[\\!](?[G|N])[\\!]";
19 | Pattern pattern = Pattern.compile(regex);
20 | List good = new ArrayList<>();
21 | while (!"end".equals(command = scanner.nextLine())) {
22 | StringBuilder decryptedText = new StringBuilder();
23 | for (int i = 0; i < command.length(); i++) {
24 | char currentChar = command.charAt(i);
25 | currentChar -= key;
26 | decryptedText.append(currentChar);
27 | }
28 | Matcher matcher = pattern.matcher(decryptedText.toString());
29 | while (matcher.find()) {
30 | String name = matcher.group("name");
31 | String behavior = matcher.group("behaviour");
32 | if ("G".equals(behavior)) {
33 | good.add(name);
34 | }
35 | }
36 | }
37 | good.forEach(System.out::println);
38 |
39 |
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Arrays/Exercise/MoreExercise/EncryptSortAndPrint.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Arrays.Exercise.MoreExercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class EncryptSortAndPrint {
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 |
10 | int arrLength = Integer.parseInt(scanner.nextLine());
11 | String vowels = "aeiouAEIOU";
12 |
13 | String[] sequenceOfStrings = new String[arrLength];
14 | int[] valueOfString = new int[arrLength];
15 |
16 | for (int i = 0; i < sequenceOfStrings.length; i++) {
17 | sequenceOfStrings[i] = scanner.nextLine();
18 |
19 | int sumVowels = 0;
20 | int sumCons = 0;
21 | char[] charArr = sequenceOfStrings[i].toCharArray();
22 | for (char index : charArr) {
23 | if (index == 'a' || index == 'e' || index == 'i' || index == 'o' || index == 'u'
24 | || index == 'A' || index == 'E' || index == 'I' || index == 'O' || index == 'U'){
25 | sumVowels += ((int)index * sequenceOfStrings[i].length());
26 | } else {
27 | sumCons += ((int)index / sequenceOfStrings[i].length());
28 | }
29 | }
30 | int stringNum = sumVowels + sumCons;
31 | valueOfString[i] = stringNum;
32 | }
33 | Arrays.sort(valueOfString);
34 | for (int value: valueOfString) {
35 | System.out.println(value);
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/DataTypeAndVariables/Exercises/MoreExercise/FromLeftToRight.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.DataTypeAndVariables.Exercises.MoreExercises.MoreMore;
2 |
3 | import java.util.Scanner;
4 |
5 | public class FromLeftToRight {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | int num = Integer.parseInt(scanner.nextLine());
11 |
12 | for (int i = 1; i <= num; i++) {
13 |
14 | String first = scanner.nextLine();
15 |
16 | String[] firstArr = first.split(" ");
17 | System.out.println(calculate(firstArr[0], firstArr[1]));
18 | }
19 | }
20 |
21 | public static int calculate(String left, String right) {
22 | int sum = 0;
23 | long leftInt = Long.parseLong(left);
24 | long rightInt = Long.parseLong(right);
25 |
26 | if (leftInt > rightInt) {
27 | for (int i = 0; i < left.length(); i++) {
28 | if (left.charAt(i) == '-') {
29 | continue;
30 | }
31 | int currentInt = Integer.parseInt(String.valueOf(left.charAt(i)));
32 | sum += currentInt;
33 | }
34 | } else {
35 | for (int i = 0; i < right.length(); i++) {
36 | if (right.charAt(i) == '-') {
37 | continue;
38 | }
39 | int currentInt = Integer.parseInt(String.valueOf(right.charAt(i)));
40 | sum += currentInt;
41 | }
42 | }
43 | return sum;
44 |
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/TextProcessing/Exercise/CharacterMultiplier.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.TextProcessing.Exercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class CharacterMultiplier {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | String[] inputData = scanner.nextLine().split(" ");
11 |
12 | char[] first = inputData[0].toCharArray();
13 | char[] second = inputData[1].toCharArray();
14 |
15 | int sum = execute(first, second);
16 |
17 | System.out.println(sum);
18 | }
19 |
20 | private static int execute(char[] first, char[] second) {
21 | int sum = 0;
22 |
23 | if (first.length > second.length) {
24 | for (int i = 0; i < first.length; i++) {
25 | if (i <= second.length - 1) {
26 | sum += first[i] * second[i];
27 | } else {
28 | sum += first[i];
29 | }
30 | }
31 | return sum;
32 |
33 | } else if (first.length < second.length) {
34 | for (int i = 0; i < second.length; i++) {
35 | if (i <= first.length - 1) {
36 | sum += second[i] * first[i];
37 | } else {
38 | sum += second[i];
39 | }
40 | }
41 | return sum;
42 |
43 | } else {
44 | for (int i = 0; i < first.length; i++) {
45 | sum += first[i] * second[i];
46 | }
47 | return sum;
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Exams/MidExamRetake02/ShootForTheWin.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 | import java.util.Scanner;
3 |
4 | public class ShootForTheWin {
5 | public static void main(String[] args) {
6 | Scanner scanner = new Scanner(System.in);
7 |
8 |
9 | int[] targets = Arrays.stream(scanner.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
10 |
11 | String command = scanner.nextLine();
12 | int shotTargets = 0;
13 |
14 | while (!"End".equals(command)) {
15 | int index = Integer.parseInt(command);
16 | if (isInRange(index, targets) && targets[index] != -1) {
17 | shotTargets++;
18 | int currentNumber = targets[index];
19 | targets[index] = -1;
20 | for (int i = 0; i < targets.length; i++) {
21 | if (targets[i] != -1 && targets[i] <= currentNumber) {
22 | targets[i] = targets[i] + currentNumber;
23 | } else if (targets[i] != -1 && targets[i] > currentNumber) {
24 | targets[i] = targets[i] - currentNumber;
25 | }
26 | }
27 |
28 | }
29 |
30 |
31 | command = scanner.nextLine();
32 | }
33 |
34 | System.out.printf("Shot targets: %d -> ", shotTargets);
35 | for (int target : targets) {
36 | System.out.print(target + " ");
37 | }
38 | }
39 |
40 | private static boolean isInRange(int index, int[] targets) {
41 | return index >= 0 && index < targets.length;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/List/Exercise/ChangeList.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.List.Exercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.List;
5 | import java.util.Scanner;
6 | import java.util.stream.Collectors;
7 |
8 | public class ChangeList {
9 | public static void main(String[] args) {
10 | Scanner scanner = new Scanner(System.in);
11 |
12 |
13 | List numbers = Arrays
14 | .stream(scanner.nextLine().split(" "))
15 | .map(Integer::parseInt)
16 | .collect(Collectors.toList());
17 |
18 | String command = scanner.nextLine();
19 | while (!command.equals("end")){
20 | String[] currentCommand = command.split(" ");
21 |
22 | switch (currentCommand[0]){
23 | case "Delete":
24 | int currentNum = Integer.parseInt(currentCommand[1]);
25 | for (int i = 0; i < numbers.size(); i++) {
26 | if (currentNum == numbers.get(i)){
27 | numbers.remove(i);
28 | i = 0;
29 | }
30 | }
31 | break;
32 | case "Insert":
33 | int element = Integer.parseInt(currentCommand[1]);
34 | int index = Integer.parseInt(currentCommand[2]);
35 | numbers.add(index, element);
36 | }
37 |
38 | command = scanner.nextLine();
39 | }
40 |
41 | for (Integer print : numbers){
42 | System.out.print(print + " ");
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/Methods/Exercises/TopNumber.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods.Exercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class TopNumber {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | int number = Integer.parseInt(scanner.nextLine());
10 |
11 | for (int i = 1; i <= number; i++) {
12 |
13 | boolean divisible = checkDivisible(i);
14 | if (divisible){
15 | boolean oddCheck = checkOddDigit(i);
16 | if (oddCheck){
17 | System.out.println(i);
18 | }
19 | }
20 | }
21 | }
22 |
23 |
24 | public static boolean checkDivisible(int num){
25 | int sum = 0;
26 | while (num != 0){
27 | int currentNumber = num % 10;
28 | sum += currentNumber;
29 | num = num / 10;
30 | }
31 | if (sum % 8 == 0){
32 | return true;
33 | } else {
34 | return false;
35 | }
36 | }
37 |
38 | public static boolean checkOddDigit(int num){
39 | String stringNumber = String.valueOf(num);
40 |
41 | boolean find = false;
42 | for (int i = 0; i < stringNumber.length(); i++) {
43 | int currentNum = Integer.parseInt(String.valueOf(stringNumber.charAt(i)));
44 | if (currentNum % 2 != 0){
45 | find = true;
46 | break;
47 | }
48 | }
49 | if (find){
50 | return true;
51 | } else {
52 | return false;
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/ObjectAndClasses/Exercise/AdvertisementMessage.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.OOP.Exercise;
2 |
3 | import java.util.*;
4 |
5 | public class AdvertisementMessage {
6 |
7 | private static class Advertisement {
8 | String[] phrasesArr = {"Excellent product.", "Such a great product.", "I always use that product.", "Best product of its category.", "Exceptional product.", "I can’t live without this product." };
9 | String[] eventsArr = {"Now I feel good.", "I have succeeded with this product.", "Makes miracles. I am happy of the results!", "I cannot believe but now I feel awesome.", "Try it yourself, I am very satisfied.", "I feel great!" };
10 | String[] authorsArr = {"Diana", "Petya", "Stella", "Elena", "Katya", "Iva", "Annie", "Eva" };
11 | String[] citiesArr = {"Burgas", "Sofia", "Plovdiv", "Varna", "Ruse" };
12 |
13 | Random rnd = new Random();
14 |
15 | public String getAdvertisement(){
16 | String output =phrasesArr[rnd.nextInt(phrasesArr.length)] + " " + eventsArr[rnd.nextInt(eventsArr.length)] + " " + authorsArr[rnd.nextInt(authorsArr.length)]
17 | + " " + citiesArr[rnd.nextInt(citiesArr.length)];
18 |
19 | return output;
20 | }
21 | }
22 |
23 | public static void main(String[] args) {
24 | Scanner scanner = new Scanner(System.in);
25 |
26 |
27 | int printTimes = Integer.parseInt(scanner.nextLine());
28 |
29 | for (int i = 1; i <= printTimes; i++) {
30 | Advertisement advert = new Advertisement();
31 | System.out.println(advert.getAdvertisement());
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/TextProcessing/Exercise/StringExplosion.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.TextProcessing.Exercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class StringExplosion {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | String input = scanner.nextLine();
11 | StringBuilder sb = new StringBuilder();
12 | boolean bomb = false;
13 | int bomber = 0;
14 | int count = 0;
15 | for (int i = 0; i < input.length(); i++) {
16 | if (bomb && count != bomber) {
17 | count++;
18 | if (input.charAt(i) == '>') {
19 | bomber = (bomber - (count - 1)) + Integer.parseInt(String.valueOf(input.charAt(i+1)));
20 | count = 0;
21 | sb.append(input.charAt(i));
22 | continue;
23 | } else if (count == bomber) {
24 | bomb = false;
25 | bomber = 0;
26 | count = 0;
27 | continue;
28 | }
29 | continue;
30 | }
31 | if (input.charAt(i) == '>') {
32 | if (i != input.length() -1) {
33 | bomber = bomber + Integer.parseInt(String.valueOf(input.charAt(i + 1)));
34 | bomb = true;
35 | count = 0;
36 | sb.append(input.charAt(i));
37 | }
38 | } else {
39 | sb.append(input.charAt(i));
40 | }
41 | }
42 | System.out.println(sb.toString());
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/Methods/Exercises/MiddleCharacter.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.Methods.Exercises;
2 |
3 | import java.util.Scanner;
4 |
5 | public class MiddleCharacter {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | String input = scanner.nextLine();
10 |
11 | System.out.println(calcLength(input));
12 | }
13 |
14 | public static String calcLength(String text){
15 |
16 | int count = 0;
17 | String midChar = "";
18 | for (int i = 0; i < text.length(); i++) {
19 | count++;
20 | }
21 | if (count % 2 == 0){
22 | midChar = evenLength(text);
23 | } else {
24 | midChar = oddLength(text);
25 | }
26 | return midChar;
27 | }
28 |
29 | public static String evenLength(String text){
30 |
31 | int calcMiddle = text.length() - 2;
32 | int remainCharacters = calcMiddle / 2;
33 | String midChars = "";
34 |
35 | for (int i = 0; i < remainCharacters + 2; i++) {
36 | if (i > remainCharacters - 1){
37 | midChars += text.charAt(i);
38 | }
39 | }
40 | return midChars;
41 | }
42 |
43 | public static String oddLength(String text){
44 |
45 | int calcMiddle = text.length() - 1;
46 | int remainCharacter = calcMiddle / 2;
47 | String midChar = "";
48 |
49 | for (int i = 0; i < remainCharacter + 1; i++) {
50 | if (i == remainCharacter){
51 | midChar += text.charAt(i);
52 | }
53 | }
54 | return midChar;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/AssociativeArrays/Exercise/Orders.java:
--------------------------------------------------------------------------------
1 | package Fundamentals.AssociativeArrays.Exercise;
2 |
3 | import java.util.*;
4 |
5 | public class Orders {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 |
10 | String command = scanner.nextLine();
11 | LinkedHashMap quantityList = new LinkedHashMap<>();
12 | LinkedHashMap priceList = new LinkedHashMap<>();
13 |
14 | while (!command.equals("buy")){
15 | String[] commandArr = command.split(" ");
16 | String item = commandArr[0];
17 | double prize = Double.parseDouble(commandArr[1]);
18 | double quantity = Integer.parseInt(commandArr[2]);
19 |
20 | if (!quantityList.containsKey(item)){
21 | quantityList.put(item, quantity);
22 | priceList.put(item, prize);
23 | } else {
24 | double oldQuantity = quantityList.get(item);
25 | quantityList.put(item, oldQuantity + quantity);
26 | priceList.put(item, prize);
27 | }
28 |
29 |
30 | command = scanner.nextLine();
31 | }
32 |
33 | for (Map.Entry quantity : quantityList.entrySet()) {
34 | for (Map.Entry price : priceList.entrySet()) {
35 |
36 | if (quantity.getKey().equals(price.getKey())){
37 | System.out.printf("%s -> %.2f%n", price.getKey(), quantity.getValue() * price.getValue());
38 | break;
39 | }
40 | }
41 |
42 | }
43 |
44 |
45 | }
46 | }
47 |
--------------------------------------------------------------------------------