Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

System.out.println();*/

NumArrayList secIArrLst = iArrLst.iMult(arrSize);

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

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

What is meant by the opportunity dimension of the AMO formula?

Answered: 1 week ago