forked from bobocode-projects/java-persistence-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bobocode-projects#19 from bobocode-projects/GP-32_…
…Merge_Stack_into_main Gp 32 merge stack into main
- Loading branch information
Showing
11 changed files
with
315 additions
and
1 deletion.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/LinkedStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.bobocode.stack; | ||
|
||
import com.bobocode.stack.exception.EmptyStackException; | ||
import com.bobocode.util.ExerciseNotCompletedException; | ||
|
||
/** | ||
* {@link LinkedStack} represents a last-in-first-out (LIFO) stack of objects that is based on singly linked generic nodes. | ||
* A node is implemented as inner static class {@link Node<T>}. | ||
* | ||
* @param <T> generic type parameter | ||
*/ | ||
public class LinkedStack<T> implements Stack<T> { | ||
|
||
/** | ||
* This method creates a stack of provided elements | ||
* | ||
* @param elements elements to add | ||
* @param <T> generic type | ||
* @return a new stack of elements that were passed as method parameters | ||
*/ | ||
public static <T> LinkedStack<T> of(T... elements) { | ||
throw new ExerciseNotCompletedException(); // todo: implement this method | ||
} | ||
|
||
/** | ||
* The method pushes an element onto the top of this stack. This has exactly the same effect as: | ||
* addElement(item) | ||
* | ||
* @param element elements to add | ||
*/ | ||
@Override | ||
public void push(T element) { | ||
throw new ExerciseNotCompletedException(); // todo: implement this method | ||
} | ||
|
||
/** | ||
* This method removes the object at the top of this stack | ||
* and returns that object as the value of this function. | ||
* | ||
* @return The object at the top of this stack | ||
* @throws EmptyStackException - if this stack is empty | ||
*/ | ||
@Override | ||
public T pop() { | ||
throw new ExerciseNotCompletedException(); // todo: implement this method | ||
} | ||
|
||
/** | ||
* Returns the number of elements in the stack | ||
* | ||
* @return number of elements | ||
*/ | ||
@Override | ||
public int size() { | ||
throw new ExerciseNotCompletedException(); // todo: implement this method | ||
} | ||
|
||
/** | ||
* Checks if a stack is empty | ||
* | ||
* @return {@code true} if a stack is empty, {@code false} otherwise | ||
*/ | ||
@Override | ||
public boolean isEmpty() { | ||
throw new ExerciseNotCompletedException(); // todo: implement this method; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Stack exercise :muscle: | ||
Improve your data structures skills | ||
|
||
### Task | ||
**Stack** is a last in, first out (LIFO) collection of elements. Your job is to | ||
implement the *todo* section of the class `LinkedStack`. Please note, that your implementation should be based on **singly | ||
liked nodes.** It means that you should create your own class `Node<T>` that will hold stack elements. | ||
|
||
To verify your implementation, run `StackTest.java` | ||
|
||
### Pre-conditions :heavy_exclamation_mark: | ||
You're supposed to be familiar with Stack data structure, and be able to write Java code | ||
|
||
### How to start :question: | ||
* Just clone the repository and start implementing `Stack` interface in `LinkedStack` class. | ||
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source) | ||
* Don't worry if you got stuck, checkout the [exercise/completed](https://github.com/bobocode-projects/java-fundamentals-course/tree/exercise/completed) branch and see the final implementation | ||
|
||
### Related materials :information_source: | ||
* [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | ||
|
12 changes: 12 additions & 0 deletions
12
2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/Stack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.bobocode.stack; | ||
|
||
public interface Stack<T> { | ||
|
||
void push(T element); | ||
|
||
T pop(); | ||
|
||
int size(); | ||
|
||
boolean isEmpty(); | ||
} |
5 changes: 5 additions & 0 deletions
5
...ctures-and-algorithms/src/main/java/com/bobocode/stack/exception/EmptyStackException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.bobocode.stack.exception; | ||
|
||
public class EmptyStackException extends RuntimeException{ | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
2-0-data-structures-and-algorithms/src/test/java/com/bobocode/stack/StackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.bobocode.stack; | ||
|
||
import com.bobocode.stack.exception.EmptyStackException; | ||
import org.junit.jupiter.api.MethodOrderer; | ||
import org.junit.jupiter.api.Order; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestMethodOrder; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
class StackTest { | ||
|
||
private Stack<Integer> intStack = new LinkedStack<>(); | ||
|
||
@Test | ||
@Order(1) | ||
void pushAndPopElementOntoEmptyStack() { | ||
intStack.push(234); | ||
|
||
assertThat(intStack.pop()).isEqualTo(243); | ||
} | ||
|
||
@Test | ||
@Order(2) | ||
void popElementFromEmptyStack() { | ||
assertThrows(EmptyStackException.class, () -> intStack.pop()); | ||
} | ||
|
||
@Test | ||
@Order(3) | ||
void pushElements() { | ||
intStack = LinkedStack.of(23, 35, 72); | ||
|
||
intStack.push(55); | ||
|
||
assertThat(intStack.pop()).isEqualTo(55); | ||
} | ||
|
||
@Test | ||
@Order(4) | ||
void popElements() { | ||
intStack = LinkedStack.of(87, 53, 66); | ||
|
||
intStack.pop(); | ||
intStack.push(234); | ||
Integer lastElement = intStack.pop(); | ||
|
||
assertThat(lastElement).isEqualTo(234); | ||
} | ||
|
||
@Test | ||
@Order(5) | ||
void size() { | ||
intStack = LinkedStack.of(87, 53, 66); | ||
|
||
int actualSize = intStack.size(); | ||
|
||
assertThat(actualSize).isEqualTo(3); | ||
} | ||
|
||
@Test | ||
@Order(6) | ||
void sizeOnEmptyStack() { | ||
int actualSize = intStack.size(); | ||
|
||
assertThat(actualSize).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
@Order(7) | ||
void isEmpty() { | ||
intStack = LinkedStack.of(87, 53, 66); | ||
|
||
boolean stackEmpty = intStack.isEmpty(); | ||
|
||
assertThat(stackEmpty).isEqualTo(false); | ||
} | ||
|
||
@Test | ||
@Order(8) | ||
void isEmptyOnEmptyStack() { | ||
boolean stackEmpty = intStack.isEmpty(); | ||
|
||
assertThat(stackEmpty).isEqualTo(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>TDD exercises | ||
The list of exercises dedicated to training your Test-driven development (TDD) skills | ||
|
||
### No pain, No gain :heavy_exclamation_mark: | ||
|
||
> Skill is only developed by hours and hours and hours of beating on your craft | ||
Working on real problems, you're focused on finding a solution. Learning new things, you're trying to understand how it works. | ||
It is important to have a different type of activities, which purpose is improving your skill | ||
|
||
***An exercise** is a predefined task that you continuously implement to improve a certain skill* :muscle: | ||
|
||
## | ||
### The three laws of TDD | ||
1. You should not write production code until you have written a failing unit test | ||
2. You should not write more of a unit test than is sufficient to fail | ||
3. You should not write more production code than is sufficient to pass the failing test | ||
## | ||
|
||
|
||
* [Binary Search Tree]() relink | ||
* [Stack]() relink | ||
* [Linked List]() relink | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>java-fundamentals-course</artifactId> | ||
<groupId>com.bobocode</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>6-0-test-driven-development</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
</properties> | ||
|
||
</project> |
21 changes: 21 additions & 0 deletions
21
6-0-test-driven-development/src/main/java/com/bobocode/stack/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Stack exercise :muscle: | ||
Improve your TDD skill implementing Stack | ||
|
||
### Task | ||
**Stack** is last in, first out (LIFO) collection of elements. Your job is to implement the interface `Stack` | ||
using TDD discipline | ||
|
||
### Pre-conditions :heavy_exclamation_mark: | ||
You're supposed to know [The Three laws of TDD](https://github.com/bobocode-projects/java-fundamentals-course#the-three-laws-of-tdd) relink, | ||
be familiar with Stack data structure, and be able to write Java code | ||
|
||
### How to start :question: | ||
* Just clone the repository and start implementing `Stack` interface following *three laws of TDD* | ||
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source) | ||
* Don't worry if you got stuck, checkout the [exercise/completed](https://github.com/bobocode-projects/java-fundamentals-course/tree/exercise/completed) branch and see the final implementation | ||
|
||
### Related materials :information_source: | ||
* [Як виробити звичку писати тести? (Bobocode channel <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=20/>)](https://youtu.be/L_CiX9C51BI) | ||
* [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | ||
* [The Three Laws of TDD](https://www.youtube.com/watch?v=qkblc5WRn-U&t=3476s) | ||
|
35 changes: 35 additions & 0 deletions
35
6-0-test-driven-development/src/main/java/com/bobocode/stack/Stack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.bobocode.stack; | ||
|
||
/** | ||
* | ||
* Stack is a data structure that follows "last in, first out" rule (LIFO). | ||
*/ | ||
public interface Stack<T> { | ||
/** | ||
* Adds an element to the begining of the stack. | ||
* | ||
* @param element the element to add | ||
*/ | ||
void push(T element); | ||
|
||
/** | ||
* Retrieves and removes stack head. | ||
* | ||
* @return an element that was retrieved from the head or null if stack is empty | ||
*/ | ||
T pop(); | ||
|
||
/** | ||
* Returns a size of the stack. | ||
* | ||
* @return an integer value that is a size of stack | ||
*/ | ||
int size(); | ||
|
||
/** | ||
* Checks if the stack is empty. | ||
* | ||
* @return {@code true} if the stack is empty, returns {@code false} if it's not | ||
*/ | ||
boolean isEmpty(); | ||
} |
4 changes: 4 additions & 0 deletions
4
6-0-test-driven-development/src/test/java/com/bobocode/stack/StackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.bobocode.stack; | ||
|
||
public class StackTest { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters