From 7dec539eb65c733111451fe9c227a5642b90ab4e Mon Sep 17 00:00:00 2001 From: SherllinT Date: Wed, 13 Mar 2024 15:59:10 +0000 Subject: [PATCH 1/2] feat: Lesson6 Test --- .../com/codedifferently/lesson6/Lesson6.java | 97 +++++++++++++++---- 1 file changed, 79 insertions(+), 18 deletions(-) diff --git a/lesson_06/conditionals/conditionals_app/src/main/java/com/codedifferently/lesson6/Lesson6.java b/lesson_06/conditionals/conditionals_app/src/main/java/com/codedifferently/lesson6/Lesson6.java index 80a1d7b9..3878e49f 100644 --- a/lesson_06/conditionals/conditionals_app/src/main/java/com/codedifferently/lesson6/Lesson6.java +++ b/lesson_06/conditionals/conditionals_app/src/main/java/com/codedifferently/lesson6/Lesson6.java @@ -21,7 +21,11 @@ public static void main(String[] args) { * @return True if the age corresponds to a voting age and false otherwise. */ public static boolean canVote(int age) { - return false; + if (age >= 18) { + return true; + } else { + return false; + } } /** @@ -38,7 +42,13 @@ public static int compareStrings(String a, String b) { // TODO(you): Finish this method. - return 0; + if (distance < 0) { + return -1; + } else if (distance > 0) { + return 1; + } else { + return 0; + } } /** @@ -51,7 +61,29 @@ public static int compareStrings(String a, String b) { * @return The letter grade ("A+", "A", "A-", "B+", etc.). */ public static String convertGpaToLetterGrade(double gpa) { - return "F"; + if (gpa >= 4.0) { + return "A"; + } else if (gpa >= 3.7) { + return "A-"; + } else if (gpa >= 3.3) { + return "B+"; + } else if (gpa >= 3.0) { + return "B"; + } else if (gpa >= 2.7) { + return "B-"; + } else if (gpa >= 2.3) { + return "C+"; + } else if (gpa >= 2.0) { + return "C"; + } else if (gpa >= 1.7) { + return "C-"; + } else if (gpa >= 1.3) { + return "D+"; + } else if (gpa >= 1.0) { + return "D"; + } else { + return "F"; + } } /** @@ -61,7 +93,15 @@ public static String convertGpaToLetterGrade(double gpa) { * @return The factorial of n. */ public static int computeFactorial(int n) { - return 0; + if (n < 0) { + throw new IllegalArgumentException("Input cannot be negative"); + } + + int factorial = 1; + for (int i = 1; i <= n; i++) { + factorial *= i; + } + return factorial; } /** @@ -71,7 +111,12 @@ public static int computeFactorial(int n) { * @return The sum of all the values. */ public static double addNumbers(double[] values) { - return 0; + + double sum = 0; + for (int i = 0; i < values.length; i++) { + sum += values[i]; + } + return sum; } /** @@ -81,7 +126,25 @@ public static double addNumbers(double[] values) { * @return An array containing the first `n` fibonacci values. */ public static int[] getFirstNFibonacciNumbers(int n) { - return null; + if (n < 0) { + return null; + } + + int[] fibonacci = new int[n]; + + if (n >= 1) { + fibonacci[0] = 1; + } + + if (n >= 2) { + fibonacci[1] = 1; + } + + for (int i = 2; i < n; i++) { + fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]; + } + + return fibonacci; } /** @@ -94,19 +157,17 @@ public static int[] getFirstNFibonacciNumbers(int n) { * @return The index of the value if found in the array and -1 otherwise. */ public static int binarySearch(int[] values, int start, int end, int value) { - if (end < start) { - // - return -1; - } - - int pivotIndex = (start + end) / 2; // The index in the middle of the array. + while (start <= end) { + int pivotIndex = start + (end - start) / 2; - // TODO(you): Finish implementing this algorithm - - // If values[pivotIndex] is equal to value then return `pivotIndex`. - // Else if values[pivotIndex] is greater than the value, then - // call `binarySearch(values, start, pivotIndex - 1, value)` and return its value; - // Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value. + if (values[pivotIndex] == value) { + return pivotIndex; + } else if (values[pivotIndex] < value) { + start = pivotIndex + 1; + } else { + end = pivotIndex - 1; + } + } return -1; } } From da5b90cd74bb6b21de1139f048386874416ad14e Mon Sep 17 00:00:00 2001 From: SherllinT Date: Wed, 13 Mar 2024 16:44:14 +0000 Subject: [PATCH 2/2] feat: made updates on Lesson6 --- .../java/com/codedifferently/lesson6/Lesson6.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lesson_06/conditionals/conditionals_app/src/main/java/com/codedifferently/lesson6/Lesson6.java b/lesson_06/conditionals/conditionals_app/src/main/java/com/codedifferently/lesson6/Lesson6.java index 3c58e115..cdd70df2 100644 --- a/lesson_06/conditionals/conditionals_app/src/main/java/com/codedifferently/lesson6/Lesson6.java +++ b/lesson_06/conditionals/conditionals_app/src/main/java/com/codedifferently/lesson6/Lesson6.java @@ -166,14 +166,12 @@ public static int binarySearch(int[] values, int start, int end, int value) { // TODO(you): Finish implementing this algorithm - if (values[pivotIndex] == value) { - return pivotIndex; - } else if (values[pivotIndex] < value) { - start = pivotIndex + 1; - } else { - end = pivotIndex - 1; - } + if (values[pivotIndex] == value) { + return pivotIndex; + } else if (values[pivotIndex] > value) { + return binarySearch(values, start, pivotIndex - 1, value); + } else { + return binarySearch(values, pivotIndex + 1, end, value); } - return -1; } }