Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions HYFBE-java-week-5.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file removed resources/cat.jpeg
Binary file not shown.
Empty file added resources/data.txt
Empty file.
5 changes: 5 additions & 0 deletions resources/introduction/ks_serotonine.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Once upon a time there was a little dinosaur.
This little dinosaur was very sad. Its mom had disappeared for a long time now, and it felt so lonely and insecure!
Our little dinosaur wandered all day through the huge trees and plants of the primary forest, yelling non-stop after its mom.
Rexor the Tyrannosaurus had a very fine ear… and an acute nose. Danger was there!
The sky got darker… the day was vanishing, and the forest filled with shadows.
Empty file.
Empty file added resources/introduction/new.txt
Empty file.
2 changes: 2 additions & 0 deletions resources/introduction/notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Java I/O is powerful.
Streams make reading and writing easier.
Binary file added resources/introduction/smiling_pit.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/introduction/smiling_pit_copy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
1 change: 1 addition & 0 deletions resources/introduction/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Once upon a time
1 change: 1 addition & 0 deletions resources/test/subtest/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Une souris verte...
143 changes: 110 additions & 33 deletions src/generics/exercises/Exercise2.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generics.exercises;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -31,50 +32,126 @@
public class Exercise2 {

public static void main(String[] args) {
String[] words = {"Hello", "World", "Java", "Zia petit toutou"};
Integer[] numbers = {0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Double[] doubles = {133.99, 24.50, 33.99, 4d, 575.98};
List<Integer> numbersList = new ArrayList<>(List.of(numbers));
List<Double> doublesList = new ArrayList<>(List.of(doubles));

System.out.println("=== Task 1: Print Array ===\n");

// TODO: Call printArray method
String[] words = {"Hello", "World", "Java"};
Integer[] numbers = {1, 2, 3, 4, 5};


printArray(words); printArray(numbers); printArray(doubles);

System.out.println("\n=== Task 2: Reverse Array ===\n");

// TODO: Call reverse method


Integer[] reverseNumbers = reverse(numbers);
System.out.println(Arrays.toString(reverseNumbers));
Double[] reverseDoubles = reverse(doubles);
System.out.println(Arrays.toString(reverseDoubles));

System.out.println("\n=== Task 3: Find Minimum ===\n");

// TODO: Call findMin method
List<Integer> integers = new ArrayList<>();
integers.add(10);
integers.add(5);
integers.add(20);
integers.add(3);

System.out.println("Min: " + findMin(numbersList));
System.out.println("Min: " + findMin(doublesList));


System.out.println("\n=== Task 4: Calculator ===\n");

// TODO: Create and use Calculator instances

Calculator ci = new Calculator<>(numbers);
System.out.println("[ADD] " + ci.add());
System.out.println("[SUBSTRACT] " + ci.substract());
System.out.println("[MULTIPLY] " + ci.multiply());
Calculator cd = new Calculator<>(doubles);
System.out.println("[ADD] " + cd.add());
System.out.println("[SUBSTRACT] " + cd.substract());
System.out.printf("[MULTIPLY] %.2f %n",cd.multiply());



System.out.println("\n=== Task 5: Count Greater Than ===\n");

// TODO: Call countGreaterThan method

System.out.printf("Greater elements: %d%n", countGreaterThan(words, "Vernaculaire"));
System.out.printf("Greater elements: %d%n", countGreaterThan(numbers, 5));
System.out.printf("Greater elements: %d%n", countGreaterThan(doubles, 100d));

}

// TODO: Task 1 - Implement printArray method


// TODO: Task 2 - Implement reverse method


// TODO: Task 3 - Implement findMin method
// Task 1 - Implement printArray method
public static <T> void printArray(T[] array){
if(array.length == 0){
System.out.println("Empty array.");
return;
}
for (T item:array){
System.out.print(item + " ");
}
System.out.println();
}

// Task 2 - Implement reverse method
public static <T> T[] reverse(T[] array){

T [] reverse = array;
int lg = reverse.length -1;
for(int i = 0; i<= lg / 2; i++){
T temp = reverse[i];
reverse[i] = reverse[lg-i];
reverse[lg-i] = temp;
}
return reverse;
}


// TODO: Task 5 - Implement countGreaterThan method
// Task 3 - Implement findMin method
public static double findMin(List<? extends Number> numbers){
double min = numbers.get(0).doubleValue();
for(Number item:numbers){
double i = item.doubleValue();
min = min < i ? min: i;
}
return min;
}


// Task 5 - Implement countGreaterThan method
public static <T extends Comparable<T>> int countGreaterThan(T[] array, T element ){
int count = 0;
for(T item:array){
if( element.compareTo(item) < 0){
System.out.println(item + " is greater than " + element);
count++;
}
}
return count;
}

}

// TODO: Task 4 - Create Calculator<T extends Number> class here
// Task 4 - Create Calculator<T extends Number> class here
class Calculator<T extends Number>{
T[] array;
public Calculator(T[] array){
this.array = array;
}
protected double add(){
double count = this.array[0].doubleValue();
int lg = this.array.length;
for (int i = 1; i < lg; i++) {
count += this.array[i].doubleValue();
}
return count;
}
protected double substract(){
double count = this.array[0].doubleValue();
int lg = this.array.length;
for (int i = 1; i < lg; i++) {
count -= this.array[i].doubleValue();
}
return count;
}
protected double multiply(){
double count = this.array[0].doubleValue();
int lg = this.array.length ;
for (int i = 1; i < lg; i++) {
count *= this.array[i].doubleValue();
}
return count;
}

}

93 changes: 0 additions & 93 deletions src/generics/exercises/Exercise3.java

This file was deleted.

Loading