Browse Source

Implemented Math Utilities, few methods in StringUtilities and LoopFun

Yesoda Sanka 6 years ago
parent
commit
61b2da63f2
3 changed files with 72 additions and 11 deletions
  1. 22
    3
      LoopFun.java
  2. 17
    4
      MathUtilities.java
  3. 33
    4
      StringUtilities.java

+ 22
- 3
LoopFun.java View File

@@ -8,8 +8,15 @@ public class LoopFun
8 8
        * @return the factorial of the number
9 9
        */
10 10
       public int factorial(int number){
11
-          return -1;
12
-      }
11
+          int sum=0;
12
+          for(int i=1;i<=number;i++)
13
+          { 
14
+              int fact=i*number;
15
+               sum=sum+fact;
16
+              
17
+          
18
+      }return sum;
19
+    }
13 20
 
14 21
       /**
15 22
        * Given a phrase, get the acronym of that phrase. Acronym is the combination of
@@ -19,6 +26,15 @@ public class LoopFun
19 26
        * @return Upper case string of the first letter of each word
20 27
        */
21 28
       public String acronym(String phrase) {
29
+          phrase.trim();
30
+          int str=phrase.length();
31
+          char ch;
32
+          
33
+          
34
+          
35
+          
36
+          
37
+          
22 38
           return null;
23 39
       }
24 40
 
@@ -35,6 +51,9 @@ public class LoopFun
35 51
        * @return the encrypted string by shifting each character by three character
36 52
        */
37 53
       public String encrypt(String word) {
38
-          return null;
54
+          String str=word.replace('a','d');
55
+          
56
+          
57
+          return str;
39 58
       }
40 59
 }

+ 17
- 4
MathUtilities.java View File

@@ -1,3 +1,7 @@
1
+
2
+
3
+
4
+
1 5
 public class MathUtilities{
2 6
   /**
3 7
    * Add two number together
@@ -6,7 +10,8 @@ public class MathUtilities{
6 10
    * @return the sum of the two numbers
7 11
    */
8 12
   public int add(int num1, int num2){
9
-      return -1;
13
+      int sum=num1+num2;
14
+      return sum;
10 15
   }
11 16
 
12 17
   /**
@@ -16,7 +21,7 @@ public class MathUtilities{
16 21
    * @return the sum of the two numbers
17 22
    */
18 23
   public double add(double num1, double num2){
19
-      return -1;
24
+      return num1+num2;
20 25
   }
21 26
 
22 27
   /**
@@ -25,7 +30,8 @@ public class MathUtilities{
25 30
    * @return the half of the number in double
26 31
    */
27 32
   public double half(int number) {
28
-      return -1;
33
+     double val=(double)number/2;
34
+      return val;
29 35
   }
30 36
 
31 37
   /**
@@ -34,7 +40,14 @@ public class MathUtilities{
34 40
    * @return true if the number is odd, false if it is even
35 41
    */
36 42
   public boolean isOdd(int number){
43
+      if(number%2==1)
44
+      {
45
+      return true;
46
+    }
47
+      else
48
+      {
37 49
       return false;
50
+    }
38 51
   }
39 52
 
40 53
 
@@ -44,7 +57,7 @@ public class MathUtilities{
44 57
    * @return the result of the number multiply by itself
45 58
    */
46 59
   public int square(int number) {
47
-      return -1;
60
+      return number*number;
48 61
   }
49 62
 
50 63
 }

+ 33
- 4
StringUtilities.java View File

@@ -1,11 +1,40 @@
1
+ //import java.utils.*;
1 2
 public class StringUtilities {
2 3
    public Character getMiddleCharacter(String word){
3
-       return null;
4
-   }
4
+       int  n= word.length();
5
+       char ch;
6
+       int val=n%2;
7
+       int i=0;
8
+       if(val== 0)
9
+       {  
10
+          i =n/2;
11
+          ch=word.charAt(i);
12
+           //System.out.println(val);
13
+           return ch;
14
+          
15
+        }
16
+          else
17
+          {
18
+          i=(int)Math.floor(n/2);
19
+          ch= word.charAt(i -1);
20
+          return ch;
21
+        }
22
+    }    
23
+       
24
+   
25
+
5 26
    
6 27
    public String removeCharacter(String value, char charToRemove){
7
-     return null;  
8
-   }
28
+       int n=value.length();
29
+       for(int i=0;i<n;i++)
30
+       {
31
+           
32
+        }
33
+       
34
+    
35
+    
36
+   return null;
37
+}
9 38
    
10 39
    public String getLastWord(String value) {
11 40
        return null;