Просмотр исходного кода

Implemented Math Utilities, few methods in StringUtilities and LoopFun

Yesoda Sanka 8 лет назад
Родитель
Сommit
61b2da63f2
3 измененных файлов: 72 добавлений и 11 удалений
  1. 22
    3
      LoopFun.java
  2. 17
    4
      MathUtilities.java
  3. 33
    4
      StringUtilities.java

+ 22
- 3
LoopFun.java Просмотреть файл

8
        * @return the factorial of the number
8
        * @return the factorial of the number
9
        */
9
        */
10
       public int factorial(int number){
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
        * Given a phrase, get the acronym of that phrase. Acronym is the combination of
22
        * Given a phrase, get the acronym of that phrase. Acronym is the combination of
19
        * @return Upper case string of the first letter of each word
26
        * @return Upper case string of the first letter of each word
20
        */
27
        */
21
       public String acronym(String phrase) {
28
       public String acronym(String phrase) {
29
+          phrase.trim();
30
+          int str=phrase.length();
31
+          char ch;
32
+          
33
+          
34
+          
35
+          
36
+          
37
+          
22
           return null;
38
           return null;
23
       }
39
       }
24
 
40
 
35
        * @return the encrypted string by shifting each character by three character
51
        * @return the encrypted string by shifting each character by three character
36
        */
52
        */
37
       public String encrypt(String word) {
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 Просмотреть файл

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

+ 33
- 4
StringUtilities.java Просмотреть файл

1
+ //import java.utils.*;
1
 public class StringUtilities {
2
 public class StringUtilities {
2
    public Character getMiddleCharacter(String word){
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
    public String removeCharacter(String value, char charToRemove){
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
    public String getLastWord(String value) {
39
    public String getLastWord(String value) {
11
        return null;
40
        return null;