From ac384a66cefbc23f499f4d97a1ece334edc74d4e Mon Sep 17 00:00:00 2001 From: Ethan McCue Date: Sat, 21 Sep 2024 22:54:02 -0400 Subject: [PATCH] Add null challenges --- src/null/challenges.md | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/null/challenges.md b/src/null/challenges.md index 8f12955..a3ebda4 100644 --- a/src/null/challenges.md +++ b/src/null/challenges.md @@ -93,20 +93,33 @@ void main() { ## Challenge 5. -Without changing anything in the `main` method, make the `howBig` +Without changing anything in the `main` method, make the `bigness` method not throw a `NullPointerException` and still have the "correct" behavior for non-null inputs. ```java,editable -int howBig(String letters) { +String bigness(String letters) { int bigness = 0; for (int i = 0; i < letters.length(); i++) { bigness++; } - return bigness; + + if (bigness < 5) { + return "small"; + } + else if (bigness < 10) { + return "medium" + } + else { + return "big"; + } } void main() { + System.out.println( + bigness("bore") + ); + System.out.println( bigness("boiler") ); @@ -119,6 +132,10 @@ void main() { bigness("knower") ); + System.out.println( + bigness("chrysanthemum") + ); + System.out.println( bigness(null) );