Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
twitch
live-coding-fr
Commits
6079210f
Commit
6079210f
authored
Apr 20, 2021
by
Colin DAMON
Browse files
Java puzzles subject
parent
fca2359e
Changes
11
Hide whitespace changes
Inline
Side-by-side
.gitlab-ci.yml
View file @
6079210f
...
...
@@ -35,3 +35,4 @@ include:
-
local
:
"
/tennis/greenfield-2/.gitlab-ci.yml"
-
local
:
"
/mastermind/.gitlab-ci.yml"
-
local
:
"
/try-monade/.gitlab-ci.yml"
-
local
:
"
/java-puzzles/.gitlab-ci.yml"
java-puzzles/.gitlab-ci.yml
0 → 100644
View file @
6079210f
package-java-puzzles
:
variables
:
PROJECT_FOLDER
:
"
java-puzzles"
extends
:
.java
only
:
refs
:
-
master
-
merge_requests
changes
:
-
"
.gitlab-common-ci.yml"
-
"
java-puzzles/**/*"
java-puzzles/pom.xml
0 → 100644
View file @
6079210f
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<parent>
<version>
1.0.0
</version>
<groupId>
fr.ippon.kata
</groupId>
<artifactId>
java-parent
</artifactId>
<relativePath>
../java-parent
</relativePath>
</parent>
<version>
1.0.0-SNAPSHOT
</version>
<artifactId>
java-puzzles
</artifactId>
<name>
JavaPuzzles
</name>
<developers>
<developer>
<email>
cdamon@ippon.fr
</email>
<name>
Colin DAMON
</name>
</developer>
</developers>
</project>
java-puzzles/readme.md
0 → 100644
View file @
6079210f
# Java puzzles
Petits exercices de code Java
-
**Auteurs**
: Colin DAMON
-
**Date**
: 22/04/2021
-
**Langage**
: Java
-
**Niveau**
: TODO
-
**Replay**
: TODO
java-puzzles/src/test/java/fr/ippon/PlayingWithBigDecimal.java
0 → 100644
View file @
6079210f
package
fr.ippon
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.*;
import
java.math.BigDecimal
;
import
org.junit.jupiter.api.Test
;
public
class
PlayingWithBigDecimal
{
@Test
public
void
roundingSubstract
()
{
BigDecimal
a
=
new
BigDecimal
(
2.00
);
BigDecimal
b
=
new
BigDecimal
(
0.99
);
// assertThat(a.subtract(b).toString()).isEqualTo("1");
// assertThat(a.subtract(b).toString()).isEqualTo("1.01");
// assertThat(a.subtract(b).toString()).isEqualTo("1.0100000000000000088817841970012523233890533447265625");
}
}
java-puzzles/src/test/java/fr/ippon/PlayingWithFinally.java
0 → 100644
View file @
6079210f
package
fr.ippon
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.*;
import
org.junit.jupiter.api.Test
;
public
class
PlayingWithFinally
{
@Test
public
void
playingWithReturn
()
{
// assertThat(finallyReturn()).isEqualTo(42);
// assertThat(finallyReturn()).isEqualTo(1337);
}
@SuppressWarnings
(
"finally"
)
private
int
finallyReturn
()
{
try
{
return
42
;
}
finally
{
return
1337
;
}
}
@Test
public
void
playingWithThrow
()
{
// assertThat(finallyThrow()).isEqualTo(42);
// assertThatThrownBy(() -> finallyThrow())
// .isInstanceOf(IllegalArgumentException.class)
// .hasMessageContaining("Damned");
}
@SuppressWarnings
(
"finally"
)
private
int
finallyThrow
()
{
try
{
return
42
;
}
finally
{
throw
new
IllegalArgumentException
(
"Damned"
);
}
}
}
java-puzzles/src/test/java/fr/ippon/PlayingWithInstanciations.java
0 → 100644
View file @
6079210f
package
fr.ippon
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.*;
import
org.junit.jupiter.api.Test
;
public
class
PlayingWithInstanciations
{
private
static
final
long
instanciation
=
System
.
currentTimeMillis
();
@Test
public
void
playingWithInstanciationTime
()
throws
InterruptedException
{
Thread
.
sleep
(
10
);
long
start
=
System
.
currentTimeMillis
();
// assertThat(start).isLessThanOrEqualTo(instanciation);
// assertThat(start).isGreaterThan(instanciation);
MyPrivateClass
.
doStuff
();
// assertThat(start).isLessThanOrEqualTo(MyPrivateClass.instanciation);
// assertThat(start).isGreaterThan(MyPrivateClass.instanciation);
}
private
static
class
MyPrivateClass
{
private
static
final
long
instanciation
=
System
.
currentTimeMillis
();
private
static
void
doStuff
()
{}
}
}
java-puzzles/src/test/java/fr/ippon/PlayingWithInteger.java
0 → 100644
View file @
6079210f
package
fr.ippon
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.*;
import
org.junit.jupiter.api.Test
;
public
class
PlayingWithInteger
{
@Test
public
void
comparingUniversaleAnswerIntegerReferences
()
{
Integer
a
=
42
;
Integer
b
=
42
;
// assertThat(a).isSameAs(b);
// assertThat(a).isNotSameAs(b);
}
@Test
public
void
comparingDevilIntegerReferences
()
{
Integer
a
=
666
;
Integer
b
=
666
;
// assertThat(a).isSameAs(b);
// assertThat(a).isNotSameAs(b);
}
@Test
public
void
checkingAbsoluteValue
()
{
// assertThat(Math.abs(Integer.MIN_VALUE)).isEqualTo(Integer.MAX_VALUE);
// assertThat(Math.abs(Integer.MIN_VALUE))
// .isEqualTo(Integer.MIN_VALUE);
}
}
java-puzzles/src/test/java/fr/ippon/PlayingWithString.java
0 → 100644
View file @
6079210f
package
fr.ippon
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.*;
import
org.junit.jupiter.api.Test
;
public
class
PlayingWithString
{
@Test
public
void
playingWithReferences
()
{
// assertThat("coucou").isSameAs("coucou");
// assertThat("coucou").isNotSameAs("coucou");
}
@Test
public
void
playingWithStringObjectsReferences
()
{
// assertThat(new String("coucou")).isSameAs(new String("coucou"));
// assertThat(new String("coucou")).isNotSameAs(new String("coucou"));
}
@Test
public
void
playingWithIntern
()
{
// assertThat(new String("coucou").intern())
// .isSameAs(new String("coucou").intern());
// assertThat(new String("coucou").intern()).isNotSameAs(new
// String("coucou").intern());
}
}
java-puzzles/src/test/java/fr/ippon/PlayingWithThreads.java
0 → 100644
View file @
6079210f
package
fr.ippon
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.*;
import
java.text.SimpleDateFormat
;
import
java.util.Calendar
;
import
java.util.Collections
;
import
java.util.Date
;
import
java.util.GregorianCalendar
;
import
java.util.Set
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.atomic.AtomicBoolean
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
org.junit.jupiter.api.Test
;
public
class
PlayingWithThreads
{
private
static
boolean
threadDoneFlag
;
private
static
AtomicBoolean
threadDoneAtomicFlag
=
new
AtomicBoolean
(
false
);
@Test
public
void
playingWithSimpleDateFormat
()
throws
InterruptedException
{
Set
<
Integer
>
days
=
Collections
.
newSetFromMap
(
new
ConcurrentHashMap
<>());
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
10
);
AtomicInteger
counter
=
new
AtomicInteger
(
1
);
for
(
int
i
=
1
;
i
<
32
;
++
i
)
{
executor
.
submit
(
()
->
{
SimpleDateFormat
format
=
new
SimpleDateFormat
(
"dd"
);
Date
date
=
new
GregorianCalendar
(
2018
,
Calendar
.
JANUARY
,
counter
.
getAndIncrement
()).
getTime
();
days
.
add
(
Integer
.
parseInt
(
format
.
format
(
date
)));
}
);
}
executor
.
shutdown
();
executor
.
awaitTermination
(
3
,
TimeUnit
.
SECONDS
);
// assertThat(days).hasSize(31);
// assertThat(days.size()).isLessThan(31);
}
@Test
public
void
firstAttemptToStopAThread
()
throws
InterruptedException
{
AtomicBoolean
threadStopped
=
new
AtomicBoolean
();
new
Thread
(
()
->
{
while
(!
threadDoneFlag
)
{}
threadStopped
.
set
(
true
);
}
)
.
start
();
Thread
.
sleep
(
500
);
threadDoneFlag
=
true
;
Thread
.
sleep
(
50
);
// assertThat(threadStopped.get()).isFalse();
// assertThat(threadStopped.get()).isTrue();
}
@Test
public
void
secondAttemptToStopAThread
()
throws
InterruptedException
{
AtomicBoolean
threadStopped
=
new
AtomicBoolean
();
new
Thread
(
()
->
{
while
(!
threadDoneFlag
)
{
System
.
out
.
println
(
threadDoneFlag
);
// Ok, let's just log the flag
}
threadStopped
.
set
(
true
);
}
)
.
start
();
Thread
.
sleep
(
500
);
threadDoneFlag
=
true
;
Thread
.
sleep
(
50
);
// assertThat(threadStopped.get()).isFalse();
// assertThat(threadStopped.get()).isTrue();
}
@Test
public
void
thirdAttemptToStopAThread
()
throws
InterruptedException
{
AtomicBoolean
threadStopped
=
new
AtomicBoolean
();
new
Thread
(
()
->
{
while
(!
threadDoneAtomicFlag
.
get
())
{}
threadStopped
.
set
(
true
);
}
)
.
start
();
Thread
.
sleep
(
500
);
threadDoneAtomicFlag
.
set
(
true
);
Thread
.
sleep
(
50
);
// assertThat(threadStopped.get()).isFalse();
// assertThat(threadStopped.get()).isTrue();
}
}
readme.md
View file @
6079210f
...
...
@@ -43,6 +43,7 @@ Un kata de code est un petit exercice pensé pour s'entrainer jusqu'à maitriser
-
[
Movie rental
](
/movie-rental
)
-
[
Bowling Game
](
/bowling-game
)
-
[
Tennis Refactoring kata
](
/tennis/refactoring
)
-
[
Puzzles
](
java-puzzles
)
### Énervé
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment