Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update AboutConditionals.java #80

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions koans/src/beginner/AboutConditionals.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,36 @@ public void switchStatementSwitchValues() {

@Koan
public void shortCircuit() {
int i = 1;
int a = 6;
// Why did we use a variable here?
// What happens if you replace 'a' with '6' below?
if ((a < 9) || (++i < 8)) i = i + 1;
assertEquals(i, __);
counter trueCount = new Counter(true);
counter falseCount = new Counter(false);
String x = "Hai";
if (trueCount.count() || falseCount.count()) {
x = "kthxbai";
}
assertEquals(x, __);
assertEquals(trueCount.count, __);
assertEquals(falseCount.count, __);
}

@Koan
public void bitwise() {
counter trueCount = new Counter(true);
counter falseCount = new Counter(false);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the Counter class contain a suitable constructor to initialize the returnValue attribute?

public Counter(boolean b) { this.returnValue = b; }

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, probably shouldn't edit java files on my phone 🥇

String x = "Hai";
if (trueCount.count() | falseCount.count()) {
x = "kthxbai";
}
assertEquals(x, __);
assertEquals(trueCount.count, __);
assertEquals(falseCount.count, __);
}

class Counter {
boolean returnValue;
int count = 0;
boolean count() {
count++;
return returnValue;
}
}
}