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
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,14 @@

import com.bobocode.util.ExerciseNotCompletedException;

/**
* Welcome! This is an introduction exercise that will show you a simple example of Bobocode exercises.
* <p>
* JavaDoc is a way of communication with other devs. We use Java Docs in every exercise to define the task.
* So PLEASE MAKE SURE you read the Java Docs carefully.
* <p>
* Every exercise is covered with tests, see {@link ExerciseIntroductionTest}.
* <p>
* In this repo you'll find dozens of exercises covering various fundamental topics.
* They all have the same structure helping you to focus on practice and build strong skills!
*
* @author Taras Boychuk
*/
public class ExerciseIntroduction {
/**
* This method returns a very important message. If understood well, it can save you years of inefficient learning,
* and unlock your potential!
*
* @return "The key to efficient learning is practice!"
*/

public String getWelcomeMessage() {
// todo: implement a method and return a message according to javadoc
throw new ExerciseNotCompletedException();
return "The key to efficient learning is practice!";
}

/**
* Method encodeMessage accepts one {@link String} parameter and returns encoded {@link String}.
* <p>
* PLEASE NOTE THAT YOU WILL GET STUCK ON THIS METHOD INTENTIONALLY! ;)
* <p>
* Every exercise has a completed solution that is stored in the branch "completed". So in case you got stuck
* and don't know what to do, go check out completed solution.
*
* @param message input message
* @return encoded message
*/
public String encodeMessage(String message) {
// todo: switch to branch "completed" in order to see how it should be implemented
// можеш залишити поки що так
throw new ExerciseNotCompletedException();
}
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,2 @@
package com.bobocode.intro;

import lombok.SneakyThrows;
import org.junit.jupiter.api.*;

import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;

/**
* This is a {@link ExerciseIntroductionTest} that is meant to verify if you properly implement {@link ExerciseIntroduction}.
* It is a simple example that shows how each exercise is organized: todo section + tests.
* <p>
* A typical Java test uses JUnit framework to run the test, and may also use some other frameworks for assertions.
* In our exercises we use JUnit 5 + AssertJ
* <p>
* PLEASE NOTE:
* - annotation @{@link Order} is used to help you to understand which method should be implemented first.
* - annotation @{@link DisplayName} is used to provide you more detailed instructions.
*
* @author Taras Boychuk
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ExerciseIntroductionTest {
private ExerciseIntroduction exerciseIntroduction = new ExerciseIntroduction();
private String EXPECTED_MESSAGE = "The key to efficient learning is practice!";

@Test
@Order(1)
@DisplayName("getWelcomeMessage method returns correct phrase")
void getWelcomeMessage() {
String message = exerciseIntroduction.getWelcomeMessage();

assertThat(message).isEqualTo(EXPECTED_MESSAGE);
}

@Test
@Order(2)
@DisplayName("encodeMessage returns correct encoded message")
@SneakyThrows
void encodeMessageReturnsCorrectPhrase() {
var encodeMessageMethod = Arrays.stream(ExerciseIntroduction.class.getDeclaredMethods())
.filter(method -> method.getName().equals("encodeMessage"))
.findAny()
.orElseThrow();

var encodedMessage = encodeMessageMethod.invoke(new ExerciseIntroduction(), EXPECTED_MESSAGE);

assertThat(encodedMessage).isEqualTo("VGhlIGtleSB0byBlZmZpY2llbnQgbGVhcm5pbmcgaXMgcHJhY3RpY2Uh");
}
package com.bobocode.intro; import lombok.SneakyThrows; import org.junit.jupiter.api.*; import java.util.Arrays; import static org.assertj.core.api.Assertions.assertThat; /** * This is a {@link ExerciseIntroductionTest} that is meant to verify if you properly implement . * It is a simple example that shows how each exercise is organized: todo section + tests. * <p> * A typical Java test uses JUnit framework to run the test, and may also use some other frameworks for assertions. * In our exercises we use JUnit 5 + AssertJ * <p> * PLEASE NOTE: * - annotation @{@link Order} is used to help you to understand which method should be implemented first. * - annotation @{@link DisplayName} is used to provide you more detailed instructions. * * @author Taras Boychuk */ @TestMethodOrder(MethodOrderer.OrderAnnotation.class) class ExerciseIntroductionTest { private ExerciseIntroduction exerciseIntroduction = new ExerciseIntroduction(); private String EXPECTED_MESSAGE = "The key to efficient learning is practice!"; @Test @Order(1) @DisplayName("getWelcomeMessage method returns correct phrase") void getWelcomeMessage() { String message = exerciseIntroduction.getWelcomeMessage(); assertThat(message).isEqualTo(EXPECTED_MESSAGE); } @Test @Order(2) @DisplayName("encodeMessage returns correct encoded message") @SneakyThrows void encodeMessageReturnsCorrectPhrase() { var encodeMessageMethod = Arrays.stream(ExerciseIntroduction.class.getDeclaredMethods()) .filter(method -> method.getName().equals("encodeMessage")) .findAny() .orElseThrow(); var encodedMessage = encodeMessageMethod.invoke(new ExerciseIntroduction(), EXPECTED_MESSAGE); assertThat(encodedMessage).isEqualTo("VGhlIGtleSB0byBlZmZpY2llbnQgbGVhcm5pbmcgaXMgcHJhY3RpY2Uh"); } }
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package com.bobocode.basics;

/**
* {@link Box} is a container class that can store a value of any given type. Using Object as a field type
* is flexible, because we can store anything we want there. But it is not safe, because it requires runtime casting
* and there is no guarantee that we know the type of the stored value.
* <p>
* todo: refactor this class so it uses generic type "T" and run {@link com.bobocode.basics.BoxTest} to verify it
*/
public class Box {
private Object value;
public class Box<T> {
private T value;

public Box(Object value) {
public Box(T value) {
this.value = value;
}

public Object getValue() {
public T getValue() {
return value;
}

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

Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package com.bobocode.basics;

/**
* This demo demonstrates why using Object is not safe. It's not safe because runtime casting can cause runtime
* exceptions. We should always fail as soon as possible. So in this code we should not allow setting String
* value at compile time, if we expect to work with integers.
* <p>
* todo: refactor class {@link Box} to make type parameterization safe and make this demo fail at compile time
*/
public class BoxDemoApp {
public static void main(String[] args) {
Box intBox = new Box(123);
Box intBox2 = new Box(321);
System.out.println((int) intBox.getValue() + (int) intBox2.getValue());
Box<Integer> intBox = new Box<>(123);
Box<Integer> intBox2 = new Box<>(321);

System.out.println(intBox.getValue() + intBox2.getValue());

intBox.setValue(222);
intBox.setValue("abc"); // this should not be allowed
// the following code will compile, but will throw runtime exception
System.out.println((int) intBox.getValue() + (int) intBox2.getValue());
// intBox.setValue("abc"); // ❌ Тепер так не можна

System.out.println(intBox.getValue() + intBox2.getValue());
}
}
Loading