Skip to content

Commit 10eb8ed

Browse files
committed
Merge branch 'main' into exercise/completed
2 parents 47923cb + dd5a328 commit 10eb8ed

File tree

11 files changed

+315
-1
lines changed

11 files changed

+315
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.bobocode.stack;
2+
3+
import com.bobocode.stack.exception.EmptyStackException;
4+
import com.bobocode.util.ExerciseNotCompletedException;
5+
6+
/**
7+
* {@link LinkedStack} represents a last-in-first-out (LIFO) stack of objects that is based on singly linked generic nodes.
8+
* A node is implemented as inner static class {@link Node<T>}.
9+
*
10+
* @param <T> generic type parameter
11+
*/
12+
public class LinkedStack<T> implements Stack<T> {
13+
14+
/**
15+
* This method creates a stack of provided elements
16+
*
17+
* @param elements elements to add
18+
* @param <T> generic type
19+
* @return a new stack of elements that were passed as method parameters
20+
*/
21+
public static <T> LinkedStack<T> of(T... elements) {
22+
throw new ExerciseNotCompletedException(); // todo: implement this method
23+
}
24+
25+
/**
26+
* The method pushes an element onto the top of this stack. This has exactly the same effect as:
27+
* addElement(item)
28+
*
29+
* @param element elements to add
30+
*/
31+
@Override
32+
public void push(T element) {
33+
throw new ExerciseNotCompletedException(); // todo: implement this method
34+
}
35+
36+
/**
37+
* This method removes the object at the top of this stack
38+
* and returns that object as the value of this function.
39+
*
40+
* @return The object at the top of this stack
41+
* @throws EmptyStackException - if this stack is empty
42+
*/
43+
@Override
44+
public T pop() {
45+
throw new ExerciseNotCompletedException(); // todo: implement this method
46+
}
47+
48+
/**
49+
* Returns the number of elements in the stack
50+
*
51+
* @return number of elements
52+
*/
53+
@Override
54+
public int size() {
55+
throw new ExerciseNotCompletedException(); // todo: implement this method
56+
}
57+
58+
/**
59+
* Checks if a stack is empty
60+
*
61+
* @return {@code true} if a stack is empty, {@code false} otherwise
62+
*/
63+
@Override
64+
public boolean isEmpty() {
65+
throw new ExerciseNotCompletedException(); // todo: implement this method;
66+
}
67+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Stack exercise :muscle:
2+
Improve your data structures skills
3+
4+
### Task
5+
**Stack** is a last in, first out (LIFO) collection of elements. Your job is to
6+
implement the *todo* section of the class `LinkedStack`. Please note, that your implementation should be based on **singly
7+
liked nodes.** It means that you should create your own class `Node<T>` that will hold stack elements.
8+
9+
To verify your implementation, run `StackTest.java`
10+
11+
### Pre-conditions :heavy_exclamation_mark:
12+
You're supposed to be familiar with Stack data structure, and be able to write Java code
13+
14+
### How to start :question:
15+
* Just clone the repository and start implementing `Stack` interface in `LinkedStack` class.
16+
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
17+
* 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
18+
19+
### Related materials :information_source:
20+
* [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type))
21+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.bobocode.stack;
2+
3+
public interface Stack<T> {
4+
5+
void push(T element);
6+
7+
T pop();
8+
9+
int size();
10+
11+
boolean isEmpty();
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.bobocode.stack.exception;
2+
3+
public class EmptyStackException extends RuntimeException{
4+
5+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.bobocode.stack;
2+
3+
import com.bobocode.stack.exception.EmptyStackException;
4+
import org.junit.jupiter.api.MethodOrderer;
5+
import org.junit.jupiter.api.Order;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.TestMethodOrder;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
import static org.junit.jupiter.api.Assertions.assertThrows;
11+
12+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
13+
class StackTest {
14+
15+
private Stack<Integer> intStack = new LinkedStack<>();
16+
17+
@Test
18+
@Order(1)
19+
void pushAndPopElementOntoEmptyStack() {
20+
intStack.push(234);
21+
22+
assertThat(intStack.pop()).isEqualTo(243);
23+
}
24+
25+
@Test
26+
@Order(2)
27+
void popElementFromEmptyStack() {
28+
assertThrows(EmptyStackException.class, () -> intStack.pop());
29+
}
30+
31+
@Test
32+
@Order(3)
33+
void pushElements() {
34+
intStack = LinkedStack.of(23, 35, 72);
35+
36+
intStack.push(55);
37+
38+
assertThat(intStack.pop()).isEqualTo(55);
39+
}
40+
41+
@Test
42+
@Order(4)
43+
void popElements() {
44+
intStack = LinkedStack.of(87, 53, 66);
45+
46+
intStack.pop();
47+
intStack.push(234);
48+
Integer lastElement = intStack.pop();
49+
50+
assertThat(lastElement).isEqualTo(234);
51+
}
52+
53+
@Test
54+
@Order(5)
55+
void size() {
56+
intStack = LinkedStack.of(87, 53, 66);
57+
58+
int actualSize = intStack.size();
59+
60+
assertThat(actualSize).isEqualTo(3);
61+
}
62+
63+
@Test
64+
@Order(6)
65+
void sizeOnEmptyStack() {
66+
int actualSize = intStack.size();
67+
68+
assertThat(actualSize).isEqualTo(0);
69+
}
70+
71+
@Test
72+
@Order(7)
73+
void isEmpty() {
74+
intStack = LinkedStack.of(87, 53, 66);
75+
76+
boolean stackEmpty = intStack.isEmpty();
77+
78+
assertThat(stackEmpty).isEqualTo(false);
79+
}
80+
81+
@Test
82+
@Order(8)
83+
void isEmptyOnEmptyStack() {
84+
boolean stackEmpty = intStack.isEmpty();
85+
86+
assertThat(stackEmpty).isEqualTo(true);
87+
}
88+
}

6-0-test-driven-development/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>TDD exercises
2+
The list of exercises dedicated to training your Test-driven development (TDD) skills
3+
4+
### No pain, No gain :heavy_exclamation_mark:
5+
6+
> Skill is only developed by hours and hours and hours of beating on your craft
7+
8+
Working on real problems, you're focused on finding a solution. Learning new things, you're trying to understand how it works.
9+
It is important to have a different type of activities, which purpose is improving your skill
10+
11+
***An exercise** is a predefined task that you continuously implement to improve a certain skill* :muscle:
12+
13+
##
14+
### The three laws of TDD
15+
1. You should not write production code until you have written a failing unit test
16+
2. You should not write more of a unit test than is sufficient to fail
17+
3. You should not write more production code than is sufficient to pass the failing test
18+
##
19+
20+
21+
* [Binary Search Tree]() relink
22+
* [Stack]() relink
23+
* [Linked List]() relink
24+
25+

6-0-test-driven-development/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-fundamentals-course</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>6-0-test-driven-development</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>11</maven.compiler.source>
16+
<maven.compiler.target>11</maven.compiler.target>
17+
</properties>
18+
19+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Stack exercise :muscle:
2+
Improve your TDD skill implementing Stack
3+
4+
### Task
5+
**Stack** is last in, first out (LIFO) collection of elements. Your job is to implement the interface `Stack`
6+
using TDD discipline
7+
8+
### Pre-conditions :heavy_exclamation_mark:
9+
You're supposed to know [The Three laws of TDD](https://github.com/bobocode-projects/java-fundamentals-course#the-three-laws-of-tdd) relink,
10+
be familiar with Stack data structure, and be able to write Java code
11+
12+
### How to start :question:
13+
* Just clone the repository and start implementing `Stack` interface following *three laws of TDD*
14+
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
15+
* 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
16+
17+
### Related materials :information_source:
18+
* [Як виробити звичку писати тести? (Bobocode channel <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=20/>)](https://youtu.be/L_CiX9C51BI)
19+
* [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type))
20+
* [The Three Laws of TDD](https://www.youtube.com/watch?v=qkblc5WRn-U&t=3476s)
21+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.bobocode.stack;
2+
3+
/**
4+
*
5+
* Stack is a data structure that follows "last in, first out" rule (LIFO).
6+
*/
7+
public interface Stack<T> {
8+
/**
9+
* Adds an element to the begining of the stack.
10+
*
11+
* @param element the element to add
12+
*/
13+
void push(T element);
14+
15+
/**
16+
* Retrieves and removes stack head.
17+
*
18+
* @return an element that was retrieved from the head or null if stack is empty
19+
*/
20+
T pop();
21+
22+
/**
23+
* Returns a size of the stack.
24+
*
25+
* @return an integer value that is a size of stack
26+
*/
27+
int size();
28+
29+
/**
30+
* Checks if the stack is empty.
31+
*
32+
* @return {@code true} if the stack is empty, returns {@code false} if it's not
33+
*/
34+
boolean isEmpty();
35+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.bobocode.stack;
2+
3+
public class StackTest {
4+
}

0 commit comments

Comments
 (0)