Skip to content

Commit

Permalink
Add string challenges
Browse files Browse the repository at this point in the history
  • Loading branch information
bowbahdoe committed Sep 22, 2024
1 parent 679e02c commit 017b675
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,15 @@
- [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)
- [Equality ignoring case](./strings_ii/equality_ignoring_case.md)
- [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

Expand Down
107 changes: 107 additions & 0 deletions src/enums/challenges.md
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);
}
```
70 changes: 69 additions & 1 deletion src/instance_methods/challenges.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ------------
Expand All @@ -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);
}
```
113 changes: 113 additions & 0 deletions src/strings_ii/challenges.md
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(" ");
}
```

0 comments on commit 017b675

Please sign in to comment.