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
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*.zip
*.tar.gz
*.rar
*.iml

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
Expand All @@ -31,5 +32,6 @@ replay_pid*
/.vscode/settings.json
/.idea/vcs.xml
/.idea/uiDesigner.xml
/out
/.idea

# Resources file
resources/*
1 change: 1 addition & 0 deletions resources/introduction/info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello some will come to check if I exists.
19 changes: 13 additions & 6 deletions src/dates/exercises/Exercise1.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dates.exercises;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
* Exercise 1: Basic Date and Time Operations
Expand All @@ -16,16 +17,22 @@
*/
public class Exercise1 {
public static void main(String[] args) {
// TODO: Task 1 - Create your birthday
LocalDate birthday = null; // Replace with your birthday
// Task 1 - Create your birthday
LocalDate birthday = LocalDate.of(1970,10,16);

// TODO: Task 2 - Print formatted birthday message
// Task 2 - Print formatted birthday message
DateTimeFormatter f = DateTimeFormatter.ofPattern("EEEE dd MMMM Y");
System.out.println("I'm born a " + birthday.format(f));


// TODO: Task 3 - Check if leap year
// Task 3 - Check if leap year
String response = birthday.isLeapYear() ? "My birthday is a leap year.": "My birthday is NOT a leap year.";
System.out.println(response);


// TODO: Task 4 - Print day of year you were born

// Task 4 - Print day of year you were born
System.out.println("I'm born the " + birthday.getDayOfYear() + "th day of the year " + birthday.getYear());
System.out.println("It was a "+ birthday.getDayOfWeek() + ".");

}
}
64 changes: 47 additions & 17 deletions src/dates/exercises/Exercise2.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package dates.exercises;

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.TemporalAdjusters;

/**
* Exercise 2: Date Arithmetic
*
Expand All @@ -17,28 +21,54 @@
*/
public class Exercise2 {
public static void main(String[] args) {
// TODO: Task 1 - Create today's date


// TODO: Task 2 - Sprint 1 deadline (7 days)


// TODO: Task 3 - Sprint 2 deadline (2 weeks)


// TODO: Task 4 - Mid-project review (1 month)


// TODO: Task 5 - Project end (3 months)


// TODO: Task 6 - Pre-planning phase (15 days before today)
// Task 1 - Create today's date
LocalDate today = LocalDate.now();
System.out.println("Today: " + today);

// Task 2 - Sprint 1 deadline (7 days)
LocalDate sp1DeadLine = today.plusDays(7);
System.out.println("Sprint 1 deadline: " + sp1DeadLine);
long nbDays = sp1DeadLine.toEpochDay() - today.toEpochDay();
System.out.println("How many days before Sprint 1 deadline? " + nbDays + " days.");
System.out.println("=".repeat(20));

// Task 3 - Sprint 2 deadline (2 weeks)
LocalDate sp2DeadLine = today.plusWeeks(2);
System.out.println("Sprint 2 deadline: " + sp2DeadLine);
nbDays = sp2DeadLine.toEpochDay() - today.toEpochDay();
System.out.println("How many days before Sprint 2 deadline? " + nbDays + " days.");
System.out.println("=".repeat(20));

// Task 4 - Mid-project review (1 month)
LocalDate midReview = today.plusMonths(1);
System.out.println("Mid-project review: " + midReview);
nbDays = midReview.toEpochDay() - today.toEpochDay();
System.out.println("How many days before the Mid-project review? " + nbDays + " days.");
System.out.println("=".repeat(20));

// Task 5 - Project end (3 months)
LocalDate endOfP = today.plusMonths(3);
System.out.println("Project end: " + endOfP);
nbDays =endOfP.toEpochDay() - today.toEpochDay();
System.out.println("How many days before the end of the project? " + nbDays + " days.");
System.out.println("=".repeat(20));

// TODO: Task 7 - 2 months and 10 days from today
// Task 6 - Pre-planning phase (15 days before today)
LocalDate preP = today.minusDays(15);
System.out.println("Pre-planning phase: " + preP);
System.out.println("How many days before?" + Period.between(today,preP).getDays() + " days.");
System.out.println("=".repeat(20));

// Task 7 - 2 months and 10 days from today
LocalDate holidays = today.plusMonths(2).plusDays(10);
System.out.println("My holidays: " + holidays);
nbDays = holidays.toEpochDay() - today.toEpochDay();
System.out.println("How many days before my holidays? " + nbDays + " days.");


// TODO: BONUS - Last day of next month (Hint: use TemporalAdjusters)
LocalDate bonus = today.plusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
System.out.println("Last day of next month: " + (bonus.getDayOfWeek()).toString().toLowerCase());

}
}
63 changes: 44 additions & 19 deletions src/dates/exercises/Exercise3.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package dates.exercises;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
import java.time.temporal.TemporalAdjusters;

/**
* Exercise 3: Period and Duration Calculations
Expand All @@ -19,36 +22,58 @@
*/
public class Exercise3 {
public static void main(String[] args) {
// TODO: Task 1 - Calculate your age
LocalDate birthday = LocalDate.of(2000, 1, 1); // Replace with your birthday
// Task 1 - Calculate your age
LocalDate birthday = LocalDate.of(1970, 02, 16);
LocalDate today = LocalDate.now();


// TODO: Task 2 - Create a method to calculate days between dates
int myAge = Period.between(birthday,today).getYears();
System.out.println("My age: " + myAge);


// Task 2 - Create a method to calculate days between dates
// Call it with two different dates and print the result


// TODO: Task 3 - Calculate working hours
LocalDate nextBirthday = LocalDate.of(2027, 02, 16);

System.out.println("Number of days to wait before my birthday: " + calculateDaysBetween( today,nextBirthday));

// Task 3 - Calculate working hours
LocalTime workStart = LocalTime.of(9, 0);
LocalTime workEnd = LocalTime.of(17, 30);


// TODO: Task 4 - Calculate actual working time (subtract 30 min lunch)


// TODO: Task 5 - Days until New Year 2027
LocalTime workEnd = LocalTime.of(17, 0);
Duration workDay = Duration.between(workStart,workEnd);
System.out.println("workDay " + workDay.toHours());

// Task 4 - Calculate actual working time (subtract 30 min lunch)
long init = workDay.minusMinutes(30).getSeconds();
long seconds = init % 60;
long temp = (init - seconds) / 60;
long minutes = temp % 60;
long hours = (temp - minutes) / 60;
if(seconds > 0){
System.out.println("Actual working time: " + hours + ":" + minutes + ":" + seconds);
}
else{
System.out.println("Actual working time: " + hours + ":" + minutes);
}

// Task 5 - Days until New Year 2027
LocalDate newY = today.with(TemporalAdjusters.lastDayOfYear()).plusDays(1);
long newYdays = newY.toEpochDay() - today.toEpochDay();
System.out.println("Days until New Year 2027: " + newYdays);


// TODO: Task 6 - Add period to today
// Task 6 - Add period to today
Period toNY = Period.between(today, newY);
System.out.println("Add period to today: " + today.plus(toNY));


// TODO: BONUS - Calculate full weeks between birthday and today
long fullWeeks = Math.round( newYdays / 7);
System.out.println("Full weeks between birthday and today: " + fullWeeks );

}

// TODO: Task 2 - Implement this method
// Task 2 - Implement this method
public static long calculateDaysBetween(LocalDate start, LocalDate end) {
// Your code here
return 0;

return Math.abs(start.toEpochDay() - end.toEpochDay());
}
}
118 changes: 71 additions & 47 deletions src/generics/exercises/Exercise1.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package generics.exercises;


import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
* Exercise 1: Basic Generic Classes
*
Expand All @@ -23,61 +28,80 @@
* - Pair<String, Integer> for ("age", 25)
* - Swap it and print both pairs
*/
public class Exercise1
{
public static void main(String[] args)
{

Container<String> stringContainer = new Container<>("Hello");
Container<Integer> intContainer = new Container<>(100);
Container<Boolean> boolContainer = new Container<>(true);

System.out.println("String value: " + stringContainer.getValue());
System.out.println("Integer value: " + intContainer.getValue());
System.out.println("Boolean value: " + boolContainer.getValue());


Pair<String, Integer> agePair = new Pair<>("age", 25);

System.out.println("Original Pair: " +
agePair.getKey() + " = " + agePair.getValue());

Pair<Integer, String> swapped = agePair.swap();

System.out.println("Swapped Pair: " + swapped);
public class Exercise1 {

public static void main(String[] args) {
System.out.println("=== Task 1 & 2: Container Class ===\n");

// Create Container instances and use them
Container s = new Container<String>("I am a String");
Container i = new Container<Integer>(456);
Container l = new Container<LocalDate>(LocalDate.now());
Container a = new Container<List>(new ArrayList<>());
Container n = new Container<Object>(null);

s.printValue();
i.printValue();
l.printValue();
a.printValue();
System.out.println("If this value null? " + s.isEmpty());
System.out.println("If this value null? " + n.isEmpty());

System.out.println("\n=== Task 3 & 4: Pair Class ===\n");

// Create Paire instances, try swap() method.
Paire b = new Paire("Roberta", 24);
System.out.println("Before swap");
System.out.println(b.toString());
Paire c = b.swap();
System.out.println("After swap");
System.out.println(c.toString());
}
}

class Container<T> {
// Task 1 - Create Container<T> class here
class Container<T>{

private T value;
T value;
public Container(T value){
setValue(value);

public Container(T value) {
this.value = value;
}

public T getValue() {
return value;
}

public void setValue(T value) {
this.value = value;
}

public boolean isEmpty() {
return value == null;
public T getValue(){
return this.value;
}
public void printValue(){
System.out.println("Class of value: " + value.getClass());
}
// method that returns true if value is null.
public boolean isEmpty(){
return this.value == null;
}
}

class Pair<K, V> {
private K key;
private V value;

public Pair(K key, V value) {
this.key = key;
this.value = value;
// Task 3 - Create Pair<K, V> class here
class Paire<K,V>{
K key;
V value;
public Paire(K key, V value){
setKey(key);
setValue(value);
}
// SWAP METHOD
public Paire swap(){
return new Paire(this.getValue(), this.getKey());
}
// TOSTRING METHOD
@Override
public String toString() {
return "Key: " + this.getKey() + " Value: " + this.getValue();
}

// GETTERS
public K getKey() {
return key;
}
Expand All @@ -86,12 +110,12 @@ public V getValue() {
return value;
}

public Pair<V, K> swap() {
return new Pair<>(value, key);
// SETTERS
public void setKey(K key){
this.key = key;
}

@Override
public String toString() {
return "K: " + this.key + ", V: " + this.value;
public void setValue(V value){
this.value = value;
}
}

}
Loading