Skip to content
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
79 changes: 74 additions & 5 deletions src/main/java/LoopFun.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import java.util.Arrays;

public class LoopFun
{
Expand All @@ -10,7 +10,14 @@ public class LoopFun
* @return the factorial of the number
*/
public Integer factorial(Integer number){
return null;
Integer factorialResult = 1;
for (int i = 1; i <= number; i++) {
factorialResult = factorialResult * i;
}
return factorialResult;

//ternary operator would've worked too
//return (number ==1) || (number == 0) ? 1 : number * factorial(number -1);
}

/**
Expand All @@ -21,7 +28,23 @@ public Integer factorial(Integer number){
* @return Upper case string of the first letter of each word
*/
public String acronym(String phrase) {
return null;
StringBuilder acronym = new StringBuilder();
String input = phrase;
input = input.toUpperCase();
String[] phraseArray = input.split(" ");
for (int i = 0; i < phraseArray.length; i++) {
acronym.append(phraseArray[i].charAt(0));
}
String result = acronym.toString();
return result;

/*String [] sentence = phrase.split(" ");
String result = "";
for (String s : sentence) {
result = result + s.charAt(0);
}
return result.toUpperCase();
*/
}

/**
Expand All @@ -37,6 +60,52 @@ public String acronym(String phrase) {
* @return the encrypted string by shifting each character by three character
*/
public String encrypt(String word) {
return null;
//this code doesn't check for letters, and can encrypt special characters...
//Not the best implementation, but now the method only accepts strings composed of letters. How it re-enters input
//would have to be considered where the method is called.
for (int j = 0; j < word.length(); j++) {
if (!Character.isLetter(word.charAt(j))) {
return "Please only enter letters";
}
}
//The actual, used code. The numeric values of the letters weren't matching unicode... I've got to find java's numeric values.
char[] charArray = new char[word.length()];

for (int i = 0; i < word.length(); i++) {
int numberValueOfCurrentChar = java.lang.Character.getNumericValue(word.charAt(i));
if (numberValueOfCurrentChar >= 33) {
int encryptedValue = (word.charAt(i) - 23);
charArray[i] = (char) encryptedValue;
} else {
int encryptedValue = (word.charAt(i) + 3);
charArray[i] = (char) encryptedValue;
}
}
String encryptedWord = String.valueOf(charArray);
return encryptedWord;

//second attempt
// int[] valueOfChars = new int[word.length()];
// System.out.println(valueOfChars);
// for (int i = 0; i < word.length(); i++) {
// valueOfChars[i] = word.charAt(i) + 3;
// }
// System.out.println(valueOfChars);
// return null;

//first attempt -_-
// char[] lettersToChange = new char[word.length()];
// char currentLetter;
// for (int i = 0; i <word.length(); i++) {
// lettersToChange[i] = word.charAt(i);
// }
// for (int i = 0; i < word.length(); i++) {
// currentLetter = lettersToChange[i];
// currentLetter += 3;
// lettersToChange[i] = currentLetter;
// }
// String encryptedWord = lettersToChange.toString();
// return encryptedWord;
}
}
}

10 changes: 5 additions & 5 deletions src/main/java/MathUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class MathUtilities{
* @return the sum of the two numbers
*/
public Integer add(Integer baseValue, Integer valueToAdd){
return null;
return baseValue + valueToAdd;
}

/**
Expand All @@ -19,7 +19,7 @@ public Integer add(Integer baseValue, Integer valueToAdd){
* @return the sum of the two numbers
*/
public Double add(Double baseValue, Double valueToAdd){
return null;
return baseValue + valueToAdd;
}

/**
Expand All @@ -28,7 +28,7 @@ public Double add(Double baseValue, Double valueToAdd){
* @return the half of the number in double
*/
public Double half(Integer number) {
return null;
return (number / 2.0);
}

/**
Expand All @@ -37,7 +37,7 @@ public Double half(Integer number) {
* @return true if the number is odd, false if it is even
*/
public Boolean isOdd(Integer number){
return null;
return number % 2 != 0;
}


Expand All @@ -47,7 +47,7 @@ public Boolean isOdd(Integer number){
* @return the result of the number multiply by itself
*/
public Integer square(Integer number) {
return null;
return number * number;
}

}
45 changes: 39 additions & 6 deletions src/main/java/StringUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class StringUtilities {
* @return `input`
*/
public String returnInput(String input) {
return null;
return input;
}

/**
Expand All @@ -14,23 +14,38 @@ public String returnInput(String input) {
* @return the concatenation of `baseValue` and `valueToBeAdded`
*/
public String concatenate(String baseValue, String valueToBeAdded) {
return null;
String concatenatedString = baseValue + valueToBeAdded;
return concatenatedString;
}

/**
* @param valueToBeReversed value to be reversed
* @return identical string with characters in opposite order
*/
public String reverse(String valueToBeReversed) {
return null;
StringBuilder reverser = new StringBuilder();
for (int i = valueToBeReversed.length() -1; i >= 0; i--) {
reverser.append(valueToBeReversed.charAt(i));
}
String result = reverser.toString();
return result;

// return new StringBuilder(valueToBeReversed).reverse().toString(); // a one line solution.
}

/**
* @param word word to get middle character of
* @return middle character of `word`
*/
public Character getMiddleCharacter(String word) {
return null;
if (word.length() % 2 == 0) {
throw new IllegalArgumentException("Word must contain odd number of letters");
}
int characterIndexToRetrieve = word.length() / 2;
Character result = word.charAt(characterIndexToRetrieve);
return result;

//return word.charAt(word.length()/2); // one line solution that does the same thing.
}

/**
Expand All @@ -39,14 +54,32 @@ public Character getMiddleCharacter(String word) {
* @return `value` with char of value `charToRemove` removed
*/
public String removeCharacter(String value, Character charToRemove) {
return null;
return value.replaceAll(charToRemove.toString(), "");
}

/**
* @param sentence String delimited by spaces representative of a sentence
* @return last `word` in sentence
*/
public String getLastWord(String sentence) {
return null;

//code directly below would extract all non letters and keep sentence structure w/ spaces. That way our last 'word' doesn't include punctuation.
// String updatedSentence = "";
// for (int i = 0; i < sentence.length(); i++) {
// if (Character.isLetter(sentence.charAt(i)) || Character.isSpaceChar(sentence.charAt(i))) {
// updatedSentence = updatedSentence + sentence.charAt((i));
// }
// }
// System.out.println(updatedSentence);
//this works but I don't like it. It meets the test cases, but still includes punctuation. may update method & test cases.
String[] input = sentence.split(" ");
return input[input.length -1];

//or
//ArrayList<String> inputSentence = new ArrayList<>(sentence.split(""));
//return inputSentence.get(inputSentence.size()-1;

// or
//could have also used a variable to spot the last space, store that index position. then use that with input.substring();
}
}
13 changes: 13 additions & 0 deletions src/test/java/LoopFunEncryptTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@ public void testEncryptWithTheLastLetters() {
//Then
assertEquals(expected, actual);
}

// @Test
// public void testEncryptNoSpecialCharacters() {
// //Given
// String word = "quiz1!@#";
// String expected = "Please only enter letters";
//
// //When
// String actual = loop.encrypt(word);
//
// //Then
// assertEquals(expected, actual);
// }
}
4 changes: 4 additions & 0 deletions src/test/java/LoopsFunAcronymTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void test3() {
assertEquals(expected, actual);
}

@Test
public void test6() {
// given
String phrase = "Center Disease Control Whistle Blower";
Expand All @@ -63,6 +64,7 @@ public void test6() {
assertEquals(expected, actual);
}

@Test
public void test7() {
// given
String phrase = "Tuskegee Macon County, Alabama";
Expand All @@ -75,6 +77,7 @@ public void test7() {
assertEquals(expected, actual);
}

@Test
public void test8() {
// given
String phrase = "John Charles Cutler";
Expand All @@ -100,6 +103,7 @@ public void test9() {
assertEquals(expected, actual);
}

@Test
public void test4() {
// given
String phrase = "Tactical Reconnaissance Nuclear Powered Aerospace Platform";
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/StringUtilsGetLastWordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void test2(){
}

@Test
public void test4() {
public void test3() {
// Given
String sentence = "quantum information conservation";
String expected = "conservation";
Expand All @@ -53,7 +53,7 @@ public void test4() {
}

@Test
public void test5() {
public void test4() {
// Given
String sentence = "The universe yields geometries so elegant, they animate with intelligence.";
String expected = "intelligence.";
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/StringUtilsGetMiddleCharacterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,18 @@ public void test4(){
//Then
assertEquals(expected, actual);
}

//I don't think I did the exception correctly.
// @Test(expected = Exception.class)
// public void testStringWithEvenNoOfCharacters() {
// //Given
// String word = "pooppoop";
// String expected = "Word must contain odd number of letters";
//
// //When
// char actual = utilities.getMiddleCharacter(word);
// //Then
// assertEquals(expected, actual);
//
// }
}
24 changes: 24 additions & 0 deletions src/test/java/TestSuite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)

@Suite.SuiteClasses({
LoopFunEncryptTest.class,
LoopFunFactorialTest.class,
LoopsFunAcronymTest.class,
MathUtilitiesAddTest.class,
MathUtilitiesHalfTest.class,
MathUtilitiesIsOddTest.class,
MathUtilitiesSquareTest.class,
StringUtilsConcatenateTest.class,
StringUtilsGetLastWordTest.class,
StringUtilsGetMiddleCharacterTest.class,
StringUtilsRemoveCharacterTest.class,
StringUtilsReturnInputTest.class,
StringUtilsReverseTest.class,
})

public class TestSuite {
}