diff --git a/.idea/misc.xml b/.idea/misc.xml
index e122dea..fdc35ea 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -8,7 +8,7 @@
-
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 3afe1c1..e75cd67 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,7 +4,9 @@
-
+
+
+
@@ -20,6 +22,18 @@
+ {
+ "lastFilter": {
+ "state": "OPEN",
+ "assignee": "Carolista"
+ }
+}
+ {
+ "selectedUrlAndAccountId": {
+ "url": "https://github.com/Carolista/software-dev-course-java-basics-3-demo.git",
+ "accountId": "cccb2ce8-290b-4d89-b99e-7386737c7e96"
+ }
+}{
"associatedIndex": 2
}
@@ -35,15 +49,18 @@
"keyToString": {
"Application.Main.executor": "Run",
"JUnit.ConditionalExercisesTests.executor": "Run",
+ "JUnit.LoopExercisesTests.testSum.executor": "Run",
+ "JUnit.LoopExercisesTests.testSumBackwardsUntilEven.executor": "Run",
+ "JUnit.LoopExercisesTests.testSumUntilEven.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.git.unshallow": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
- "git-widget-placeholder": "main",
+ "git-widget-placeholder": "solution",
"kotlin-language-version-configured": "true",
"last_opened_file_path": "/home/mikel/projects/java-new-curriculum/software-dev-course-classes-and-objects-2/pom.xml",
"project.structure.last.edited": "Project",
- "project.structure.proportion": "0.0",
- "project.structure.side.proportion": "0.0"
+ "project.structure.proportion": "0.15",
+ "project.structure.side.proportion": "0.2"
}
}]]>
@@ -56,8 +73,41 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/pom.xml b/pom.xml
index 4bc6aa6..87f401b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -9,8 +9,8 @@
1.0-SNAPSHOT
- 23
- 23
+ 21
+ 21UTF-8
diff --git a/src/main/java/org/example/ConditionalExercises.java b/src/main/java/org/example/ConditionalExercises.java
index ba577bd..af7883d 100644
--- a/src/main/java/org/example/ConditionalExercises.java
+++ b/src/main/java/org/example/ConditionalExercises.java
@@ -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) {
@@ -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;
+ }
}
}
diff --git a/src/main/java/org/example/LoopExercises.java b/src/main/java/org/example/LoopExercises.java
index ff1aee6..fc119c5 100644
--- a/src/main/java/org/example/LoopExercises.java
+++ b/src/main/java/org/example/LoopExercises.java
@@ -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;
}
}
diff --git a/src/test/java/LoopExercisesTests.java b/src/test/java/LoopExercisesTests.java
index ca8f332..84a7fcd 100644
--- a/src/test/java/LoopExercisesTests.java
+++ b/src/test/java/LoopExercisesTests.java
@@ -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;
+ }
}