226k views
1 vote
Create a data file with 12 integers named Integers.dat. The recommended project name is Lab9L1ArrayListofIntegers. The package name is arrayListOfIntegers and the only class that you will create for this lab is named Lab9L1.

Create a small program that reads in an ARBITRARY number of integers (that is, ANY number of integers) from a file named Integers.dat. The program must use an ArrayList and in order to receive full credit, must use the diamond operator to create an ArrayList of Integer. Use an enhanced for loop to output each element of the array, then use an implicit call to ArrayList's toString() method to print out the list.

User Mogzol
by
7.5k points

1 Answer

3 votes

Final answer:

The question asks for the creation of a Java program named Lab9L1 that reads integers from a file into an ArrayList and prints the contents. The program should use the diamond operator and an enhanced for loop for displaying the list of integers, followed by an implicit toString() method call.

Step-by-step explanation:

The student's question involves creating a Java program that reads an arbitrary number of integers from a file called Integers.dat, storing them in an ArrayList of Integers, and then printing out each element using an enhanced for loop. The program must adhere to a specific naming convention for the project, package, class, and use the diamond operator while declaring the ArrayList. Lastly, the program should invoke the toString() method implicitly to output the entire list.

Step-by-Step Explanation:

  1. Create a file named Integers.dat containing 12 integers, with one integer per line.
  2. Create a Java project named Lab9L1ArrayListofIntegers with a package named arrayListOfIntegers.
  3. Within this package, create a class named Lab9L1.
  4. In the Lab9L1 class, write Java code to read from Integers.dat using a Scanner or FileReader.
  5. Declare an ArrayList of Integer using the diamond operator, for example, ArrayList<Integer> list = new ArrayList<>();
  6. Read the file contents and add each integer to the ArrayList using list.add().
  7. Use an enhanced for loop to iterate through the ArrayList and print each element.
  8. Finally, print out the entire ArrayList using the implicit toString() call.

User Anjan Baradwaj
by
7.9k points