Skip to content

Commit

Permalink
Add String challenges from other PR + flags for turning on and off `v…
Browse files Browse the repository at this point in the history
…oid main` (Together-Java#18)

* Update division.md

* Add basic feature flags impl

* Integrate String challenges

Fix script to not make an out.json

Co-Authored-By: alphaBEE <[email protected]>

* Remove "float point"

* Fix mdlint complaints

---------

Co-authored-by: alphaBEE <[email protected]>
  • Loading branch information
bowbahdoe and ankitsmt211 authored Mar 15, 2023
1 parent 07635dd commit 3232fb2
Show file tree
Hide file tree
Showing 37 changed files with 1,086 additions and 55 deletions.
11 changes: 10 additions & 1 deletion book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@ multilingual = false
[output.html]
git-repository-url = "https://github.com/Together-Java/ModernJava"
edit-url-template = "https://github.com/Together-Java/ModernJava/edit/develop/{path}"
mathjax-support = true
mathjax-support = true

[preprocessor.features]
command = "python3 features.py"
# Not ready
toplevel_anonymous_class = true
# Not ready
simple_io = true
# Turn on when Java 21 released
java_21 = false
49 changes: 49 additions & 0 deletions features.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3

import json
import sys

def preprocess_content(context, content):
options = context.get("config", {}).get("preprocessor", {}).get("features", {})
newContent = []
skipping = False
for line in content.splitlines():
if skipping:
if line.strip() == "~ENDIF" or line.strip() == "~ ENDIF":
skipping = False
elif line.strip() == "~ELSE" or line.strip() == "~ ELSE":
skipping = False
elif line.strip().startswith("~IF ") or line.strip().startswith("~ IF "):
if not options.get(line.split(" ", 2)[1].strip(), False):
skipping = True
elif line.strip() == "~ENDIF" or line.strip() == "~ ENDIF":
continue
elif line.strip() == "~ELSE" or line.strip() == "~ ELSE":
skipping = True
continue
else:
if options.get("simple_io", False) and options.get("toplevel_anonymous_class", False):
newContent.append(line.replace("System.out.println", "println").replace("System.out.print", "print"))
else:
newContent.append(line)

return "\n".join(newContent)

def preprocess_section(context, section):
if "Chapter" in section:
section["Chapter"]["content"] = \
preprocess_content(context, section["Chapter"]["content"])
for sub_section in section["Chapter"].get("sub_items", []):
preprocess_section(context, sub_section)

if __name__ == '__main__':
if len(sys.argv) > 1:
if sys.argv[1] == "supports":
sys.exit(0)

context, book = json.load(sys.stdin)

for section in book["sections"]:
preprocess_section(context, section)

print(json.dumps(book))
5 changes: 5 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# Modern Java

- [Prelude](./prelude.md)
- [Asking for Help](./prelude/asking_for_help.md)
- [Toy Problems](./prelude/toy_problems.md)
- [First Steps](./first_steps.md)
- [Comments](./first_steps/comments.md)
Expand Down Expand Up @@ -88,6 +89,8 @@
- [Multiline String Literals](./strings/multiline.md)
- [Concatenation](./strings/concatenation.md)
- [Equality](./strings/equality.md)
- [Length](./strings/length.md)
- [Access Individual Characters](./strings/access_individual_characters.md)
- [Challenges](./strings/challenges.md)

# Control Flow I
Expand All @@ -98,12 +101,14 @@
- [Else If](./branching_logic/else_if.md)
- [Relation to Delayed Assignment](./branching_logic/relation_to_delayed_assignment.md)
- [Ternary Expression](./branching_logic/ternary_expression.md)
- [Challenges](./branching_logic/challenges.md)
- [Loops](./loops.md)
- [While](./loops/while.md)
- [Termination Conditions](./loops/termination_conditions.md)
- [Break](./loops/break.md)
- [Continue](./loops/continue.md)
- [Do](./loops/do.md)
- [Challenges](./loops/challenges.md)

# User Defined Types

Expand Down
56 changes: 56 additions & 0 deletions src/boolean/challenges.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ Remember the rules for this are

What will this program output when run? Write down your guess and then try running it.

~IF toplevel_anonymous_class

```java
void main() {
boolean a = true;
boolean b = false;
boolean c = true;
boolean d = false;

boolean result = a || b && c || !d;

System.out.println(result);
}
```

~ELSE

```java
public class Main {
public static void main(String[] args) {
Expand All @@ -24,10 +41,29 @@ public class Main {
}
```

~ENDIF

## Challenge 2

What will this program output when run? Write down your guess and then try running it.

~IF toplevel_anonymous_class

```java
void main() {
boolean a = true;
boolean b = false;
boolean c = true;
boolean d = false;

boolean result = !(a || b && c || !d) || (a && b || c);

System.out.println(result);
}
```

~ELSE

```java
public class Main {
public static void main(String[] args) {
Expand All @@ -43,10 +79,28 @@ public class Main {
}
```

~ENDIF

## Challenge 3

Say you have two boolean variables, how could you use the operators we've covered to get the "exclusive or" of the two.

~IF toplevel_anonymous_class

```java
void main() {
// Change these two variables to test your solution
boolean hasIceCream = true;
boolean hasCookie = false;

boolean validChoice = < YOUR CODE HERE >;

System.out.println(validChoice);
}
```

~ELSE

```java
public class Main {
public static void main(String[] args) {
Expand All @@ -61,6 +115,8 @@ public class Main {
}
```

~ENDIF

Make sure to test all the possibilities.

| hasIceCream | hasCookie | validChoice |
Expand Down
1 change: 1 addition & 0 deletions src/branching_logic/challenges.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Challenges
30 changes: 30 additions & 0 deletions src/characters/challenges.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ A lot of math problems ask you to find \\( x^2 \\). What is the value of the cha

Try to work it out on paper before running the program below.

~IF toplevel_anonymous_class

```java
void main() {
char x = 'x';

System.out.println(x * x);
}
```

~ELSE

```java
public class Main {
public static void main(String[] args) {
Expand All @@ -21,12 +33,28 @@ public class Main {
}
```

~ENDIF

## Challenge 2

Alter the program below so that it will output `true` if the character declared at the top is a letter.

Make use of the fact that the numeric values for `a` - `z` and `A` - `Z` are contiguous.

~IF toplevel_anonymous_class

```java
void main() {
char c = 'a';

boolean isLetter = ???;

System.out.println(isLetter);
}
```

~ELSE

```java
public class Main {
public static void main(String[] args) {
Expand All @@ -39,6 +67,8 @@ public class Main {
}
```

~ENDIF

## Challenge 3

How many UTF-16 code units does it take to represent this emoji? `👨‍🍳`.
47 changes: 45 additions & 2 deletions src/first_steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

If you made it through the [Getting Started section](./getting_started/hello_world.md) you've successfully run this program.

~IF toplevel_anonymous_class

```java
void main() {
System.out.println("Hello, World!");
}
```

~ELSE

```java
public class Main {
public static void main(String[] args) {
Expand All @@ -10,11 +20,26 @@ public class Main {
}
```

~ENDIF

This "prints" - not in the sense of a physical printer, but like "displays on the screen" -
the text `"Hello, World!"`.

Its a tradition for this to be your first program in
any language. Unfortunately, for reasons that are impossible to explain with the context you have at this point,
Its a tradition for this to be your first program in any language.
~IF toplevel_anonymous_class

We aren't quite at the point where I can explain what `void main()` means, but
all you need to know for now is that it is what Java looks for to know where to start the program.

```java
void main() {
< WRITE YOUR CODE HERE >
}
```

~ELSE

Unfortunately, for reasons that are impossible to explain with the context you have at this point,
half of this probably reads as cryptic nonsense.

```java
Expand All @@ -33,6 +58,8 @@ public class Main {
}
```
~ENDIF
So for all intents and purposes, this is the whole program.
```java
Expand All @@ -51,6 +78,20 @@ System.out.print("World");
System.out.println("!");
```
~IF toplevel_anonymous_class
Which, when we add back `void main()`, looks like this.
```java
void main() {
System.out.print("Hello, ");
System.out.print("World");
System.out.println("!");
}
```
~ELSE
Which, when we add back the part you are squinting past, looks like this.
```java
Expand All @@ -63,6 +104,8 @@ public class Main {
}
```
~ENDIF
You should get in the habit of, whenever you see some bit of code, trying to physically type it out, run it,
tweak it, and play with it.
Expand Down
34 changes: 34 additions & 0 deletions src/first_steps/challenges.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ Jasmine

What will this program output when run? Write down your guess and then try actually running it.

~IF toplevel_anonymous_class

```text
void main() {
System.out.println("A");
//System.out.println("B");
System.out.println("C");//
System.out.println("D");
/*
System.out.println("E");
System.out.println("F");*/
System.out.println("G");
}
```

~ELSE

```text
public class Main {
public static void main(String[] args) {
Expand All @@ -36,12 +53,27 @@ public class Main {
}
```

~ENDIF

## Challenge 3

There are four semicolons in this perfectly functional program. Delete one of them at random and see what the errors you get look like.

How could you use that error to figure out where you might have forgotten to put a semicolon?

~IF toplevel_anonymous_class

```java
void main() {
System.out.println("Apple");
System.out.println("Banana");
System.out.println("Clementine");
System.out.println("Durian");
}
```

~ELSE

```java
public class Main {
public static void main(String[] args) {
Expand All @@ -52,3 +84,5 @@ public class Main {
}
}
```

~ENDIF
Loading

0 comments on commit 3232fb2

Please sign in to comment.