Browse Source

completed the quiz

thulasi 6 years ago
parent
commit
9f5d9ae77f
3 changed files with 62 additions and 10 deletions
  1. 17
    2
      LoopFun.java
  2. 8
    5
      MathUtilities.java
  3. 37
    3
      StringUtilities.java

+ 17
- 2
LoopFun.java View File

@@ -8,7 +8,10 @@ public class LoopFun
8 8
        * @return the factorial of the number
9 9
        */
10 10
       public int factorial(int number){
11
-          return -1;
11
+          int fact =1;
12
+          for(int i=1;i<=number;i++)
13
+            fact *=i;
14
+          return fact;
12 15
       }
13 16
 
14 17
       /**
@@ -19,7 +22,14 @@ public class LoopFun
19 22
        * @return Upper case string of the first letter of each word
20 23
        */
21 24
       public String acronym(String phrase) {
22
-          return null;
25
+          
26
+        String ar[] = phrase.split(" ");
27
+        phrase ="";
28
+        for(String p : ar) {
29
+            phrase += p.substring(0,1).toUpperCase();
30
+        }
31
+        //System.out.println(phrase); 
32
+          return phrase;
23 33
       }
24 34
 
25 35
       /**
@@ -35,6 +45,11 @@ public class LoopFun
35 45
        * @return the encrypted string by shifting each character by three character
36 46
        */
37 47
       public String encrypt(String word) {
48
+         
49
+        String encrypted = "";
50
+        for(int i = 0;i<word.length();i++){
51
+            encrypted += (char)(word.charAt(i) + 3);
52
+        }
38 53
           return null;
39 54
       }
40 55
 }

+ 8
- 5
MathUtilities.java View File

@@ -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,7 @@ 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
+      return (double)number/2;
29 29
   }
30 30
 
31 31
   /**
@@ -34,7 +34,10 @@ public class MathUtilities{
34 34
    * @return true if the number is odd, false if it is even
35 35
    */
36 36
   public boolean isOdd(int number){
37
-      return false;
37
+      if(number%2!=0)
38
+        return true;
39
+      else
40
+        return false ;
38 41
   }
39 42
 
40 43
 
@@ -44,7 +47,7 @@ public class MathUtilities{
44 47
    * @return the result of the number multiply by itself
45 48
    */
46 49
   public int square(int number) {
47
-      return -1;
50
+      return number*number;
48 51
   }
49 52
 
50 53
 }

+ 37
- 3
StringUtilities.java View File

@@ -1,13 +1,47 @@
1 1
 public class StringUtilities {
2 2
    public Character getMiddleCharacter(String word){
3
-       return null;
3
+       if(word.length()%2 == 0){
4
+            return word.charAt(word.length()/2);
5
+        }
6
+        else{
7
+            return word.charAt(word.length()/2);
8
+        }
4 9
    }
5 10
    
6 11
    public String removeCharacter(String value, char charToRemove){
7
-     return null;  
12
+       StringBuilder b = new StringBuilder(value);
13
+       /*for(int i=0;i<value.length;i++)
14
+        if(value.charAt(i) == charToRemove)
15
+            value.charAt(i+1) = 
16
+            */
17
+           
18
+       for(int i=0;i<b.length();i++){
19
+           if(b.charAt(i) == charToRemove){
20
+               b.deleteCharAt(i);
21
+                if(i<b.length() && b.charAt(i) == charToRemove)
22
+                    b.deleteCharAt(i); 
23
+                    if(i<b.length() && b.charAt(i) == charToRemove)
24
+                        b.deleteCharAt(i);
25
+            }
26
+            }
27
+       value ="";
28
+       value = String.format("%s",b.toString());
29
+       
30
+       /*String newString ="";
31
+       
32
+       for(int i=0;i<value.length();i++){
33
+           if(value.charAt(i) == charToRemove){
34
+            newString = value.substring(0,i)+value.substring(i+1);
35
+            break;
36
+           }
37
+        }*/
38
+       return value;  
8 39
    }
9 40
    
10 41
    public String getLastWord(String value) {
11
-       return null;
42
+         String ar[] = value.split(" ");
43
+      
44
+       
45
+       return ar[(ar.length-1)];
12 46
    }
13 47
 }