Browse Source

String lab1

Clement Ojie 6 years ago
parent
commit
2c2ca86e66
5 changed files with 66 additions and 53 deletions
  1. 26
    26
      MathUtilities.java
  2. 5
    4
      PredicateUtilities.java
  3. 24
    13
      StringUtilities.java
  4. 1
    0
      StringUtilitiesTest.java
  5. 10
    10
      package.bluej

+ 26
- 26
MathUtilities.java View File

11
     * @return sum of `baseValue` and `difference`
11
     * @return sum of `baseValue` and `difference`
12
     */
12
     */
13
    public Integer add(int baseValue, int difference) {
13
    public Integer add(int baseValue, int difference) {
14
-       return null;
14
+       return baseValue + difference;
15
    }
15
    }
16
 
16
 
17
    /**
17
    /**
20
     * @return sum of `baseValue` and `difference`
20
     * @return sum of `baseValue` and `difference`
21
     */
21
     */
22
    public Long add(long baseValue, long difference) {
22
    public Long add(long baseValue, long difference) {
23
-       return null;
23
+       return baseValue + difference;
24
    }
24
    }
25
 
25
 
26
    /**
26
    /**
29
     * @return sum of `baseValue` and `difference`
29
     * @return sum of `baseValue` and `difference`
30
     */
30
     */
31
    public Short add(short baseValue, short difference) {
31
    public Short add(short baseValue, short difference) {
32
-       return null;
32
+       return (short)(baseValue + difference);
33
    }
33
    }
34
 
34
 
35
    /**
35
    /**
38
     * @return sum of `baseValue` and `difference`
38
     * @return sum of `baseValue` and `difference`
39
     */
39
     */
40
    public Byte add(byte baseValue, byte difference) {
40
    public Byte add(byte baseValue, byte difference) {
41
-       return null;
41
+       return (byte)(baseValue + difference);
42
    }
42
    }
43
 
43
 
44
    /**
44
    /**
47
     * @return sum of `baseValue` and `difference`
47
     * @return sum of `baseValue` and `difference`
48
     */
48
     */
49
    public Float add(float baseValue, float difference) {
49
    public Float add(float baseValue, float difference) {
50
-       return null;
50
+       return (float)(baseValue + difference);
51
    }
51
    }
52
 
52
 
53
    /**
53
    /**
56
     * @return sum of `baseValue` and `difference`
56
     * @return sum of `baseValue` and `difference`
57
     */
57
     */
58
    public Double add(double baseValue, double difference) {
58
    public Double add(double baseValue, double difference) {
59
-       return null;
59
+       return (double)( baseValue + difference);
60
    }
60
    }
61
 
61
 
62
    /**
62
    /**
65
     * @return difference between `baseValue` and `difference`
65
     * @return difference between `baseValue` and `difference`
66
     */
66
     */
67
    public Integer subtract(int baseValue, int difference) {
67
    public Integer subtract(int baseValue, int difference) {
68
-       return null;
68
+       return baseValue - difference;
69
    }
69
    }
70
 
70
 
71
    /**
71
    /**
74
     * @return difference between `baseValue` and `difference`
74
     * @return difference between `baseValue` and `difference`
75
     */
75
     */
76
    public Long subtract(long baseValue, long difference) {
76
    public Long subtract(long baseValue, long difference) {
77
-       return null;
77
+       return baseValue - difference;
78
    }
78
    }
79
 
79
 
80
    /**
80
    /**
83
     * @return difference between `baseValue` and `difference`
83
     * @return difference between `baseValue` and `difference`
84
     */
84
     */
85
    public Short subtract(short baseValue, short difference) {
85
    public Short subtract(short baseValue, short difference) {
86
-       return null;
86
+       return (short) (baseValue - difference);
87
    }
87
    }
88
 
88
 
89
    /**
89
    /**
92
     * @return difference between `baseValue` and `difference`
92
     * @return difference between `baseValue` and `difference`
93
     */
93
     */
94
    public Byte subtract(byte baseValue, byte difference) {
94
    public Byte subtract(byte baseValue, byte difference) {
95
-       return null;
95
+       return (byte) ( baseValue - difference);
96
    }
96
    }
97
 
97
 
98
    /**
98
    /**
101
     * @return difference between `baseValue` and `difference`
101
     * @return difference between `baseValue` and `difference`
102
     */
102
     */
103
    public Float subtract(float baseValue, float difference) {
103
    public Float subtract(float baseValue, float difference) {
104
-       return null;
104
+       return (float) (baseValue - difference);
105
    }
105
    }
106
 
106
 
107
    /**
107
    /**
110
     * @return difference between `baseValue` and `difference`
110
     * @return difference between `baseValue` and `difference`
111
     */
111
     */
112
    public Double subtract(double baseValue, double difference) {
112
    public Double subtract(double baseValue, double difference) {
113
-       return null;
113
+       return (double) ( baseValue - difference);
114
    }
114
    }
115
 
115
 
116
 
116
 
120
     * @return division of `dividend` by `divisor
120
     * @return division of `dividend` by `divisor
121
     */
121
     */
122
    public Integer divide(int dividend, int divisor) {
122
    public Integer divide(int dividend, int divisor) {
123
-       return null;
123
+       return dividend / divisor;
124
    }
124
    }
125
 
125
 
126
    /**
126
    /**
129
     * @return division of `dividend` by `divisor
129
     * @return division of `dividend` by `divisor
130
     */
130
     */
131
    public Long divide(long dividend, long divisor) {
131
    public Long divide(long dividend, long divisor) {
132
-       return null;
132
+       return dividend / divisor;
133
    }
133
    }
134
 
134
 
135
    /**
135
    /**
138
     * @return division of `dividend` by `divisor
138
     * @return division of `dividend` by `divisor
139
     */
139
     */
140
    public Short divide(short dividend, short divisor) {
140
    public Short divide(short dividend, short divisor) {
141
-       return null;
141
+       return (short) (dividend / divisor);
142
    }
142
    }
143
 
143
 
144
    /**
144
    /**
147
     * @return division of `dividend` by `divisor
147
     * @return division of `dividend` by `divisor
148
     */
148
     */
149
    public Byte divide(byte dividend, byte divisor) {
149
    public Byte divide(byte dividend, byte divisor) {
150
-       return null;
150
+       return (byte) ( dividend / divisor) ;
151
    }
151
    }
152
 
152
 
153
    /**
153
    /**
156
     * @return division of `dividend` by `divisor
156
     * @return division of `dividend` by `divisor
157
     */
157
     */
158
    public Float divide(float dividend, float divisor) {
158
    public Float divide(float dividend, float divisor) {
159
-       return null;
159
+       return (float) (dividend / divisor);
160
    }
160
    }
161
 
161
 
162
    /**
162
    /**
165
     * @return division of `dividend` by `divisor
165
     * @return division of `dividend` by `divisor
166
     */
166
     */
167
    public Double divide(double dividend, double divisor) {
167
    public Double divide(double dividend, double divisor) {
168
-       return null;
168
+       return (double) ( dividend / divisor) ;
169
    }
169
    }
170
 
170
 
171
 
171
 
175
     * @return product of `multiplicand` by `multiplier`
175
     * @return product of `multiplicand` by `multiplier`
176
     */
176
     */
177
    public Integer multiply(int multiplicand, int multiplier) {
177
    public Integer multiply(int multiplicand, int multiplier) {
178
-       return null;
178
+       return multiplicand * multiplier;
179
    }
179
    }
180
 
180
 
181
    /**
181
    /**
184
     * @return product of `multiplicand` by `multiplier`
184
     * @return product of `multiplicand` by `multiplier`
185
     */
185
     */
186
    public Long multiply(long multiplicand, long multiplier) {
186
    public Long multiply(long multiplicand, long multiplier) {
187
-       return null;
187
+       return multiplicand * multiplier;
188
    }
188
    }
189
 
189
 
190
    /**
190
    /**
193
     * @return product of `multiplicand` by `multiplier`
193
     * @return product of `multiplicand` by `multiplier`
194
     */
194
     */
195
    public Short multiply(short multiplicand, short multiplier) {
195
    public Short multiply(short multiplicand, short multiplier) {
196
-       return null;
196
+       return (short) ( multiplicand * multiplier);
197
    }
197
    }
198
    /**
198
    /**
199
     * @param multiplicand value to be multiplied
199
     * @param multiplicand value to be multiplied
201
     * @return product of `multiplicand` by `multiplier`
201
     * @return product of `multiplicand` by `multiplier`
202
     */
202
     */
203
    public Byte multiply(byte multiplicand, byte multiplier) {
203
    public Byte multiply(byte multiplicand, byte multiplier) {
204
-       return null;
204
+       return (byte) ( multiplicand * multiplier);
205
    }
205
    }
206
 
206
 
207
    /**
207
    /**
210
     * @return product of `multiplicand` by `multiplier`
210
     * @return product of `multiplicand` by `multiplier`
211
     */
211
     */
212
    public Float multiply(float multiplicand, float multiplier) {
212
    public Float multiply(float multiplicand, float multiplier) {
213
-       return null;
213
+       return (float) (multiplicand * multiplier);
214
    }
214
    }
215
 
215
 
216
    /**
216
    /**
219
     * @return product of `multiplicand` by `multiplier`
219
     * @return product of `multiplicand` by `multiplier`
220
     */
220
     */
221
    public Double multiply(double multiplicand, double multiplier) {
221
    public Double multiply(double multiplicand, double multiplier) {
222
-       return null;
222
+       return (double) ( multiplicand * multiplier);
223
    }
223
    }
224
 
224
 
225
 
225
 
227
      * @return true
227
      * @return true
228
     */
228
     */
229
    public Boolean returnTrue() {
229
    public Boolean returnTrue() {
230
-       return null;
230
+       return true;
231
    }
231
    }
232
 
232
 
233
    /**
233
    /**
234
     * @return false
234
     * @return false
235
     */
235
     */
236
    public Boolean returnFalse() {
236
    public Boolean returnFalse() {
237
-       return null;
237
+       return false;
238
    }
238
    }
239
 
239
 
240
 }
240
 }

+ 5
- 4
PredicateUtilities.java View File

10
     * @return true if `x` is greater than `y`
10
     * @return true if `x` is greater than `y`
11
     */
11
     */
12
    public Boolean isGreaterThan(int x, int y) {
12
    public Boolean isGreaterThan(int x, int y) {
13
-       return null;
13
+       return x > y; 
14
    }
14
    }
15
 
15
 
16
    /**
16
    /**
19
     * @return true if `x` is less than `y`
19
     * @return true if `x` is less than `y`
20
     */
20
     */
21
    public Boolean isLessThan(int x, int y) {
21
    public Boolean isLessThan(int x, int y) {
22
-       return null;
22
+       return x < y;
23
    }
23
    }
24
 
24
 
25
    /**
25
    /**
28
     * @return true if `x` is greater than or equal to `y`
28
     * @return true if `x` is greater than or equal to `y`
29
     */
29
     */
30
    public Boolean isGreaterThanOrEqualTo(int x, int y) {
30
    public Boolean isGreaterThanOrEqualTo(int x, int y) {
31
-       return null;
31
+       return x >= y;
32
+     
32
    }
33
    }
33
 
34
 
34
    /**
35
    /**
37
     * @return true if `x` is less than or equal to `y`
38
     * @return true if `x` is less than or equal to `y`
38
     */
39
     */
39
    public Boolean isLessThanOrEqualTo(int x, int y) {
40
    public Boolean isLessThanOrEqualTo(int x, int y) {
40
-       return null;
41
+       return x <= y;
41
    }
42
    }
42
 }
43
 }

+ 24
- 13
StringUtilities.java View File

8
      * @return `Hello World` as a string
8
      * @return `Hello World` as a string
9
      */
9
      */
10
     public String getHelloWorld() {
10
     public String getHelloWorld() {
11
-        return null;
11
+        return "Hello World";
12
     }
12
     }
13
 
13
 
14
     /**
14
     /**
17
      * @return the concatenation of two strings, `firstSegment`, and `secondSegment`
17
      * @return the concatenation of two strings, `firstSegment`, and `secondSegment`
18
      */
18
      */
19
     public String concatenation(String firstSegment, String secondSegment){
19
     public String concatenation(String firstSegment, String secondSegment){
20
-        return null;
20
+        return firstSegment + secondSegment;
21
     }
21
     }
22
 
22
 
23
     /**
23
     /**
26
      * @return the concatenation of an integer, `firstSegment`, and a String, `secondSegment`
26
      * @return the concatenation of an integer, `firstSegment`, and a String, `secondSegment`
27
      */
27
      */
28
     public String concatenation(int firstSegment, String secondSegment){
28
     public String concatenation(int firstSegment, String secondSegment){
29
-        return null;
29
+        return firstSegment + secondSegment;
30
     }
30
     }
31
 
31
 
32
     /**
32
     /**
34
      * @return the first 3 characters of `input`
34
      * @return the first 3 characters of `input`
35
      */
35
      */
36
     public String getPrefix(String input){
36
     public String getPrefix(String input){
37
-        return null;
37
+        return input.substring(0 , 3);
38
     }
38
     }
39
 
39
 
40
     /**
40
     /**
42
      * @return the last 3 characters of `input`
42
      * @return the last 3 characters of `input`
43
      */
43
      */
44
     public String getSuffix(String input){
44
     public String getSuffix(String input){
45
-        return null;
45
+        return input.substring(input.length()-3, input.length());
46
     }
46
     }
47
 
47
 
48
     /**
48
     /**
51
      * @return the equivalence of two strings, `inputValue` and `comparableValue`
51
      * @return the equivalence of two strings, `inputValue` and `comparableValue`
52
      */
52
      */
53
     public Boolean compareTwoStrings(String inputValue, String comparableValue){
53
     public Boolean compareTwoStrings(String inputValue, String comparableValue){
54
-        return null;
54
+        return inputValue.equals(comparableValue);
55
     }
55
     }
56
 
56
 
57
     /**
57
     /**
59
      * @return the middle character of `inputValue`
59
      * @return the middle character of `inputValue`
60
      */
60
      */
61
     public Character getMiddleCharacter(String inputValue){
61
     public Character getMiddleCharacter(String inputValue){
62
-        return null;
63
-    }
62
+        if (inputValue.length() %2 ==0){
63
+            return inputValue.charAt((inputValue.length()/2) -1);
64
+            
65
+        } else{ return inputValue.charAt(inputValue.length()/2);
66
+      
67
+    }}
64
 
68
 
65
     /**
69
     /**
66
      * @param spaceDelimitedString a string, representative of a sentence, containing spaces
70
      * @param spaceDelimitedString a string, representative of a sentence, containing spaces
67
      * @return the first sequence of characters
71
      * @return the first sequence of characters
68
      */
72
      */
69
     public String getFirstWord(String spaceDelimitedString){
73
     public String getFirstWord(String spaceDelimitedString){
70
-        return null;
74
+        return spaceDelimitedString.substring(0, spaceDelimitedString.indexOf(" "));
71
     }
75
     }
72
 
76
 
73
     /**
77
     /**
75
      * @return the second word of a string delimited by spaces.
79
      * @return the second word of a string delimited by spaces.
76
      */
80
      */
77
     public String getSecondWord(String spaceDelimitedString){
81
     public String getSecondWord(String spaceDelimitedString){
78
-        return null;
82
+        return spaceDelimitedString.substring 
83
+        (spaceDelimitedString.indexOf(" ")+1, spaceDelimitedString.length());
84
+              
79
     }
85
     }
80
 
86
 
81
     /**
87
     /**
83
      * @return an identical string with characters in reverse order.
89
      * @return an identical string with characters in reverse order.
84
      */
90
      */
85
     public String reverse(String stringToReverse){
91
     public String reverse(String stringToReverse){
86
-        return null;
92
+     
93
+        String sol ="";
94
+        for(int x = stringToReverse.length() -1; x>-1; x--){
95
+            sol+= stringToReverse.charAt(x);
96
+        }
97
+        return sol;
87
     }
98
     }
88
 
99
 
89
     /**
100
     /**
91
      * @return an identical string with spaces removed.
102
      * @return an identical string with spaces removed.
92
      */
103
      */
93
     public String removeWhitespace(String input){
104
     public String removeWhitespace(String input){
94
-        return null;
105
+        return input.replaceAll("\\s", "");
95
     }
106
     }
96
 
107
 
97
     /**
108
     /**
99
      * @return an identical string with spaces in the front and end removed.
110
      * @return an identical string with spaces in the front and end removed.
100
      */
111
      */
101
     public String trim(String input){
112
     public String trim(String input){
102
-        return null;
113
+        return input.trim();
103
     }
114
     }
104
 }
115
 }

+ 1
- 0
StringUtilitiesTest.java View File

41
 
41
 
42
         // : When
42
         // : When
43
         String actual = stringUtilities.getHelloWorld();
43
         String actual = stringUtilities.getHelloWorld();
44
+      
44
 
45
 
45
         // : Then
46
         // : Then
46
         assertEquals(expected, actual);
47
         assertEquals(expected, actual);

+ 10
- 10
package.bluej View File

1
 #BlueJ package file
1
 #BlueJ package file
2
-dependency1.from=StringUtilitiesTest
3
-dependency1.to=StringUtilities
2
+dependency1.from=PredicateUtilitiesTest
3
+dependency1.to=PredicateUtilities
4
 dependency1.type=UsesDependency
4
 dependency1.type=UsesDependency
5
-dependency2.from=PredicateUtilitiesTest
6
-dependency2.to=PredicateUtilities
5
+dependency2.from=MathUtilitiesTest
6
+dependency2.to=MathUtilities
7
 dependency2.type=UsesDependency
7
 dependency2.type=UsesDependency
8
-dependency3.from=MathUtilitiesTest
9
-dependency3.to=MathUtilities
8
+dependency3.from=StringUtilitiesTest
9
+dependency3.to=StringUtilities
10
 dependency3.type=UsesDependency
10
 dependency3.type=UsesDependency
11
-editor.fx.0.height=0
12
-editor.fx.0.width=0
13
-editor.fx.0.x=0
14
-editor.fx.0.y=0
11
+editor.fx.0.height=722
12
+editor.fx.0.width=864
13
+editor.fx.0.x=201
14
+editor.fx.0.y=23
15
 objectbench.height=145
15
 objectbench.height=145
16
 objectbench.width=776
16
 objectbench.width=776
17
 package.divider.horizontal=0.6
17
 package.divider.horizontal=0.6