-
Notifications
You must be signed in to change notification settings - Fork 4
Lösung Jonas Render #2
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
base: main
Are you sure you want to change the base?
Conversation
| @Test | ||
| @DisplayName("Test 1 MadLib") | ||
| void createSentence () throws Exception { | ||
| MadLib sentence = new MadLib(); | ||
|
|
||
| String s = sentence.create(); | ||
|
|
||
| if (s != null && s.startsWith("Java programming is so")) { | ||
| } else { | ||
| throw new Exception("Sentence is null or not starting correctly."); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bei Unit-Tests brauchen wir immer mind. ein assert-Statement, damit es ein "echter" Unit Test ist, also z. B. sowas hier:
@DisplayName("should return a random sentence about java")
void test1() {
// when
var result = underTest.create();
// then
assertNotNull(result);
assertTrue(result.startsWith("Java programming is so"));
}
@Test
@DisplayName("should contain the random adjectives, verbs and famous persons")
void test2() {
// when
var result = underTest.create();
// then
assertTrue(contains(result, MadLib.ADJECTIVES));
assertTrue(contains(result, MadLib.VERBS));
assertTrue(contains(result, MadLib.FAMOUS_PERSONS));
}
private boolean contains(String value, String[] wordList) {
for (String word : wordList) {
if (value.contains(word)) {
return true;
}
}
return false;
}| public static String[] getADJECTIVES() { | ||
| return ADJECTIVES; | ||
| } | ||
|
|
||
| public static String[] getVERBS() { | ||
| return VERBS; | ||
| } | ||
|
|
||
| return ""; // TODO: this is only here so that the code can be compiled, please replace it with your result | ||
| public static String[] getFamousPersons() { | ||
| return FAMOUS_PERSONS; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Die getter-Methoden auf die static Arrays sind nicht zwingend notwendig. Du nutzt sie z. B. auch gar nicht.
Man sollte immer nach dem YAGNI (You ain't gonna need id) Konzept programmieren. Wenn du etwas nicht nutzt, dann programmiere es nicht.
| public String create() { | ||
| // TODO: implement this method | ||
| Random randomNumber = new Random(); | ||
|
|
||
| int randomAdjective = randomNumber.nextInt(ADJECTIVES.length); | ||
| int randomVerb = randomNumber.nextInt(VERBS.length); | ||
| int otherRandomVerb = randomNumber.nextInt(VERBS.length); | ||
| int randomFamousPerson = randomNumber.nextInt(VERBS.length); | ||
|
|
||
| return "Java programming is so " + ADJECTIVES[randomAdjective] + "! It makes me so excited all the time because I love to " + VERBS[randomVerb] + ". Stay hydrated and " + VERBS[otherRandomVerb] +" like you are " + FAMOUS_PERSONS[randomFamousPerson]+"!"; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Das passt soweit. Das hier ist die Musterlösung:
public String create() {
String randomAdjective = randomChoice(ADJECTIVES);
String randomVerb1 = randomChoice(VERBS);
String randomVerb2 = randomChoice(VERBS);
String randomFamousPerson = randomChoice(FAMOUS_PERSONS);
return String.format(
"Java programming is so %s! It makes me so excited all the time because I love to %s. Stay hydrated and %s like you are %s!",
randomAdjective,
randomVerb1,
randomVerb2,
randomFamousPerson
);
}
private String randomChoice(String[] wordList) {
return wordList[random.nextInt(wordList.length)];
}Mit String.format() kann man ein String ohne die ganzen " + " zusammenbauen.
Hi Daniel,
kannst du dir meine Lösung mal anschauen und mir dazu Feedback geben? Danke!