Browse Source

QuizWeek1

Soujanya Buragapu 6 years ago
parent
commit
4659551f7d
6 changed files with 131 additions and 73 deletions
  1. 17
    4
      LoopFun.java
  2. 38
    11
      MathUtilities.java
  3. 28
    24
      MathUtilitiesTest.java
  4. 19
    8
      StringUtilities.java
  5. 8
    4
      StringUtilitiesTest.java
  6. 21
    22
      package.bluej

+ 17
- 4
LoopFun.java View File

@@ -7,8 +7,15 @@ public class LoopFun
7 7
        * @param number
8 8
        * @return the factorial of the number
9 9
        */
10
-      public int factorial(int number){
11
-          return -1;
10
+      public int factorial(int number)
11
+      {
12
+          int i;
13
+          int fact = 1;
14
+          for(i=1;i<=number;i++)
15
+          {
16
+              fact=fact*i;
17
+            }
18
+          return fact;
12 19
       }
13 20
 
14 21
       /**
@@ -18,8 +25,14 @@ public class LoopFun
18 25
        * @param phrase
19 26
        * @return Upper case string of the first letter of each word
20 27
        */
21
-      public String acronym(String phrase) {
22
-          return null;
28
+      public String acronym(String phrase) 
29
+      {
30
+          String res = "";
31
+          res = phrase.replaceAll("[a-z ]", "" ).toUpperCase();
32
+          
33
+          
34
+          
35
+          return res;
23 36
       }
24 37
 
25 38
       /**

+ 38
- 11
MathUtilities.java View File

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

+ 28
- 24
MathUtilitiesTest.java View File

@@ -1,5 +1,4 @@
1 1
 
2
-
3 2
 import static org.junit.Assert.*;
4 3
 import org.junit.After;
5 4
 import org.junit.Before;
@@ -11,12 +10,14 @@ public class MathUtilitiesTest
11 10
     private static final double DELTA = 0.009;
12 11
 
13 12
     @Before
14
-    public void setup(){
13
+    public void setup()
14
+    {
15 15
         utilities = new MathUtilities();
16 16
     }
17 17
 
18 18
     @Test
19
-    public void testAddInteger(){
19
+    public void testAddInteger()
20
+    {
20 21
         //Given
21 22
         int expected = 15;
22 23
 
@@ -28,7 +29,8 @@ public class MathUtilitiesTest
28 29
     }
29 30
 
30 31
     @Test
31
-    public void testAddDouble(){
32
+    public void testAddDouble()
33
+    {
32 34
         //Given
33 35
         double expected = 11.22;
34 36
 
@@ -40,7 +42,8 @@ public class MathUtilitiesTest
40 42
     }
41 43
 
42 44
     @Test
43
-    public void testHalfForEven(){
45
+    public void testHalfForEven()
46
+    {
44 47
         //Given
45 48
         double expected = 2.0;
46 49
 
@@ -52,7 +55,8 @@ public class MathUtilitiesTest
52 55
     }
53 56
 
54 57
     @Test
55
-    public void testHalfForOdd(){
58
+    public void testHalfForOdd()
59
+    {
56 60
         //Given
57 61
         double expected = 12.5;
58 62
 
@@ -75,37 +79,37 @@ public class MathUtilitiesTest
75 79
 
76 80
     @Test
77 81
     public void testSquareForBaseCase() {
78
-      //Given
79
-      int expected = 1;
82
+        //Given
83
+        int expected = 1;
80 84
 
81
-      //When
82
-      int actual = utilities.square(1);
85
+        //When
86
+        int actual = utilities.square(1);
83 87
 
84
-      //Then
85
-      assertEquals(expected, actual);
88
+        //Then
89
+        assertEquals(expected, actual);
86 90
     }
87 91
 
88 92
     @Test
89 93
     public void testSquareForOneDigit() {
90
-      //Given
91
-      int expected = 9;
94
+        //Given
95
+        int expected = 9;
92 96
 
93
-      //When
94
-      int actual = utilities.square(3);
97
+        //When
98
+        int actual = utilities.square(3);
95 99
 
96
-      //Then
97
-      assertEquals(expected, actual);
100
+        //Then
101
+        assertEquals(expected, actual);
98 102
     }
99 103
 
100 104
     @Test
101 105
     public void testSquareForTwoDigit() {
102
-      //Given
103
-      int expected = 121;
106
+        //Given
107
+        int expected = 121;
104 108
 
105
-      //When
106
-      int actual = utilities.square(11);
109
+        //When
110
+        int actual = utilities.square(11);
107 111
 
108
-      //Then
109
-      assertEquals(expected, actual);
112
+        //Then
113
+        assertEquals(expected, actual);
110 114
     }
111 115
 }

+ 19
- 8
StringUtilities.java View File

@@ -1,13 +1,24 @@
1
+
2
+import java.util.*;
3
+
1 4
 public class StringUtilities {
2
-   public Character getMiddleCharacter(String word){
3
-       return null;
5
+   public Character getMiddleCharacter(String word)
6
+   {
7
+       return word.charAt(word.length()/2);
8
+       
9
+    }
10
+   public String removeCharacter(String value, char charToRemove)
11
+   {
12
+     String s = "";
13
+     s= s+charToRemove;
14
+     String str ="";
15
+     str=value.replaceAll(s,"");
16
+     return str;
4 17
    }
5 18
    
6
-   public String removeCharacter(String value, char charToRemove){
7
-     return null;  
8
-   }
9
-   
10
-   public String getLastWord(String value) {
11
-       return null;
19
+   public String getLastWord(String value) 
20
+   {
21
+       String str= value.substring(value.lastIndexOf(" ")+1);
22
+       return str;
12 23
    }
13 24
 }

+ 8
- 4
StringUtilitiesTest.java View File

@@ -3,17 +3,20 @@ import org.junit.After;
3 3
 import org.junit.Before;
4 4
 import org.junit.Test;
5 5
 
6
-public class StringUtilitiesTest {
6
+public class StringUtilitiesTest 
7
+{
7 8
 
8 9
     private StringUtilities utilities;
9 10
 
10 11
     @Before
11
-    public void setUp() {
12
+    public void setUp()
13
+    {
12 14
         utilities = new StringUtilities();
13 15
     }
14 16
     
15 17
     @Test
16
-    public void testGetMiddleCharacter_ForOddWord(){
18
+    public void testGetMiddleCharacter_ForOddWord()
19
+    {
17 20
         //Given
18 21
         String word = "bokeh";
19 22
         char expected = 'k';
@@ -26,7 +29,8 @@ public class StringUtilitiesTest {
26 29
     }
27 30
     
28 31
     @Test
29
-    public void testGetMiddleCharacter_ForLongOddWord(){
32
+    public void testGetMiddleCharacter_ForLongOddWord()
33
+    {
30 34
         //Given
31 35
         String word = "disinformations";
32 36
         char expected = 'r';

+ 21
- 22
package.bluej View File

@@ -2,25 +2,25 @@
2 2
 dependency1.from=StringUtilitiesTest
3 3
 dependency1.to=StringUtilities
4 4
 dependency1.type=UsesDependency
5
-dependency2.from=BonusTest
6
-dependency2.to=Bonus
5
+dependency2.from=LoopFunTest
6
+dependency2.to=LoopFun
7 7
 dependency2.type=UsesDependency
8 8
 dependency3.from=MathUtilitiesTest
9 9
 dependency3.to=MathUtilities
10 10
 dependency3.type=UsesDependency
11
-editor.fx.0.height=722
12
-editor.fx.0.width=876
13
-editor.fx.0.x=158
14
-editor.fx.0.y=216
15
-objectbench.height=164
16
-objectbench.width=669
17
-package.divider.horizontal=0.6
18
-package.divider.vertical=0.7628294036061026
19
-package.editor.height=543
11
+editor.fx.0.height=663
12
+editor.fx.0.width=928
13
+editor.fx.0.x=120
14
+editor.fx.0.y=34
15
+objectbench.height=149
16
+objectbench.width=397
17
+package.divider.horizontal=0.600297176820208
18
+package.divider.vertical=0.7629179331306991
19
+package.editor.height=495
20 20
 package.editor.width=567
21
-package.editor.x=557
22
-package.editor.y=43
23
-package.frame.height=779
21
+package.editor.x=352
22
+package.editor.y=23
23
+package.frame.height=716
24 24
 package.frame.width=693
25 25
 package.numDependencies=3
26 26
 package.numTargets=6
@@ -40,20 +40,19 @@ target1.width=110
40 40
 target1.x=110
41 41
 target1.y=150
42 42
 target2.height=50
43
-target2.name=BonusTest
43
+target2.name=LoopFunTest
44 44
 target2.showInterface=false
45 45
 target2.type=UnitTestTargetJunit4
46
-target2.width=80
47
-target2.x=120
48
-target2.y=270
49
-target3.association=BonusTest
46
+target2.width=110
47
+target2.x=10
48
+target2.y=240
50 49
 target3.height=50
51
-target3.name=Bonus
50
+target3.name=LoopFun
52 51
 target3.showInterface=false
53 52
 target3.type=ClassTarget
54 53
 target3.width=80
55
-target3.x=90
56
-target3.y=300
54
+target3.x=200
55
+target3.y=210
57 56
 target4.association=MathUtilitiesTest
58 57
 target4.height=50
59 58
 target4.name=MathUtilities