Spaces:
Running
Running
TurkuBasicOOPinJava / Week 7: Enum, Generic Type, Streams, write to file, class diagram /04B. Generic Duplicator
| Write a 'generically type-defined class' | |
| Duplicator<T> | |
| The class should have the following properties: | |
| 1 | |
| A constructor that takes an element of type T as a parameter. | |
| 2 | |
| Set and get methods for the element (getElement and setElement). | |
| 3 | |
| A method ArrayList<T> getMany(int amount), which returns a new list. | |
| The size of the list is the integer given as a parameter, and each element of the list is the element stored in the object. | |
| An example of utilizing the class: | |
| public static void main(String[] args) { | |
| Duplicator<String> d1 = new Duplicator<>("abc"); | |
| System.out.println(d1.getMany(3)); | |
| Duplicator<Double> d2 = new Duplicator<>(2.5); | |
| System.out.println(d2.getMany(4)); | |
| } | |
| The program prints: | |
| [abc, abc, abc] | |
| [2.5, 2.5, 2.5, 2.5] | |
| import java.util.Random; | |
| import java.util.ArrayList; | |
| public class Test { | |
| public static void main(String[] args) { | |
| final Random r = new Random(); | |
| System.out.println("Testing the Duplicator class..."); | |
| System.out.println("Testing with integer type..."); | |
| int element = r.nextInt(100) + 1; | |
| System.out.println("Element value: " + element); | |
| Duplicator<Integer> d1 = new Duplicator<>(element); | |
| System.out.println("Object created!"); | |
| System.out.println("getElement returns " + d1.getElement()); | |
| element = r.nextInt(100) + 1; | |
| System.out.println("Calling setElement with value " + element); | |
| d1.setElement(element); | |
| System.out.println("getElement returns " + d1.getElement()); | |
| int size = r.nextInt(6) + 2; | |
| System.out.println("Calling getMany with value " + size); | |
| ArrayList<Integer> al = d1.getMany(size); | |
| System.out.println(al); | |
| System.out.println(""); | |
| System.out.println("Testing with string type..."); | |
| String[] s = "dog cat guinea pig hamster cow sheep chicken guinea pig".split(" "); | |
| String sElement = s[r.nextInt(s.length)]; | |
| System.out.println("Element value: " + sElement); | |
| Duplicator<String> d2 = new Duplicator<>(sElement); | |
| System.out.println("Object created!"); | |
| System.out.println("getElement returns " + d2.getElement()); | |
| sElement = s[r.nextInt(s.length)]; | |
| System.out.println("Calling setElement with value " + sElement); | |
| d2.setElement(sElement); | |
| System.out.println("getElement returns " + d2.getElement()); | |
| size = r.nextInt(6) + 2; | |
| System.out.println("Calling getMany with value " + size); | |
| ArrayList<String> al2 = d2.getMany(size); | |
| System.out.println(al2); | |
| } | |
| } | |
| //ADD | |
| class Duplicator<T> { | |
| // attribute | |
| private T singleInput; | |
| // constructor | |
| public Duplicator(T singleInput){ | |
| this.singleInput = singleInput; | |
| } | |
| // get, set | |
| public T getElement() { | |
| return this.singleInput; | |
| } | |
| public void setElement(T singleInput) { | |
| this.singleInput = singleInput; | |
| } | |
| // duplicate this.singleInput | |
| // by 'amount' times | |
| // in the ArrayList | |
| public ArrayList<T> getMany(int amount) { | |
| ArrayList<T> list = new ArrayList<>(); | |
| for (int i=0; i<amount; i++) { | |
| list.add(this.singleInput); | |
| } | |
| return list; | |
| } | |
| } | |
| Testing the Duplicator class... | |
| Testing with integer type... | |
| Element value: 94 | |
| Object created! | |
| getElement returns 94 | |
| Calling setElement with value 27 | |
| getElement returns 27 | |
| Calling getMany with value 4 | |
| [27, 27, 27, 27] | |
| Testing with string type... | |
| Element value: sheep | |
| Object created! | |
| getElement returns sheep | |
| Calling setElement with value guinea | |
| getElement returns guinea | |
| Calling getMany with value 2 | |
| [guinea, guinea] | |