forked from Together-Java/ModernJava
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
291 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(" "); | ||
} | ||
``` |