Skip to content

Solution #1

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 54 additions & 4 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>23</maven.compiler.source>
<maven.compiler.target>23</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/org/example/ConditionalExercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ public class ConditionalExercises {
public boolean lessThanFive(int x) {
// Replace the line below with code that returns true if x is less than 5, and false otherwise
// (use an if statement with an else block.)
return false;
if (x<5) {
return true;
} else {
return false;
}
// return x < 5;
}

public String getAgeGroup(int age) {
// Replace the line below with code that returns "child" if age is less than 13,
// "teen" if age is less than 20, and "adult" otherwise
// (use if, else if, and else)
return "";
if (age >= 20) {
return "adult";
} else if (age >= 13) {
return "teen";
} else {
return "child";
}
}

public boolean isValidPassword(String password) {
Expand All @@ -20,6 +31,10 @@ public boolean isValidPassword(String password) {
// Replace the line below with code that returns true if password is at least 8 characters long
// and false otherwise
// (use an if statement with an else block.)
return false;
if (passwordLength < 8) {
return false;
} else {
return true;
}
}
}
33 changes: 31 additions & 2 deletions src/main/java/org/example/LoopExercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,42 @@ public class LoopExercises {
public int sum(int n) {
// Replace the line below with code that returns the sum of the numbers from 1 to n
// (use a for loop)
return 0;
int total = 0;
for (int i = 1; i <= n; i++) {
total += i;
}
return total;
}

public int sumUntilEven(int n) {
// Replace the line below with code that returns the sum of the numbers from 1 to n
// but stops adding when the sum is even
// (use a while loop with a sum variable and a counter variable)
return 0;
int sum = 0;
int i = 1;
while (i<=n) {
sum += i;
if (sum % 2 == 0) {
break;
}
i++;
}
return sum;
}

public int sumBackwardsUntilEven(int n) {
// Replace the line below with code that returns the sum of the numbers from 1 to n
// but stops adding when the sum is even
// (use a while loop with a sum variable and a counter variable)
int sum = 0;
int i = n;
while (i > 0) {
sum += i;
if (sum % 2 == 0) {
break;
}
i++;
}
return sum;
}
}
8 changes: 8 additions & 0 deletions src/test/java/LoopExercisesTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ public void testSumUntilEven() {
assert loopExercises.sumUntilEven(10) == 6;
assert loopExercises.sumUntilEven(100) == 6;
}

@Test
public void testSumBackwardsUntilEven() {
LoopExercises loopExercises = new LoopExercises();
assert loopExercises.sumBackwardsUntilEven(39) == 120;
assert loopExercises.sumBackwardsUntilEven(97) == 294;
assert loopExercises.sumBackwardsUntilEven(241) == 726;
}
}