The NumArrayList class has a single private instance variable L of type ArrayLis
ID: 3844121 • Letter: T
Question
The NumArrayList class has a single private instance variable L of type ArrayList<T> where T is a type that extends the abstract class Number. For example, both Integer and Double extend Number.
You will complete the assignment by writing the following methods for the NumArrayList class:
public NumArrayList(): The default constructor for the class. It instantiates the internal ArrayList L in the NumArrayList object by making an appropriate call to the ArrayList constructor.
public NumArrayList(T[] initValues): The parameterized constructor for the class. It instantiates the internal ArrayList L in the NumArrayList object by making an appropriate call to the ArrayList constructor. It then places the values in the array initValues into the new ArrayList object L.
public void add(T elem): The method adds the parameter to the internal ArrayList object L.
public T get(int index): The method returns the element found in the internal ArrayList object L in position index.
public String toString(): This method returns a String containing the elements found in the internal ArrayList object L. The elements should be separated by a space and the String should end with a newline.
public NumArrayList<Integer> iMult(Integer val): The method returns a new NumArrayList object containing Integers which is created by taking the calling NumArrayList object and multiplying each of the values found in its internal ArrayList by val. You will need to use the intValue() method of the T class in order to implement this method.
I have provided a driver program (e.g. a main method that uses the NumArrayList class) TestNumArrayList.java to help you test your code which has been posted to D2L. Be aware that the grader may add to that program, so if you feel that any of your methods haven't been tested effectively by the driver program, please modify as necessary. However, you should only submit the NumArrayList.java class.
Below is the sample output that the test program produces for my solution to the NumArrayList class. Be aware that the test program puts random numbers into the lists, so the actual values you see when you run this likely will differ from what you see below:
The Integer NumArrayList: 96 98 97 2 38 66 81 31 50 48
The Double NumArrayList:
95.35
35.28
90.41
37.68
2.99
11.11
22.40
94.84
28.25
46.84
The result of calling iMult: 960 980 970 20 380 660 810 310 500 480
TestNumArrayList.java
public class TestNumArrayList {
public static final int arrSize = 10;
public static void main(String[] args) {
Integer[] iarr = new Integer[arrSize];
for (int i = 0; i < arrSize; i++)
iarr[i] = new Integer(1 + (int) (100 * Math.random()));
NumArrayList<Integer> iArrLst = new NumArrayList<>(iarr);
System.out.println("The Integer NumArrayList: " + iArrLst);
NumArrayList<Double> dArrLst = new NumArrayList<>();
for (int i = 0; i < arrSize; i++)
dArrLst.add(new Double(100* Math.random()));
System.out.println("The Double NumArrayList: ");
// Put this code back in when you write the get method
/*for (int i = 0; i < arrSize; i++)
System.out.printf("%.2f ", dArrLst.get(i));
System.out.println();*/
NumArrayList<Integer> secIArrLst = iArrLst.iMult(arrSize);
System.out.println("The result of calling iMult: " + secIArrLst);
}
}
NumArrayList.java
import java.util.ArrayList;
public class NumArrayList<T extends Number>{
private ArrayList<T> L;
public NumArrayList() {
}
// Create a new NumArrayList, initializing it with the parameter
public NumArrayList(T[] initValues) {
}
public void add(T elem) {
}
// Uncomment out and write this method
// There isn't a reasonable stub I can put in it
//public T get(int index) {
//
//}
// Return the String representation of the NumArrayList
public String toString() {
// replace this with the correct implementation
return "";
}
// Multiply the NumArrayList by an Integer value
public NumArrayList<Integer> iMult(Integer val) {
// replace this with the correct implementation
NumArrayList<Integer> iItem = new NumArrayList<Integer>();
// Put your code to fill the NumArrayList iItem here
return iItem;
}
}
Explanation / Answer
Below is your code: -
TestNumArrayList.java
public class TestNumArrayList {
public static final int arrSize = 10;
public static void main(String[] args) {
Integer[] iarr = new Integer[arrSize];
for (int i = 0; i < arrSize; i++)
iarr[i] = new Integer(1 + (int) (100 * Math.random()));
NumArrayList<Integer> iArrLst = new NumArrayList<>(iarr);
System.out.println("The Integer NumArrayList: " + iArrLst);
NumArrayList<Double> dArrLst = new NumArrayList<>();
for (int i = 0; i < arrSize; i++)
dArrLst.add(new Double(100 * Math.random()));
System.out.println("The Double NumArrayList: ");
for (int i = 0; i < arrSize; i++)
System.out.printf("%.2f ", dArrLst.get(i));
System.out.println();
NumArrayList<Integer> secIArrLst = iArrLst.iMult(arrSize);
System.out.println("The result of calling iMult: " + secIArrLst);
}
}
NumArrayList.java
import java.util.ArrayList;
import java.util.Arrays;
public class NumArrayList<T extends Number> {
private ArrayList<T> L;
public NumArrayList() {
L = new ArrayList<>();
}
// Create a new NumArrayList, initializing it with the parameter
public NumArrayList(T[] initValues) {
L = new ArrayList<>();
L.addAll(Arrays.asList(initValues));
}
public void add(T elem) {
L.add(elem);
}
// Uncomment out and write this method
// There isn't a reasonable stub I can put in it
public T get(int index) {
return L.get(index);
}
// Return the String representation of the NumArrayList
public String toString() {
// replace this with the correct implementation
String s = "";
for (int i = 0; i < L.size(); i++) {
s = s + L.get(i) + " ";
}
return s;
}
// Multiply the NumArrayList by an Integer value
public NumArrayList<Integer> iMult(Integer val) {
// replace this with the correct implementation
NumArrayList<Integer> iItem = new NumArrayList<Integer>();
for (int i = 0; i < L.size(); i++) {
if (L.get(i).getClass() == Integer.class) {
iItem.add(val * (Integer) L.get(i));
} else if (L.get(i).getClass() == Double.class) {
iItem.add((Double.valueOf(val * (Double) L.get(i)).intValue()));
}
}
// Put your code to fill the NumArrayList iItem here
return iItem;
}
}
Sample Run: -
The Integer NumArrayList: 74 70 15 54 94 29 40 54 84 37
The Double NumArrayList:
26.46
28.05
94.05
87.05
82.60
40.53
3.98
6.26
24.03
32.90
The result of calling iMult: 740 700 150 540 940 290 400 540 840 370
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.