From 017b675790b7dcbf6683e2e0c77b9dd8e0cf0242 Mon Sep 17 00:00:00 2001 From: Ethan McCue Date: Sun, 22 Sep 2024 15:30:02 -0400 Subject: [PATCH] Add string challenges --- src/SUMMARY.md | 2 + src/enums/challenges.md | 107 +++++++++++++++++++++++++++ src/instance_methods/challenges.md | 70 +++++++++++++++++- src/strings_ii/challenges.md | 113 +++++++++++++++++++++++++++++ 4 files changed, 291 insertions(+), 1 deletion(-) create mode 100644 src/enums/challenges.md create mode 100644 src/strings_ii/challenges.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 75728a2..18660ff 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -251,6 +251,7 @@ - [Usage](./enums/usage.md) - [Equality](./enums/equality.md) - [Comparison to boolean](./enums/comparison_to_boolean.md) + - [Challenges](./enums/challenges.md) - [Strings II](./strings_ii.md) - [lowercase](./strings_ii/lowercase.md) - [UPPERCASE](./strings_ii/UPPERCASE.md) @@ -258,6 +259,7 @@ - [Check if empty](./strings_ii/check_if_empty.md) - [Check if blank](./strings_ii/check_if_blank.md) - [Strip extra whitespace](./strings_ii/strip_extra_whitespace.md) + - [Challenges](./strings_ii/challenges.md) # Control Flow III diff --git a/src/enums/challenges.md b/src/enums/challenges.md new file mode 100644 index 0000000..d42cc11 --- /dev/null +++ b/src/enums/challenges.md @@ -0,0 +1,107 @@ +# Challenges + +Remember the rules for this are + +- Try to use only the information given up to this point in this book. +- Try not to give up until you've given it a solid attempt + +## Challenge 1. + +Make an enum named `Response` which +has three variants. `YES`, `NO`, and `MAYBE_SO`. + +```java,editable +// ------------- +// CODE HERE +// ------------- + +void main() { + System.out.println( + Response.YES + ); + + System.out.println( + Response.NO + ); + + System.out.println( + Response.MAYBE_SO + ); +} +``` + +## Challenge 2. + +Write a method named `goodPerformer` which takes +in a `String` representing the name of an artist. + +If that `String` is equal to `Pitbull` or `Billy Joel` +return `YES`. If it is equal to `Shaggy` return `NO`. +Otherwise return `MAYBE_SO`. + +Use the enum you defined above. + +```java +// ------------ +// CODE HERE +// ------------ + +void main() { + Response pitbull = goodPerformer("Pitbull"); + System.out.println(pitbull); + + Response billyJoel = goodPerformer("Billy Joel"); + System.out.println(billyJoel); + + Response shaggy = goodPerformer("Shaggy"); + System.out.println(shaggy); + + Response chappelRoan = goodPerformer("Chappell Roan"); + System.out.println(chappelRoan); +} +``` + +## Challenge 3. + +Make a method named `transition` which takes in a `StopLight` +and returns the next light it will transition to. + +For those who don't drive cars: red lights go to green, +green lights go to yellow, and yellow lights go to red. + +```java,editable +enum StopLight { + RED, + YELLOW, + GREEN +} + +StopLight transition(StopLight current) { + // ------------ + // CODE HERE + // ------------ +} + +void main() { + var light = StopLight.RED; + System.out.println(light); + + light = transition(light); + System.out.println(light); + + light = transition(light); + System.out.println(light); + + light = transition(light); + System.out.println(light); + + light = transition(light); + System.out.println(light); + + light = transition(light); + System.out.println(light); + + light = transition(light); + System.out.println(light); +} +``` \ No newline at end of file diff --git a/src/instance_methods/challenges.md b/src/instance_methods/challenges.md index 547dde9..295f84c 100644 --- a/src/instance_methods/challenges.md +++ b/src/instance_methods/challenges.md @@ -116,7 +116,7 @@ field. Give it an instance method named `toCharArray` which gives a `char[]` that can be printed to display a rectangle of the given width and height. -```java +```java,editable // ------------ // CODE HERE // ------------ @@ -136,3 +136,71 @@ void main() { System.out.println(c); } ``` + +## Challenge 5. + +Update the definition for the `Taco` class so that it has a method named +`deluxe`. This should set the taco to have beef, sour cream, cheese, +and onion. Use the existing instance methods instead of directly accessing +fields. + +```java,editable +class Taco { + boolean beef; + boolean sourCream; + boolean cheese; + boolean onion; + + void addBeef() { + this.beef = true; + } + + void addSourCream() { + this.sourCream = true; + } + + void addCheese() { + this.cheese = true; + } + + void addOnion() { + this.onion = true; + } + + void deluxe() { + // ------------ + // CODE HERE + // ------------ + } +} + +void main() { + var taco = new Taco(); + taco.deluxe(); + + System.out.println("Has Beef: " + taco.beef); + System.out.println("Has Sour Cream: " + taco.sourCream); + System.out.println("Has Cheese: " + taco.cheese); + System.out.println("Has Onion: " + taco.onion); +} +``` + +## Challenge 6. + +Why doesn't this code function as you'd expect? Fix it by changing one line. + +```java +class Oscar { + boolean grouchy; + + void setGrouchy(boolean grouchy) { + grouchy = grouchy; + } +} + +void main() { + var oscar = new Oscar(); + oscar.setGrouchy(true); + System.out.println(oscar.grouchy); +} +``` \ No newline at end of file diff --git a/src/strings_ii/challenges.md b/src/strings_ii/challenges.md new file mode 100644 index 0000000..9cac1a8 --- /dev/null +++ b/src/strings_ii/challenges.md @@ -0,0 +1,113 @@ +# Challenges + +Remember the rules for this are + +- Try to use only the information given up to this point in this book. +- Try not to give up until you've given it a solid attempt + +## Challenge 1. + +Write a method named `isUpperCase` which tells you if a +given `String` is already made of all upper-case characters. + +Hint: One way to do this is to call `.toUpperCase` and check +if the result is the same as the input you were given. + +```java,editable +boolean isUpperCase(String s) { + // ----------- + // CODE HERE + // ----------- +} + +void main() { + // true + System.out.println(isUpperCase("ABC")); + // false + System.out.println(isUpperCase("abc")); + // false + System.out.println(isUpperCase("AbC")); +} +``` + +## Challenge 2. + +Do the same as above, but check if a given `String` is +made of all lower case letters. + +```java,editable +boolean isLowerCase(String s) { + // ----------- + // CODE HERE + // ----------- +} + +void main() { + // false + System.out.println(isLowerCase("ABC")); + // true + System.out.println(isLowerCase("abc")); + // false + System.out.println(isLowerCase("AbC")); +} +``` + +## Challenge 3. + +Add an instance method named `scream` to the `Muppet` class. This +should replace the name of the muppet with the name in upper case +letters + an exclamation point (`!`) at the end. + +```java,editable +class Muppet { + String name; + + // ------------- + // CODE HERE + // ------------- +} + +void main() { + var kermit = new Muppet(); + kermit.name = "kermit"; + + // kermit + System.out.println(kermit.name); + + // KERMIT! + kermit.scream(); + System.out.println(kermit.name); +} +``` + +## Challenge 4. + +Write a method called `echo`. + +If `echo` is given a non-blank `String`, it should print `You Said: <...>` +where `<...>` is the `String` they +gave minus any leading or trailing whitespace. + +If `echo` is given a `blank` `String`, it should print `You Didn't Say Anything`. + +```java,editable +void echo(String s) { + // ------------- + // CODE HERE + // ------------- +} + +void main() { + // You Said: Hello + echo("Hello"); + + // You Said: Hello + echo(" Hello "); + + // You Didn't Say Anything + echo(""); + + // You Didn't Say Anything + echo(" "); +} +``` \ No newline at end of file