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); + } +} 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..0826f215d --- /dev/null +++ b/1-0-java-basics/1-3-1-crazy-generics/1-3-1.java @@ -0,0 +1,79 @@ +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: " + box.get()); + } +} + +public class Main { + public static void main(String[] args) { + } +} 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