Question

In: Computer Science

blueJ code given: import java.until.ArrayList; public class TestArrayList; { private ArrayList<TestArrays> testArrayList; /** * Complete the...

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()

{

}

}

Solutions

Expert Solution

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();
    }

}

================================================================


Related Solutions

Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private E[] queue;    private int front = 0, rear = 0;    private static final int DEFAULT_CAPACITY = 5;       public CircularQueue(int capacity)    {    queue = (E[]) new Object[capacity + 1];    }       public CircularQueue()    {        this(DEFAULT_CAPACITY); }       //Add a method that will determine if the queue is empty. Recall that the queue is...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT