Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose of this assignment is to practice using the Java ArrayList class and the methods described below. ArrayList and Random are both in java.util

The purpose of this assignment is to practice using the Java ArrayList class and the methods described below. ArrayList and Random are both in java.util

  1. Write a Java program that will create an ArrayList of size 10 that holds integers. Use a random number generator to add 8 integers to an ArrayList called list1 so that after each number is added to list1 it is sorted from largest (at index 0)) to smallest. Each number you add to list1 must be placed in the correct spot using a linear search to find the right spot then add the number there. You must use the add(index, element), get(index) and size( ) methods to perform the search and then to place the element. Display the contents of list1 after each number is added to verify that list1 is sorted each time. (This will help you discover errors in your code) You can do this using the toString() method from the ArrayList class.

  1. Create list2 also with 8 elements sorted from largest to smallest by repeating step #1 above. This time you dont need to print out list2 after each number youve added. Just print it out once after all 8 numbers have been added.

  1. Remove the numbers from list2, one at a time, using the remove(index) method, and add them to list1 so that list1 is always sorted from largest to smallest. You must keep track of where you added the last number in list1, using a here variable. Add the second number from list2 to list1 by starting at here described above. Continue to move numbers from list2 to list1 in this fashion. Print out list1 and list2 once you are done. Note that list1 will automatically grow bigger to accommodate the additional numbers.

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_2

Step: 3

blur-text-image_3

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

Generative Artificial Intelligence For Project Management With Aws

Authors: Timothy Krimmel

1st Edition

B0CQV9KWB8, 979-8872627197

More Books

Students also viewed these Databases questions

Question

Calculate mean from the formula

Answered: 1 week ago

Question

Explain walter's model of dividend policy.

Answered: 1 week ago