Spaces:
Running
Running
Create 10b. Middle character or nothing
Browse files
Week 2: Methods, strings and lists/10b. Middle character or nothing
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write the method
|
| 2 |
+
|
| 3 |
+
char middleChar(String str)
|
| 4 |
+
|
| 5 |
+
...which returns the middle character of the string.
|
| 6 |
+
|
| 7 |
+
However, if the string contains an even number of characters (i.e. there is no middle character),
|
| 8 |
+
the method returns an empty line (i.e. a minus sign).
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
An example of a method call:
|
| 13 |
+
public static void main(String[] args) {
|
| 14 |
+
System.out.println(middleChar("abcde"));
|
| 15 |
+
System.out.println(middleChar("123"));
|
| 16 |
+
System.out.println(middleChar("wxyz"));
|
| 17 |
+
}
|
| 18 |
+
Program outputs:
|
| 19 |
+
c
|
| 20 |
+
2
|
| 21 |
+
-
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
import java.util.Random;
|
| 30 |
+
|
| 31 |
+
public class Test{
|
| 32 |
+
public static void main(String[] args){
|
| 33 |
+
final Random r = new Random();
|
| 34 |
+
|
| 35 |
+
// String - elements have dtype String
|
| 36 |
+
// [] means an array containing elements of the SAME TYPE
|
| 37 |
+
// s = string variable
|
| 38 |
+
String[] s = "first second third fourth donald test alphabet hello programming choochooo".split(" ");
|
| 39 |
+
// from string 's'
|
| 40 |
+
// iterate over every sub-string 'pa'
|
| 41 |
+
for (String pa : s) {
|
| 42 |
+
System.out.println("Testing with parameter " + pa);
|
| 43 |
+
System.out.println("Middle character: " + middleChar(pa));
|
| 44 |
+
System.out.println("");
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
//ADD
|
| 50 |
+
public static char middleChar(String str) {
|
| 51 |
+
int length = str.length();
|
| 52 |
+
if (length % 2 == 0) {
|
| 53 |
+
return '-';
|
| 54 |
+
} else {
|
| 55 |
+
return str.charAt(length / 2);
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
Testing with parameter first
|
| 66 |
+
Middle character: r
|
| 67 |
+
|
| 68 |
+
Testing with parameter second
|
| 69 |
+
Middle character: -
|
| 70 |
+
|
| 71 |
+
Testing with parameter third
|
| 72 |
+
Middle character: i
|
| 73 |
+
|
| 74 |
+
Testing with parameter fourth
|
| 75 |
+
Middle character: -
|
| 76 |
+
|
| 77 |
+
Testing with parameter donald
|
| 78 |
+
Middle character: -
|
| 79 |
+
|
| 80 |
+
Testing with parameter test
|
| 81 |
+
Middle character: -
|
| 82 |
+
|
| 83 |
+
Testing with parameter alphabet
|
| 84 |
+
Middle character: -
|
| 85 |
+
|
| 86 |
+
Testing with parameter hello
|
| 87 |
+
Middle character: l
|
| 88 |
+
|
| 89 |
+
Testing with parameter programming
|
| 90 |
+
Middle character: a
|
| 91 |
+
|
| 92 |
+
Testing with parameter choochooo
|
| 93 |
+
Middle character: c
|
| 94 |
+
|
| 95 |
+
|