CHU1TA26 6 anos atrás
pai
commit
371a9ce5f6
3 arquivos alterados com 81 adições e 42 exclusões
  1. 52
    34
      LoopFun.java
  2. 8
    4
      MathUtilities.java
  3. 21
    4
      StringUtilities.java

+ 52
- 34
LoopFun.java Ver arquivo

@@ -1,40 +1,58 @@
1 1
 public class LoopFun
2 2
 {
3 3
 
4
-      /**
5
-       * Given a number, return the factorial of that number.
6
-       * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120.
7
-       * @param number
8
-       * @return the factorial of the number
9
-       */
10
-      public int factorial(int number){
11
-          return -1;
12
-      }
4
+    /**
5
+     * Given a number, return the factorial of that number.
6
+     * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120.
7
+     * @param number
8
+     * @return the factorial of the number
9
+     */
10
+    public int factorial(int number){
11
+        for(int i=1; i<= number; i++){
12
+            return number * i;
13
+        }
14
+        return number;
15
+    }
13 16
 
14
-      /**
15
-       * Given a phrase, get the acronym of that phrase. Acronym is the combination of
16
-       * the first character of each word in upper case.
17
-       * For example, given "Ruby on Rails", this method will return "ROR"
18
-       * @param phrase
19
-       * @return Upper case string of the first letter of each word
20
-       */
21
-      public String acronym(String phrase) {
22
-          return null;
23
-      }
17
+    /**
18
+     * Given a phrase, get the acronym of that phrase. Acronym is the combination of
19
+     * the first character of each word in upper case.
20
+     * For example, given "Ruby on Rails", this method will return "ROR"
21
+     * @param phrase
22
+     * @return Upper case string of the first letter of each word
23
+     */
24
+    public String acronym(String phrase) {
24 25
 
25
-      /**
26
-       * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
27
-       * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
28
-       * at the end of the alphabet, it will wraps around.
29
-       * For example:
30
-       *  'a' => 'd'
31
-       *  'w' => 'z'
32
-       *  'x' => 'a'
33
-       *  'y' => 'b'
34
-       * @param word
35
-       * @return the encrypted string by shifting each character by three character
36
-       */
37
-      public String encrypt(String word) {
38
-          return null;
39
-      }
26
+        String []acronym = phrase.split(" ");
27
+
28
+        for (int i = 0; i < acronym.length; i++) {
29
+            String s = acronym[i];
30
+            s.charAt(0);
31
+        }
32
+
33
+        return s.toUpperCase();
34
+    }
35
+
36
+    /**
37
+     * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
38
+     * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
39
+     * at the end of the alphabet, it will wraps around.
40
+     * For example:
41
+     *  'a' => 'd'
42
+     *  'w' => 'z'
43
+     *  'x' => 'a'
44
+     *  'y' => 'b'
45
+     * @param word
46
+     * @return the encrypted string by shifting each character by three character
47
+     */
48
+    public String encrypt(String word) {
49
+        
50
+        char [] encodeWord= word.toCharArray();
51
+        for (int i= 0; i <encodeWord.length(); i++){
52
+            if (Character.isLetter(encodeWord[i])){
53
+                encodeWord[i]+=3;}
54
+        }
55
+         
56
+        return encodeWord;
57
+    }
40 58
 }

+ 8
- 4
MathUtilities.java Ver arquivo

@@ -6,7 +6,7 @@ public class MathUtilities{
6 6
    * @return the sum of the two numbers
7 7
    */
8 8
   public int add(int num1, int num2){
9
-      return -1;
9
+      return num1+num2;
10 10
   }
11 11
 
12 12
   /**
@@ -16,7 +16,7 @@ public class MathUtilities{
16 16
    * @return the sum of the two numbers
17 17
    */
18 18
   public double add(double num1, double num2){
19
-      return -1;
19
+      return num1 + num2;
20 20
   }
21 21
 
22 22
   /**
@@ -25,7 +25,8 @@ public class MathUtilities{
25 25
    * @return the half of the number in double
26 26
    */
27 27
   public double half(int number) {
28
-      return -1;
28
+      double num= number/ 2;
29
+      return (double )num;
29 30
   }
30 31
 
31 32
   /**
@@ -34,6 +35,9 @@ public class MathUtilities{
34 35
    * @return true if the number is odd, false if it is even
35 36
    */
36 37
   public boolean isOdd(int number){
38
+    
39
+     
40
+    
37 41
       return false;
38 42
   }
39 43
 
@@ -44,7 +48,7 @@ public class MathUtilities{
44 48
    * @return the result of the number multiply by itself
45 49
    */
46 50
   public int square(int number) {
47
-      return -1;
51
+      return number * number;
48 52
   }
49 53
 
50 54
 }

+ 21
- 4
StringUtilities.java Ver arquivo

@@ -1,13 +1,30 @@
1 1
 public class StringUtilities {
2 2
    public Character getMiddleCharacter(String word){
3
-       return null;
3
+       int post;
4
+        int leng;
5
+        if (word.length()% 2 == 1){
6
+            post=word.length()/2;
7
+            leng= 1;
8
+        } else {
9
+            post=word.length()/2-1;
10
+            leng = 1; 
11
+        }
12
+        String mid = word.substring(post, post
13
+                + leng);
14
+                char output = mid.charAt(0);
15
+        return output;
16
+       
4 17
    }
5
-   
18
+  
6 19
    public String removeCharacter(String value, char charToRemove){
7
-     return null;  
20
+       
21
+     String newString = value.replaceAll(Character.toString(charToRemove), "");
22
+     
23
+     return newString;  
8 24
    }
9 25
    
10 26
    public String getLastWord(String value) {
11
-       return null;
27
+       String lastWord = value.substring(value.lastIndexOf(" ")+1);
28
+       return lastWord;
12 29
    }
13 30
 }