KaiquanMah commited on
Commit
ce3d78b
·
verified ·
1 Parent(s): 6073323

class StringSwapper implements Swapper<String> {...}

Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/06B. Implement the StringSwapper Class ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ In the program, a 'generic interface class' 'Swapper' is defined.
2
+
3
+ Write a class 'StringSwapper' that implements this interface and
4
+ binds the generic type to a 'string' (i.e., the StringSwapper class is not generically defined).
5
+
6
+ An object created from StringSwapper works in such a way that it always holds only one string value at a time.
7
+ The class should have:
8
+ A constructor that receives a string as a parameter.
9
+ An implementation of the swapValue method: the method returns the current value of the swapper and saves the new value passed as a parameter in its place.
10
+
11
+
12
+
13
+
14
+ Example of using the class:
15
+ public static void main(String[] args) {
16
+ StringSwapper swapper = new StringSwapper("hello");
17
+
18
+ System.out.println(swapper.swapValue("hi there"));
19
+ System.out.println(swapper.swapValue("greetings"));
20
+ System.out.println(swapper.swapValue("good day now"));
21
+ }
22
+
23
+ The program prints:
24
+ hello
25
+ hi there
26
+ greetings
27
+
28
+
29
+
30
+
31
+ ===============================================================
32
+
33
+
34
+ import java.util.Random;
35
+
36
+ public class Test {
37
+ public static void main(String[] args) {
38
+ final Random r = new Random();
39
+
40
+ System.out.println("Testing the StringSwapper class...");
41
+
42
+ String[] queues = "fox wolf pig mouse duck cow".split(" ");
43
+ String[] q2 = "hamster guinea pig gerbil giraffe elephant".split(" ");
44
+
45
+ String value = q2[r.nextInt(q2.length)];
46
+ System.out.println("Creating object with parameter " + value);
47
+
48
+
49
+ Swapper<String> swapper = new StringSwapper(value);
50
+ System.out.println("Object created!");
51
+
52
+ for (String queue : queues) {
53
+ System.out.println("Switching value to " + queue);
54
+ value = swapper.swapValue(queue);
55
+ System.out.println("Object returned previous value " + value);
56
+ }
57
+ }
58
+ }
59
+
60
+
61
+ interface Swapper<T> {
62
+ T swapValue(T value);
63
+ }
64
+
65
+
66
+ //ADD
67
+ class StringSwapper implements Swapper<String> {
68
+ private String value;
69
+
70
+ // constructor
71
+ public StringSwapper(String value) {
72
+ this.value = value;
73
+ }
74
+
75
+ @Override
76
+ public String swapValue(String value) {
77
+ // retrieve old value
78
+ String oldValue = this.value;
79
+
80
+ // update value
81
+ this.value = value;
82
+
83
+ // return/pop old value
84
+ return oldValue;
85
+ }
86
+ }
87
+
88
+
89
+
90
+ Testing the StringSwapper class...
91
+ Creating object with parameter elephant
92
+ Object created!
93
+ Switching value to fox
94
+ Object returned previous value elephant
95
+ Switching value to wolf
96
+ Object returned previous value fox
97
+ Switching value to pig
98
+ Object returned previous value wolf
99
+ Switching value to mouse
100
+ Object returned previous value pig
101
+ Switching value to duck
102
+ Object returned previous value mouse
103
+ Switching value to cow
104
+ Object returned previous value duck
105
+
106
+
107
+