diff --git a/Answers/40230112119/.idea/.gitignore b/Answers/40230112119/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Answers/40230112119/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Answers/40230112119/.idea/compiler.xml b/Answers/40230112119/.idea/compiler.xml new file mode 100644 index 0000000..69cd7fc --- /dev/null +++ b/Answers/40230112119/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/Answers/40230112119/.idea/dbnavigator.xml b/Answers/40230112119/.idea/dbnavigator.xml new file mode 100644 index 0000000..ac5f94f --- /dev/null +++ b/Answers/40230112119/.idea/dbnavigator.xml @@ -0,0 +1,406 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Answers/40230112119/.idea/encodings.xml b/Answers/40230112119/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/Answers/40230112119/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Answers/40230112119/.idea/jarRepositories.xml b/Answers/40230112119/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/Answers/40230112119/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Answers/40230112119/.idea/libraries/junit_jupiter.xml b/Answers/40230112119/.idea/libraries/junit_jupiter.xml new file mode 100644 index 0000000..965272a --- /dev/null +++ b/Answers/40230112119/.idea/libraries/junit_jupiter.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Answers/40230112119/.idea/misc.xml b/Answers/40230112119/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/Answers/40230112119/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Answers/40230112119/.idea/uiDesigner.xml b/Answers/40230112119/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/Answers/40230112119/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Answers/40230112119/.idea/vcs.xml b/Answers/40230112119/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/Answers/40230112119/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Answers/40230112119/pom.xml b/Answers/40230112119/pom.xml new file mode 100644 index 0000000..6803a74 --- /dev/null +++ b/Answers/40230112119/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + org.example + StringMaster + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + + + org.junit + junit-bom + 5.8.2 + pom + import + + + + + + + org.junit.jupiter + junit-jupiter + test + + + + \ No newline at end of file diff --git a/Answers/40230112119/src/main/java/Advanced.java b/Answers/40230112119/src/main/java/Advanced.java new file mode 100644 index 0000000..3e24aaf --- /dev/null +++ b/Answers/40230112119/src/main/java/Advanced.java @@ -0,0 +1,76 @@ +import java.util.Objects; + +public class Advanced { + + /** + * Goal : Changing a Sentence Content + + * In this function, you have a sentence, a word & a newWord as Entry + * You have to search the sentence to find the word that you were given as input and change it with the newWord + + */ + public String wordCensor(String sentence, String word, String newWord){ + String[] sep = sentence.split(" "); + for (int i = 0; i < sep.length; i++) { + if (sep[i].equals(word)) { + sep[i] = newWord; + } + } + String result = String.valueOf(sep[0]); + for (int i = 1; i < sep.length; i++) { + result += " " + sep[i]; + } + + return result; + + } + + /** + * In this function You have a firstName and a lastName as Entry and you have to normalize them as a fullName + * @param firstName is a first name with irregular letters (example : hARry) + * @param lastName is a last name with irregular letters (example : pOtTeR) + * @return fullName is a normal full name that just the first letter of firstName & lastName is Capitalized (example : Harry Potter) + */ + public String normalizingName(String firstName, String lastName){ + char[] letterf = firstName.toCharArray(); + char[] letterl = lastName.toCharArray(); + letterf[0] = Character.toUpperCase(letterf[0]); + letterl[0] = Character.toUpperCase(letterl[0]); + for (int i = 1; i < letterf.length; i++) { + letterf[i] = Character.toLowerCase(letterf[i]); + } + for (int i = 1; i < letterl.length; i++) { + letterl[i] = Character.toLowerCase(letterl[i]); + } + if (lastName.equals(" ")) { + return String.valueOf(letterf); + } + else { + String result = String.valueOf(letterf) + " " + String.valueOf(letterl); + return result; + } + } + + /** + * Removing repeated letter in a word + * @param word This input could have Consecutive repeated letters or not + * @return if word contains Consecutive repeated letters, one of the repeated letters should be omitted + */ + public String doubleChar(String word) { + char[] a = word.toCharArray(); + String newWord = ""; + int i = 0, j = 0; + while (j < a.length) { + if (a[i] == a[j]) + j++; + else { + newWord += a[i]; + i = j; + j++; + } + } + newWord += a[j - 1]; + return newWord; + } +} + diff --git a/Answers/40230112119/src/main/java/Warmup.java b/Answers/40230112119/src/main/java/Warmup.java new file mode 100644 index 0000000..084ced2 --- /dev/null +++ b/Answers/40230112119/src/main/java/Warmup.java @@ -0,0 +1,69 @@ +public class Warmup { + + /** + * Goal : Simple Introduction To Strings + * In the first function, your inputs are a number and a sentence + * @return is the number th word of the sentence + */ + public String wordFinder(String sentence, int number) { + String[] sep = sentence.split(" "); + String result = " Number = " + number + " is out Of Bound"; + if (number > 0 && number <= sep.length) + return sep[number - 1]; + else + return result; + } + + /** + * Goal : Basic introduction to Strings & using foreach + * @param number is in String type + * @param searchForEven is a boolean entry + * @return if searchForEven is true ? return the number of even numbers : return the number of odd numbers + */ + public int oddEvenCounter(String number, boolean searchForEven) { + char[] num = number.toCharArray(); + int countOdd = 0, countEven = 0; + if (!searchForEven) { + for (int i = 0; i < number.length(); i++) { + if (num[i] == '1' || num[i] == '3' || num[i] == '5' || num[i] == '7' || num[i] == '9') + countOdd++; + } + return countOdd; + } + else { + for (int i = 0; i < number.length(); i++) { + if (num[i] == '0' || num[i] == '2' || num[i] == '4' || num[i] == '6' || num[i] == '8') + countEven++; + } + return countEven; + } + } + + /** + * @param wordA --> first word + * @param wordB --> second word + * @return The word that is first in alphabet column + */ + public String firstWord(String wordA, String wordB) { + int common_length = (wordA.length() <= wordB.length())? wordA.length():wordB.length(); + int i = 0; + String flag = " "; + while(i < common_length) + { + if(wordA.charAt(i) == wordB.charAt(i)) + i++; + else + { + if((int)wordA.charAt(i) < (int)wordB.charAt(i)){ + flag = wordA; + break; + } + else{ + flag = wordB; + break; + } + } + } + return flag; + } +} diff --git a/Answers/40230112119/src/test/java/StringTest.java b/Answers/40230112119/src/test/java/StringTest.java new file mode 100644 index 0000000..bc387a0 --- /dev/null +++ b/Answers/40230112119/src/test/java/StringTest.java @@ -0,0 +1,41 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class StringTest { + + Warmup warmup = new Warmup(); + Advanced advanced = new Advanced(); + @Test + public void wordFinder_test(){ + assertEquals("Term", warmup.wordFinder("Happy New Term", 3)); + assertEquals(" Number = 4 is out Of Bound", warmup.wordFinder("Hello From Java", 4)); + } + @Test + public void oddEvenCounter_test(){ + assertEquals(2, warmup.oddEvenCounter("43512", true)); + assertEquals(0, warmup.oddEvenCounter("00000", false)); + assertEquals(4, warmup.oddEvenCounter("1111", false)); + } + @Test + public void firstWord_test(){ + assertEquals("Fred", warmup.firstWord("George", "Fred")); + assertEquals("Harrold", warmup.firstWord("Harry", "Harrold")); + assertEquals("Ali", warmup.firstWord("Ali", "Alson")); + assertEquals(" ", warmup.firstWord(" ", "Albus")); + } + @Test + public void wordCensor_test(){ + assertEquals("We should stop He Who Must Not Be Named", advanced.wordCensor("We should stop Voldemort", "Voldemort", "He Who Must Not Be Named")); + assertEquals("I'm Coding Java", advanced.wordCensor("I'm Coding cpp", "cpp", "Java")); + } + @Test + public void normalizingName_test(){ + assertEquals("Ron Weasley", advanced.normalizingName("rOn", "weASlEy")); + assertEquals("Hogwart", advanced.normalizingName("hoGWart", " ")); + } + @Test + public void doubleChar_test(){ + assertEquals("Hary poter", advanced.doubleChar("Harry potter")); + assertEquals("Hary", advanced.doubleChar("Harrrry")); + } +} diff --git a/Answers/40230112119/target/classes/Advanced.class b/Answers/40230112119/target/classes/Advanced.class new file mode 100644 index 0000000..750ff72 Binary files /dev/null and b/Answers/40230112119/target/classes/Advanced.class differ diff --git a/Answers/40230112119/target/classes/Warmup.class b/Answers/40230112119/target/classes/Warmup.class new file mode 100644 index 0000000..9b93327 Binary files /dev/null and b/Answers/40230112119/target/classes/Warmup.class differ diff --git a/Answers/40230112119/target/test-classes/StringTest.class b/Answers/40230112119/target/test-classes/StringTest.class new file mode 100644 index 0000000..9ae0096 Binary files /dev/null and b/Answers/40230112119/target/test-classes/StringTest.class differ