Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Виконані завдання з java-fundamentals-exercises #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SashaSaito wants to merge 4 commits into bobocode-projects:main from SashaSaito:main
Closed
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
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.bobocode.util.ExerciseNotCompletedException;

import java.util.Base64;

/**
* Welcome! This is an introduction exercise that will show you a simple example of Bobocode exercises.
* <p>
Expand All @@ -23,8 +25,7 @@ public class ExerciseIntroduction {
* @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!";
}

/**
Expand All @@ -39,7 +40,7 @@ public String getWelcomeMessage() {
* @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();
return Base64.getEncoder().encodeToString(message.getBytes());
}
}

1 change: 1 addition & 0 deletions 0-0-intro/src/main/java/com/bobocode/intro/main.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = input()
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.SneakyThrows;
import org.junit.jupiter.api.*;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -22,8 +23,8 @@
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ExerciseIntroductionTest {
private ExerciseIntroduction exerciseIntroduction = new ExerciseIntroduction();
private String EXPECTED_MESSAGE = "The key to efficient learning is practice!";
private final ExerciseIntroduction exerciseIntroduction = new ExerciseIntroduction();
private final String EXPECTED_MESSAGE = "The key to efficient learning is practice!";

@Test
@Order(1)
Expand All @@ -38,7 +39,7 @@ void getWelcomeMessage() {
@Order(2)
@DisplayName("encodeMessage returns correct encoded message")
@SneakyThrows
void encodeMessageReturnsCorrectPhrase() {
void encodeMessageReturnsCorrectPhrase() throws InvocationTargetException, IllegalAccessException {
var encodeMessageMethod = Arrays.stream(ExerciseIntroduction.class.getDeclaredMethods())
.filter(method -> method.getName().equals("encodeMessage"))
.findAny()
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
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;
import lombok.Getter;
import lombok.Setter;

public Box(Object value) {
this.value = value;
}
@Setter
@Getter
public class Box<T> {
private T value;

public Object getValue() {
return value;
}

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

}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
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);
Box<Integer> intBox = new Box<>(123);
Box<Integer> intBox2 = new Box<>(321);
System.out.println((int) intBox.getValue() + (int) 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());
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,92 +3,47 @@
import com.bobocode.basics.util.BaseEntity;
import com.bobocode.util.ExerciseNotCompletedException;
import lombok.Data;
import lombok.Getter;

import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
* {@link CrazyGenerics} is an exercise class. It consists of classes, interfaces and methods that should be updated
* using generics.
* <p>
* TODO: go step by step from top to bottom. Read the java doc, write code and run CrazyGenericsTest to verify your impl
* <p>
* Hint: in some cases you will need to refactor the code, like replace {@link Object} with a generic type. In order
* cases you will need to add new fields, create new classes, or add new methods. Always try to read java doc and update
* the code according to it.
* <p><p>
* <strong>TODO: to get the most out of your learning, <a href="https://www.bobocode.com">visit our website</a></strong>
* <p>
*
* @author Taras Boychuk
*/
public class CrazyGenerics {
/**
* {@link Sourced} is a container class that allows storing any object along with the source of that data.
* The value type can be specified by a type parameter "T".
*
* @param <T> – value type
*/

@Data
public static class Sourced { // todo: refactor class to introduce type parameter and make value generic
private Object value;
public static class Sourced<T> {
private T value;
private String source;
}

/**
* {@link Limited} is a container class that allows storing an actual value along with possible min and max values.
* It is special form of triple. All three values have a generic type that should be a subclass of {@link Number}.
*
* @param <T> – actual, min and max type
*/
@Data
public static class Limited {
// todo: refactor class to introduce type param bounded by number and make fields generic numbers
private final Object actual;
private final Object min;
private final Object max;
public static class Limited<T extends Number> {
private final T actual;
private final T min;
private final T max;
}

/**
* {@link Converter} interface declares a typical contract of a converter. It works with two independent generic types.
* It defines a convert method which accepts one parameter of one type and returns a converted result of another type.
*
* @param <T> – source object type
* @param <R> - converted result type
*/
public interface Converter { // todo: introduce type parameters
// todo: add convert method
public interface Converter {
R convert(T obj);
}

/**
* {@link MaxHolder} is a container class that keeps track of the maximum value only. It works with comparable objects
* and allows you to put new values. Every time you put a value, it is stored only if the new value is greater
* than the current max.
*
* @param <T> – value type
*/
public static class MaxHolder { // todo: refactor class to make it generic
private Object max;
@Getter
public static class MaxHolder <T extends Comparable<? super T>> {
private T max;

public MaxHolder(Object max) {
public MaxHolder(T max) {
this.max = max;
}

/**
* Puts a new value to the holder. A new value is stored to the max, only if it is greater than current max value.
*
* @param val a new value
*/
public void put(Object val) {
throw new ExerciseNotCompletedException(); // todo: update parameter and implement the method
public void put(T val) {
if (val.compareTo(max) > 0) {
max = val;
}
}

public Object getMax() {
return max;
}
}

/**
Expand All @@ -97,8 +52,8 @@ public Object getMax() {
*
* @param <T> – the type of objects that can be processed
*/
interface StrictProcessor { // todo: make it generic
void process(Object obj);
interface StrictProcessor<T> { // todo: make it generic
void process(T obj);
}

/**
Expand Down

AltStyle によって変換されたページ (->オリジナル) /