In: Computer Science
blueJ code given:
import java.until.ArrayList;
public class TestArrayList;
{
private ArrayList<TestArrays> testArrayList;
/**
* Complete the constructor for the class. Instantiate the testArrayList
*and call the fillArrayList method to add objects to the testArrayList.
* @param numElements The number of elements in the ArrayList
*/
public TestArrayLists(int numElements)
{
}
/**
* Complete the fillArrayList method. It should fill the testArrayList with
* TestArrays objects consisting of 10 element int Array of random numbers.
* @param numElements The number of elements in the ArrayList
*/
private void fillArrayList(int numElements)
{
}
/**
* For each TestArrays object in the testArrayList,
*print out the numbers in the int Array.
*/
public void printArrayList()
{
}
}
Below are the 2 classes you will be needing, I have kept the main() method inside the TestArrayList.java class, you can run this class and see the output.
===========================================================================
import java.util.Random;
public class TestArrays {
private int[] numbers;
public TestArrays(int size) {
numbers = new int[size];
Random random = new Random();
for (int i = 0; i < size; i++) numbers[i] = random.nextInt(100) + 1;
}
public void display() {
for (int i = 0; i < numbers.length; i++) {
System.out.printf("%5d", numbers[i]);
}
System.out.println();
}
}
================================================================
import java.util.ArrayList;
public class TestArrayList {
private ArrayList<TestArrays> testArrayList;
/**
* Complete the constructor for the class. Instantiate the testArrayList
* <p>
* and call the fillArrayList method to add objects to the testArrayList.
*
* @param numElements The number of elements in the ArrayList
*/
public TestArrayList(int numElements) {
testArrayList = new ArrayList<>();
fillArrayList(numElements);
}
/**
* Complete the fillArrayList method. It should fill the testArrayList with
* TestArrays objects consisting of 10 element int Array of random numbers.
*
* @param numElements The number of elements in the ArrayList
*/
private void fillArrayList(int numElements) {
for (int i = 0; i < 10; i++) {
testArrayList.add(new TestArrays(numElements));
}
}
/**
* For each TestArrays object in the testArrayList,
* print out the numbers in the int Array.
*/
public void printArrayList() {
for (TestArrays testArrays : testArrayList) {
testArrays.display();
;
}
}
public static void main(String[] args) {
TestArrayList list = new TestArrayList(10);
list.printArrayList();
}
}
================================================================
