Skip to content

Commit ce41fda

Browse files
committed
Bad project
1 parent b457d9a commit ce41fda

File tree

10 files changed

+274
-5
lines changed

10 files changed

+274
-5
lines changed

src/SUMMARY.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ Adjacency Matrix
8383
8484
BFS
8585
86+
Have a project that is just klarna but the your goal is "never write this program."
87+
88+
8689
-->
8790

8891
# Modern Java
@@ -456,9 +459,9 @@ BFS
456459
- [Unchecked Exceptions](./exceptions_ii/unchecked_exceptions.md)
457460
- [throws](./exceptions_ii/throws.md)
458461
- [Propagating Exceptions](./exceptions_ii/propagating_exceptions.md)
459-
- [Rethrowing Exceptions](./exceptions_ii/rethrowing_exceptions.md)
460462
- [Exception](./exceptions_ii/exception.md)
461463
- [RuntimeException](./exceptions_ii/runtime_exception.md)
464+
- [Rethrowing Exceptions](./exceptions_ii/rethrowing_exceptions.md)
462465
- [main](./exceptions_ii/main.md)
463466
- [Challenges](./exceptions_ii/challenges.md)
464467
- [Switch II](./switch_ii.md)
@@ -582,6 +585,8 @@ BFS
582585

583586
# Projects
584587

588+
- [Buy Now, Pay Later](./projects/buy_now_pay_later.md)
589+
- [Data Visualization](./projects/data_visualization.md)
585590
<!-- read and write some journal entries -->
586591
- [Journal Entries]()
587592

@@ -1066,6 +1071,8 @@ jshell
10661071
- [Maven]
10671072
- Dependencies
10681073
1074+
SDL, Vulkan, IMGUI - the C world
1075+
10691076
```xml
10701077
<?xml version="1.0" encoding="UTF-8"?>
10711078
<project xmlns="http://maven.apache.org/POM/4.0.0"

src/exceptions_ii/challenges.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,76 @@
11
# Challenges
2+
3+
Remember the rules for this are
4+
5+
- Try to use only the information given up to this point in this book.
6+
- Try not to give up until you've given it a solid attempt
7+
8+
## Challenge 1
9+
10+
Make a method named `parseIntChecked` that works the same as `Integer.parseInt`
11+
but throws a checked exception instead of an unchecked one.
12+
13+
```java,editable
14+
// CODE HERE
15+
16+
void main() throws Exception {
17+
int x = parseIntChecked("45");
18+
IO.println(x);
19+
20+
parseIntChecked("abc");
21+
}
22+
```
23+
24+
## Challenge 2
25+
26+
The following program will not compile. Make it compile by propagating the checked exception
27+
thrown by the `doesSomethingDangerous` method.
28+
29+
```java,no_run
30+
int doesSomethingDangerous(int value) {
31+
if (value == 1) {
32+
throw new Exception("1 does not work");
33+
}
34+
return 3 * value + 2;
35+
}
36+
37+
int compute(int start) {
38+
return square(doesSomethingDangerous(start));
39+
}
40+
41+
int square(int x) {
42+
return x * x;
43+
}
44+
45+
void main() {
46+
int x = Integer.parseInt(IO.readln("Give a starting number: "));
47+
IO.println(compute(x));
48+
}
49+
```
50+
51+
## Challenge 3
52+
53+
The following program is the same as the last one. Instead of propagating the exception, make it so that
54+
`compute` catches and rethrows the checked exception as an unchecked one.
55+
56+
```java,no_run
57+
int doesSomethingDangerous(int value) throws Exception {
58+
if (value == 1) {
59+
throw new Exception("1 does not work");
60+
}
61+
return 3 * value + 2;
62+
}
63+
64+
int compute(int start) {
65+
return square(doesSomethingDangerous(start));
66+
}
67+
68+
int square(int x) {
69+
return x * x;
70+
}
71+
72+
void main() {
73+
int x = Integer.parseInt(IO.readln("Give a starting number: "));
74+
IO.println(compute(x));
75+
}
76+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
11
# RuntimeException
2+
3+
`RuntimeException` is an unchecked exception.[^naming]
4+
5+
If you want a unchecked exception and do not know of a better one, `RuntimeException` will do.
6+
7+
```java,panics
8+
void main() {
9+
throw new RuntimeException("Crash!");
10+
}
11+
```
12+
13+
[^naming]: You may be wondering why the names "`Exception`" and "`RuntimeException`" instead of the
14+
probably easier to understand "`CheckedException`" and "`UncheckedException`." Java was made by humans
15+
and initially - as I understand it - "normal" exceptions were expected to be checked. It was only
16+
exceptions that came from "The Java Runtime" that would be unchecked. So that is where the name "`RuntimeException`" comes from. It just very quickly became a choice that was too late to change
17+
without breaking all the Java code in the world.

src/global_fields/challenges.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void main() {
3131

3232
## Challenge 2
3333

34-
Make a class named `DJKhaled`. If you ask DJ Khaleed to `produceMusic`
34+
Make a class named `DJKhaled`. If you ask DJ Khaled to `produceMusic`
3535
some lyrics should be printed out but also `anothaOne` should be called at least 7 times.[^drake]
3636

3737

src/projects/buy_now_pay_later.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Buy Now, Pay Later
2+
3+
## Problem Statement
4+
5+
People exchange money for goods and/or services.
6+
7+
Some of these purchases are non-essential. This means that they are,
8+
strictly speaking, optional. Ordering extra guacamole at a restraunt,
9+
buying a brand new pair of shoes, and buying a diamond ring are all
10+
examples of non-essential spending.
11+
12+
Other purchases are essential. If you are having a heart attack (within
13+
the United States) medical care is a mandatory expense. You either make that
14+
purchase or you die. If one does not have some minimum level of food, water, and shelter
15+
those are also essential expenses.
16+
17+
It comes down to needs and wants. If you need to have something that is an essential purchase.
18+
If you want to have something that is a non-essential purchase.
19+
20+
Many people do not have enough money for their essential expenses.
21+
This can be due to any number of reasons, but in the United States minimum wage has
22+
not kept up with overall inflation since 1968. Add to that rising costs of housing,
23+
transportation, education, etc. and people's financial
24+
25+
Some people have much more money than they could ever need. These people,
26+
sometimes pooling funds via corporations, often choose to loan this money
27+
to those that do not have enough for their expenses.
28+
29+
The reason they do this is because they can charge fees. If someone can't afford
30+
a particular expense of ~$100 they can get the money for that expense right away in exchange for agreeing
31+
to say a 2% interest rate. This would mean that the amount they owe goes up by 2% every month or so that they
32+
do not pay the loan issuer back.
33+
34+
Banks work this way. You deposit money in the bank and, while the bank and the government
35+
guarentee you that you can go at any time and get your money out, they are also turning around
36+
and lending that money to any number of businesses and individuals.
37+
38+
So long as you assume most people will be able to eventually pay off their debts being a loan issuer
39+
is a very profitable business to be in. It is also something of a public good.[^christmas] But if a large number of people are unable to pay off their debts
40+
you can get financial collapses. This can be at the level of an indidual loan issuer or entire countries.
41+
42+
Because it is bad when this happens governments generally have laws that restrict the "level of risk"
43+
loan issuers are allowed to take on. If they "take on too much risk" - meaning loan enough money
44+
to people who aren't able to pay it back - there will eventually be a collapse.
45+
46+
The level of scrutiny that applies to normal debts does not seem to, at least in the United States,
47+
apply when you do it via payment plans and call it "Buy Now, Pay Later." This allows for businesses
48+
to make loans they otherwise wouldn't be allowed to to people that might not otherwise qualify.
49+
50+
This certainly helps people who are struggling to get enough to affort their essential expenses.
51+
It can also help people make non-essential expenses which they deem essential. Human's are imperfect
52+
creatures. Many will take on a payment plan with an interest rate for a burrito.
53+
54+
In all cases the ability to take on debt can either help someone stabilize their financial situation
55+
or it can ultimately make things worse. The higher the interest rate the more likely the second situation
56+
comes to pass.
57+
58+
In the modern world computers are required to track both the issuance of these debts as
59+
well as compute interest and facilitate the collection process. This is in part because payments
60+
are often made electronically but also in part because the overhead of paying a person
61+
to manually keep track of all this info would eat in to the profits from interest and flat fees.
62+
63+
"Buy Now, Pay Later" programs also want to be embedded into online storefronts. People are much more likely
64+
to accept higher interest rates or take on the debt at all the closer the offer is made to "the point of sale." This requires not only business deals but a non-trivial amount of code.
65+
66+
## Your Goal
67+
68+
Never make a program for this.
69+
70+
"Buy Now, Pay Later" is another word for debt. If you make a program that
71+
enables desperate people go deeper and deeper into debt to enrich yourself
72+
you are a scumbag.
73+
74+
I put this "project" here to tell you that building software is not
75+
a neutral act. You need to critically consider how what you decide to spend
76+
your time on will affect the world around you.
77+
78+
Facebook is one of the largest websites in the world. It makes a lot of
79+
people a lot of money. The world would be a better place
80+
if it did not exist.
81+
82+
There are real companies - at the time of writing the biggest one is called [Klarna](https://klarna.com/) -
83+
which are more than happy to offer loans to the desperate and short-sighted. Their existence makes the
84+
world a worse place and they could not exist without the efforts of people who write software.
85+
86+
## Future Goals
87+
88+
None.
89+
90+
[^christmas]: Watch "It's A Wonderful Life" sometimes. Banking is not intrinsically evil. Loaning people
91+
money so they can buy a home to live in can be a great thing.

src/switch_ii/challenges.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,52 @@
11
# Challenges
2+
3+
Remember the rules for this are
4+
5+
- Try to use only the information given up to this point in this book.
6+
- Try not to give up until you've given it a solid attempt
7+
8+
## Challenge 1
9+
10+
Complete the switch expression such that it yields
11+
the appropriate value for each case.
12+
13+
In the surfer case, make sure to print out "Radical!" before yielding
14+
the value.
15+
16+
```java,editable
17+
enum Profession {
18+
FIREFIGHTER,
19+
PLUMBER,
20+
SURFER
21+
}
22+
23+
enum NaturalEnemy {
24+
FIRE,
25+
LEAKY_PIPES,
26+
BODACIOUS_WAVES
27+
}
28+
29+
NaturalEnemy enemy(Profession p) {
30+
switch (p) {
31+
case FIREFIGHTER -> {
32+
// CODE HERE
33+
};
34+
case PLUMBER -> {
35+
// CODE HERE
36+
}
37+
case SURFER -> {
38+
// CODE HERE
39+
}
40+
}
41+
}
42+
43+
void main() {
44+
IO.println(enemy(Profession.FIREFIGHTER));
45+
IO.println(enemy(Profession.PLUMBER));
46+
IO.println(enemy(Profession.SURFER));
47+
}
48+
```
49+
50+
## Challenge 2
51+
52+
Update your program above to omit `yield` in the two cases where it is not needed.

src/switch_ii/exhaustive_switches_with_enums.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/switch_ii/exhaustive_switches_with_strings.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/switch_ii/exhaustiveness.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,34 @@ boolean cool = switch (name) {
2828
IO.println(cool);
2929
~}
3030
```
31+
32+
Unlike in a switch statement, covering all the enum values in a switch expression
33+
is enough for that switch to be considered exhaustive. You do not need an
34+
extra `default` or `case null`[^reason].
35+
36+
```java,no_run
37+
enum Species {
38+
ALIEN,
39+
PREDATOR,
40+
HUMAN
41+
}
42+
43+
void main() {
44+
Species species = Species.valueOf(
45+
IO.readln("What do you see? ").toUpperCase()
46+
);
47+
String message = switch (species) {
48+
case ALIEN -> "Run";
49+
case PREDATOR -> "Fight Back";
50+
case HUMAN -> "RUN"
51+
};
52+
53+
IO.println(message);
54+
}
55+
```
56+
57+
[^reason]: The nitty gritty of why this is comes down to what Java will do if the code is
58+
run with an unexpected enum variant. With a switch statement the code will just move on and
59+
run none of the switch cases. With a switch expression Java will crash on an unexpected value.
60+
This difference is partially due to the fact that switch statements came first in the language
61+
and switch expressions came later. What a world, huh?

src/visibility/getter_and_setters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ class Person {
3838
}
3939
```
4040

41-
The answer to that is...annoying. We'll get to it, but the short story is that its a bit of a holdover from a very weird period in the 1990s.
41+
The answer to that is...annoying. We'll get to it, but the short story is that its a bit of a holdover from a very weird period in the early 2000s.
4242

4343
I mention it specifically so that you know that there isn't any important information you are missing and you are not crazy.

0 commit comments

Comments
 (0)