KaiquanMah commited on
Commit
b415662
·
verified ·
1 Parent(s): 731d0fd

public Constructor(T param){...}; public T method1() {...}

Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/04B. Generic Duplicator ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Write a 'generically type-defined class'
2
+
3
+ Duplicator<T>
4
+
5
+ The class should have the following properties:
6
+ 1
7
+ A constructor that takes an element of type T as a parameter.
8
+ 2
9
+ Set and get methods for the element (getElement and setElement).
10
+ 3
11
+ A method ArrayList<T> getMany(int amount), which returns a new list.
12
+ 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.
13
+
14
+
15
+ An example of utilizing the class:
16
+
17
+ public static void main(String[] args) {
18
+ Duplicator<String> d1 = new Duplicator<>("abc");
19
+ System.out.println(d1.getMany(3));
20
+
21
+ Duplicator<Double> d2 = new Duplicator<>(2.5);
22
+ System.out.println(d2.getMany(4));
23
+ }
24
+
25
+
26
+
27
+
28
+ The program prints:
29
+ [abc, abc, abc]
30
+ [2.5, 2.5, 2.5, 2.5]
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+ import java.util.Random;
42
+ import java.util.ArrayList;
43
+
44
+ public class Test {
45
+ public static void main(String[] args) {
46
+ final Random r = new Random();
47
+
48
+ System.out.println("Testing the Duplicator class...");
49
+
50
+ System.out.println("Testing with integer type...");
51
+ int element = r.nextInt(100) + 1;
52
+ System.out.println("Element value: " + element);
53
+ Duplicator<Integer> d1 = new Duplicator<>(element);
54
+ System.out.println("Object created!");
55
+
56
+ System.out.println("getElement returns " + d1.getElement());
57
+ element = r.nextInt(100) + 1;
58
+ System.out.println("Calling setElement with value " + element);
59
+ d1.setElement(element);
60
+ System.out.println("getElement returns " + d1.getElement());
61
+
62
+ int size = r.nextInt(6) + 2;
63
+ System.out.println("Calling getMany with value " + size);
64
+ ArrayList<Integer> al = d1.getMany(size);
65
+ System.out.println(al);
66
+
67
+ System.out.println("");
68
+
69
+ System.out.println("Testing with string type...");
70
+ String[] s = "dog cat guinea pig hamster cow sheep chicken guinea pig".split(" ");
71
+ String sElement = s[r.nextInt(s.length)];
72
+ System.out.println("Element value: " + sElement);
73
+ Duplicator<String> d2 = new Duplicator<>(sElement);
74
+ System.out.println("Object created!");
75
+
76
+ System.out.println("getElement returns " + d2.getElement());
77
+ sElement = s[r.nextInt(s.length)];
78
+ System.out.println("Calling setElement with value " + sElement);
79
+ d2.setElement(sElement);
80
+ System.out.println("getElement returns " + d2.getElement());
81
+
82
+ size = r.nextInt(6) + 2;
83
+ System.out.println("Calling getMany with value " + size);
84
+ ArrayList<String> al2 = d2.getMany(size);
85
+ System.out.println(al2);
86
+ }
87
+ }
88
+
89
+
90
+
91
+
92
+
93
+ //ADD
94
+ class Duplicator<T> {
95
+ // attribute
96
+ private T singleInput;
97
+
98
+ // constructor
99
+ public Duplicator(T singleInput){
100
+ this.singleInput = singleInput;
101
+ }
102
+
103
+ // get, set
104
+ public T getElement() {
105
+ return this.singleInput;
106
+ }
107
+
108
+ public void setElement(T singleInput) {
109
+ this.singleInput = singleInput;
110
+ }
111
+
112
+ // duplicate this.singleInput
113
+ // by 'amount' times
114
+ // in the ArrayList
115
+ public ArrayList<T> getMany(int amount) {
116
+ ArrayList<T> list = new ArrayList<>();
117
+ for (int i=0; i<amount; i++) {
118
+ list.add(this.singleInput);
119
+ }
120
+
121
+ return list;
122
+ }
123
+ }
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+ Testing the Duplicator class...
132
+ Testing with integer type...
133
+ Element value: 94
134
+ Object created!
135
+ getElement returns 94
136
+ Calling setElement with value 27
137
+ getElement returns 27
138
+ Calling getMany with value 4
139
+ [27, 27, 27, 27]
140
+
141
+
142
+
143
+ Testing with string type...
144
+ Element value: sheep
145
+ Object created!
146
+ getElement returns sheep
147
+ Calling setElement with value guinea
148
+ getElement returns guinea
149
+ Calling getMany with value 2
150
+ [guinea, guinea]
151
+
152
+