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 16112932..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 @@ -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; } /** @@ -95,7 +158,7 @@ public static int[] getFirstNFibonacciNumbers(int n) { */ public static int binarySearch(int[] values, int start, int end, int value) { if (end < start) { - // The range is not valid so just return -1. + // return -1; } @@ -103,10 +166,12 @@ public static int binarySearch(int[] values, int start, int end, int value) { // 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. - return -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); + } } }