From 7b0acd777972be375ded11873524008029e19a4f Mon Sep 17 00:00:00 2001 From: ILLA4444F Date: Tue, 9 Dec 2025 22:57:17 +0200 Subject: [PATCH 1/6] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D0=B5=D0=BD=D0=BE=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20Stream=20sum=20of?= =?UTF-8?q?=20squares?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../5-0-1-lambda-functions-map/README.MD | 56 ++++++++++++++----- .../5-0-2-stream-sum-of-squares/README.MD | 25 ++++----- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/5-0-functional-programming/5-0-1-lambda-functions-map/README.MD b/5-0-functional-programming/5-0-1-lambda-functions-map/README.MD index d86ffcca3..76c7cfa72 100644 --- a/5-0-functional-programming/5-0-1-lambda-functions-map/README.MD +++ b/5-0-functional-programming/5-0-1-lambda-functions-map/README.MD @@ -1,18 +1,48 @@ -# Lambda Functions Map -Start learning functional programming in Java by writing simple math functions using Lambdas 💪 +import java.util.HashMap; +import java.util.Map; +import java.util.function.IntUnaryOperator; -### Objectives +public class LambdaFunctionsMap { -* implement **abs** (absolute) function using lambda ✅ -* implement **sgn** (signum) function using lambda ✅ -* implement **increment** function using lambda ✅ -* implement **decrement** function using lambda ✅ -* implement **square** function using lambda ✅ -* add all those functions to the function map ✅ + private final Map functionMap = new HashMap<>(); ---- + public LambdaFunctionsMap() { + + IntUnaryOperator abs = x -> x >= 0 ? x : -x; -#### 🆕 First time here? – [See Introduction](https://github.com/bobocode-projects/java-fundamentals-exercises/tree/main/0-0-intro#introduction) + + IntUnaryOperator sgn = x -> x > 0 ? 1 : (x < 0 ? -1 : 0); -## -
\ No newline at end of file + + IntUnaryOperator increment = x -> x + 1; + + + IntUnaryOperator decrement = x -> x - 1; + + + IntUnaryOperator square = x -> x * x; + + + functionMap.put("abs", abs); + functionMap.put("sgn", sgn); + functionMap.put("increment", increment); + functionMap.put("decrement", decrement); + functionMap.put("square", square); + } + + + public Map getFunctionMap() { + return functionMap; + } + + + public static void main(String[] args) { + LambdaFunctionsMap lfm = new LambdaFunctionsMap(); + + System.out.println("abs(-5) = " + lfm.getFunctionMap().get("abs").applyAsInt(-5)); + System.out.println("sgn(-5) = " + lfm.getFunctionMap().get("sgn").applyAsInt(-5)); + System.out.println("increment(7) = " + lfm.getFunctionMap().get("increment").applyAsInt(7)); + System.out.println("decrement(7) = " + lfm.getFunctionMap().get("decrement").applyAsInt(7)); + System.out.println("square(4) = " + lfm.getFunctionMap().get("square").applyAsInt(4)); + } +} diff --git a/5-0-functional-programming/5-0-2-stream-sum-of-squares/README.MD b/5-0-functional-programming/5-0-2-stream-sum-of-squares/README.MD index bed96a199..d5fac49c1 100644 --- a/5-0-functional-programming/5-0-2-stream-sum-of-squares/README.MD +++ b/5-0-functional-programming/5-0-2-stream-sum-of-squares/README.MD @@ -1,14 +1,11 @@ -# Stream sum of squares -Learn functional programming by refactoring a piece of code using Stream API 💪 - -### Objectives -* **refactor imperative-style code** with for loop using Stream API ✅ -* **create a `IntStream`** based on given the first, and the last values ✅ -* **transform each element** of the stream into its square value ✅ -* **calculate a sum** of all elements in the stream ✅ - ---- -#### 🆕 First time here? – [See Introduction](https://github.com/bobocode-projects/java-fundamentals-exercises/tree/main/0-0-intro#introduction) - -## -
\ No newline at end of file + public static int sumOfSquares(int start, int end) { + return IntStream.rangeClosed(start, end) + .map(x -> x * x) + .sum(); + } + public static void main(String[] args) { + int start = 1; + int end = 5; + int result = sumOfSquares(start, end); + System.out.println(result); + } \ No newline at end of file From daf36959bb97fad27636d9e382a2d826c3d6cb90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= Date: Wed, 10 Dec 2025 18:25:27 +0200 Subject: [PATCH 2/6] Add generics demo with various generic classes --- .../main/java/com/bobocode/basics/1-3-0.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/1-3-0.java diff --git a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/1-3-0.java b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/1-3-0.java new file mode 100644 index 000000000..1cddcd058 --- /dev/null +++ b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/1-3-0.java @@ -0,0 +1,71 @@ +package genericsdemo; + +public class Box { + private T value; + public Box(T value) { this.value = value; } + public T get() { return value; } +} + +public class Main { + public static void main(String[] args) { + Box b = new Box<>("hello"); + NumberBox nb = new NumberBox<>(42); + + Utils.print("Generic method!"); + Wildcards.printBox(b); + Wildcards.sumNumbers(nb); + + Pair p = new Pair<>("age", 30); + SelfComparable a = new MyCmp(10); + SelfComparable c = new MyCmp(20); + + System.out.println(a.compareTo(c)); + } +} + +class MyCmp extends SelfComparable { + public MyCmp(int data) { + super(data); + } +} + +public class NumberBox { + private T num; + public NumberBox(T num) { this.num = num; } + public double doubleValue() { return num.doubleValue(); } +} + +public class Pair { + private K key; + private V value; + public Pair(K key, V value) { + this.key = key; + this.value = value; + } +} + +public class SelfComparable> implements Comparable { + private int data; + public SelfComparable(int data) { this.data = data; } + + @Override + public int compareTo(T other) { + return Integer.compare(this.data, other.data); + } +} + +public class Wildcards { + public static void printBox(Box box) { + System.out.println("Box contains: " + box.get()); + } + + public static void sumNumbers(NumberBox nb) { + System.out.println("Double value: " + nb.doubleValue()); + } +} + +public class Utils { + public static void print(T value) { + System.out.println(value); + } +} From 15590bcf31572b5cdd432d733c5e496c883bd641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= Date: Wed, 10 Dec 2025 18:27:33 +0200 Subject: [PATCH 3/6] Create class Box { private T value; public void set(T value) { this.value = value; } public T get() { return value; } } --- ...ic T get() { return value; } } | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 1-0-java-basics/1-3-1-crazy-generics/class Box { private T value; public void set(T value) { this.value = value; } public T get() { return value; } } diff --git a/1-0-java-basics/1-3-1-crazy-generics/class Box { private T value; public void set(T value) { this.value = value; } public T get() { return value; } } b/1-0-java-basics/1-3-1-crazy-generics/class Box { private T value; public void set(T value) { this.value = value; } public T get() { return value; } } new file mode 100644 index 000000000..ad5551ffb --- /dev/null +++ b/1-0-java-basics/1-3-1-crazy-generics/class Box { private T value; public void set(T value) { this.value = value; } public T get() { return value; } } @@ -0,0 +1,74 @@ +class Box { + private T value; + + public void set(T value) { + this.value = value; + } + + public T get() { + return value; + } +} + +class NumericBox { + private T number; + + public void set(T number) { + this.number = number; + } + + public T get() { + return number; + } +} + +class Pair { + private K key; + private V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } +} + +class ComparableBox> { + private T value; + + public ComparableBox(T value) { + this.value = value; + } + + public boolean isGreaterThan(T other) { + return value.compareTo(other) > 0; + } +} + +class Utils { + public static void printArray(T[] array) { + for (T element : array) { + System.out.print(element + " "); + } + System.out.println(); + } +} + +class WildcardDemo { + public static void showBox(Box box) { + System.out.println("Box contains: " + box.get()); + } +} + +class BoundedWildcardDemo { + public static void sumNumbers(NumericBox box) { + System.out.println("Number is: " + box.get()); + } +} From 2334448f12b1ca64f46466efc7277ff6f594a489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= Date: Wed, 10 Dec 2025 18:28:42 +0200 Subject: [PATCH 4/6] Delete 1-0-java-basics/1-3-1-crazy-generics/class Box { private T value; public void set(T value) { this.value = value; } public T get() { return value; } } --- ...ic T get() { return value; } } | 74 ------------------- 1 file changed, 74 deletions(-) delete mode 100644 1-0-java-basics/1-3-1-crazy-generics/class Box { private T value; public void set(T value) { this.value = value; } public T get() { return value; } } diff --git a/1-0-java-basics/1-3-1-crazy-generics/class Box { private T value; public void set(T value) { this.value = value; } public T get() { return value; } } b/1-0-java-basics/1-3-1-crazy-generics/class Box { private T value; public void set(T value) { this.value = value; } public T get() { return value; } } deleted file mode 100644 index ad5551ffb..000000000 --- a/1-0-java-basics/1-3-1-crazy-generics/class Box { private T value; public void set(T value) { this.value = value; } public T get() { return value; } } +++ /dev/null @@ -1,74 +0,0 @@ -class Box { - private T value; - - public void set(T value) { - this.value = value; - } - - public T get() { - return value; - } -} - -class NumericBox { - private T number; - - public void set(T number) { - this.number = number; - } - - public T get() { - return number; - } -} - -class Pair { - private K key; - private V value; - - public Pair(K key, V value) { - this.key = key; - this.value = value; - } - - public K getKey() { - return key; - } - - public V getValue() { - return value; - } -} - -class ComparableBox> { - private T value; - - public ComparableBox(T value) { - this.value = value; - } - - public boolean isGreaterThan(T other) { - return value.compareTo(other) > 0; - } -} - -class Utils { - public static void printArray(T[] array) { - for (T element : array) { - System.out.print(element + " "); - } - System.out.println(); - } -} - -class WildcardDemo { - public static void showBox(Box box) { - System.out.println("Box contains: " + box.get()); - } -} - -class BoundedWildcardDemo { - public static void sumNumbers(NumericBox box) { - System.out.println("Number is: " + box.get()); - } -} From 8358d72b2e128055a2426947820b41be8076fa2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= Date: Wed, 10 Dec 2025 18:29:14 +0200 Subject: [PATCH 5/6] Add generic classes and utility methods in Java --- .../1-3-1-crazy-generics/1-3-1.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 1-0-java-basics/1-3-1-crazy-generics/1-3-1.java diff --git a/1-0-java-basics/1-3-1-crazy-generics/1-3-1.java b/1-0-java-basics/1-3-1-crazy-generics/1-3-1.java new file mode 100644 index 000000000..272ada0b1 --- /dev/null +++ b/1-0-java-basics/1-3-1-crazy-generics/1-3-1.java @@ -0,0 +1,74 @@ +class Box { + частное значение T; + + public void set(T value) { + это.значение = значение; + } + + public T get() { + возвращаемое значение; + } +} + +class NumericBox { + частный номер T; + + public void set(T number) { + this.number = number; + } + + public T get() { + номер возврата; + } +} + +class Pair { + закрытый ключ K; + частное значение V; + + public Pair(K key, V value) { + this.key = key; + это.значение = значение; + } + + public K getKey() { + клавиша возврата; + } + + public V getValue() { + возвращаемое значение; + } +} + +class ComparableBox> { + частное значение T; + + public ComparableBox(T value) { + это.значение = значение; + } + + public boolean isGreaterThan(T other) { + return value.compareTo(other) > 0; + } +} + +класс Utils { + public static void printArray(T[] array) { + for (T element : array) { + System.out.print(element + " "); + } + System.out.println(); + } +} + +class WildcardDemo { + public static void showBox(Box box) { + System.out.println("Box contains: " + box.get()); + } +} + +class BoundedWildcardDemo { + public static void sumNumbers(NumericBox box) { + System.out.println("Число: " + box.get()); + } +} From 8b57279531d274e14d8b0261e244cc9021c952e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= Date: Thu, 11 Dec 2025 08:21:19 +0200 Subject: [PATCH 6/6] Fix variable declarations and translations in Java code --- .../1-3-1-crazy-generics/1-3-1.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/1-0-java-basics/1-3-1-crazy-generics/1-3-1.java b/1-0-java-basics/1-3-1-crazy-generics/1-3-1.java index 272ada0b1..0826f215d 100644 --- a/1-0-java-basics/1-3-1-crazy-generics/1-3-1.java +++ b/1-0-java-basics/1-3-1-crazy-generics/1-3-1.java @@ -1,50 +1,50 @@ class Box { - частное значение T; + private T value; public void set(T value) { - это.значение = значение; + this.value = value; } public T get() { - возвращаемое значение; + return value; } } class NumericBox { - частный номер T; + private T number; public void set(T number) { this.number = number; } public T get() { - номер возврата; + return number; } } class Pair { - закрытый ключ K; - частное значение V; + private K key; + private V value; public Pair(K key, V value) { this.key = key; - это.значение = значение; + this.value = value; } public K getKey() { - клавиша возврата; + return key; } public V getValue() { - возвращаемое значение; + return value; } } class ComparableBox> { - частное значение T; + private T value; public ComparableBox(T value) { - это.значение = значение; + this.value = value; } public boolean isGreaterThan(T other) { @@ -52,7 +52,7 @@ public boolean isGreaterThan(T other) { } } -класс Utils { +class Utils { public static void printArray(T[] array) { for (T element : array) { System.out.print(element + " "); @@ -69,6 +69,11 @@ public static void showBox(Box box) { class BoundedWildcardDemo { public static void sumNumbers(NumericBox box) { - System.out.println("Число: " + box.get()); + System.out.println("Number: " + box.get()); + } +} + +public class Main { + public static void main(String[] args) { } }