Spaces:
Running
Running
File size: 3,710 Bytes
b415662 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 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]
|