The numarraylist class has a single private instance


The NumArrayList class has a single private instance variable L of type ArrayList 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 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.

import java.util.ArrayList;

public class NumArrayList{

private ArrayList L;

public NumArrayList() {

}

// Create a new NumArrayList, initializing it with the parameter

public NumArrayList(T[] initValues) {

}

public void add(T elem) {

}

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 iMult(Integer val) {

// replace this with the correct implementation

NumArrayList iItem = new NumArrayList();

// Put your code to fill the NumArrayList iItem here

return iItem;

}

}

TEST

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 iArrLst = new NumArrayList<>(iarr);

System.out.println("The Integer NumArrayList: " + iArrLst);

NumArrayList 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\n", dArrLst.get(i));

System.out.println();*/

NumArrayList secIArrLst = iArrLst.iMult(arrSize);

System.out.println("The result of calling iMult: " + secIArrLst);

}

}

Request for Solution File

Ask an Expert for Answer!!
Computer Engineering: The numarraylist class has a single private instance
Reference No:- TGS02877929

Expected delivery within 24 Hours