Skip to content

Commit

Permalink
feat: Implemented functions
Browse files Browse the repository at this point in the history
  • Loading branch information
CD-Jamira committed Mar 13, 2024
1 parent b54dfb1 commit c56508f
Showing 1 changed file with 79 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand All @@ -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;
}
}

/**
Expand All @@ -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";
}
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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) {
// The range is not valid so just return -1.
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;
}
}

0 comments on commit c56508f

Please sign in to comment.