Spaces:
Running
Running
TurkuBasicOOPinJava / Week 7: Enum, Generic Type, Streams, write to file, class diagram /06B. Implement the StringSwapper Class
| In the program, a 'generic interface class' 'Swapper' is defined. | |
| Write a class 'StringSwapper' that implements this interface and | |
| binds the generic type to a 'string' (i.e., the StringSwapper class is not generically defined). | |
| An object created from StringSwapper works in such a way that it always holds only one string value at a time. | |
| The class should have: | |
| A constructor that receives a string as a parameter. | |
| 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. | |
| Example of using the class: | |
| public static void main(String[] args) { | |
| StringSwapper swapper = new StringSwapper("hello"); | |
| System.out.println(swapper.swapValue("hi there")); | |
| System.out.println(swapper.swapValue("greetings")); | |
| System.out.println(swapper.swapValue("good day now")); | |
| } | |
| The program prints: | |
| hello | |
| hi there | |
| greetings | |
| =============================================================== | |
| import java.util.Random; | |
| public class Test { | |
| public static void main(String[] args) { | |
| final Random r = new Random(); | |
| System.out.println("Testing the StringSwapper class..."); | |
| String[] queues = "fox wolf pig mouse duck cow".split(" "); | |
| String[] q2 = "hamster guinea pig gerbil giraffe elephant".split(" "); | |
| String value = q2[r.nextInt(q2.length)]; | |
| System.out.println("Creating object with parameter " + value); | |
| Swapper<String> swapper = new StringSwapper(value); | |
| System.out.println("Object created!"); | |
| for (String queue : queues) { | |
| System.out.println("Switching value to " + queue); | |
| value = swapper.swapValue(queue); | |
| System.out.println("Object returned previous value " + value); | |
| } | |
| } | |
| } | |
| interface Swapper<T> { | |
| T swapValue(T value); | |
| } | |
| //ADD | |
| class StringSwapper implements Swapper<String> { | |
| private String value; | |
| // constructor | |
| public StringSwapper(String value) { | |
| this.value = value; | |
| } | |
| @Override | |
| public String swapValue(String value) { | |
| // retrieve old value | |
| String oldValue = this.value; | |
| // update value | |
| this.value = value; | |
| // return/pop old value | |
| return oldValue; | |
| } | |
| } | |
| Testing the StringSwapper class... | |
| Creating object with parameter elephant | |
| Object created! | |
| Switching value to fox | |
| Object returned previous value elephant | |
| Switching value to wolf | |
| Object returned previous value fox | |
| Switching value to pig | |
| Object returned previous value wolf | |
| Switching value to mouse | |
| Object returned previous value pig | |
| Switching value to duck | |
| Object returned previous value mouse | |
| Switching value to cow | |
| Object returned previous value duck | |