Delete a random element from an arraylist

Assignment Help JAVA Programming
Reference no: EM13338610

1. In the archive, you will find the RandomQueue class, which implements the Queue interface in a way that, when we call remove()/poll(), a random element is removed from the queue. Currently, this is done by storing all the elements in an ArrayList and picking one at random to remove. Unfortunately, this slow, since deleting a random element from an ArrayList is slow because of all the shifting. For this question, you should modify the poll()method so that it runs in constant time.

Hint: The order that elements are stored in the ArrayList is not important, so you're free to reorder elements to make the deletion faster.

2. In the archive, you will find the BulkArrayDeque class, which is a subclass of ArrayDeque that has a slow implementation of the addAll(i,c) method. Look at this implementation, understand why it is so slow, and reimplement this method so that it runs in O(1+c.size() + n - i) time.
3.The ArrayDeque implementation given in the archive is pretty good, but it suffers a little from its use of modular arithmetic. Complete the ArrayDeque2 class that works like an ArrayDeque, but all the data always occupies positions a[j],a[j+1],...,a[j+n-1](notice that there is no modular arithmetic being done).

If ever the data runs off either end of a then the resize() operation is called to resize a to size 2*n and place the data at locations a[n/2],...,a[n/2+n-1].

Like an ArrayDeque, the add(i,x), and remove(i) operations should run in O(1+min{i,n-i}) amortized time.

4. This is another exercise in avoiding modular arithmetic. When a.length is a power of 2, i%a.length = i&(a.length-1). Using this fact, complete the ArrayDeque3 class in the implementation so that it uses & and - instead of %. The trick here is to change how and when a is resized so that a.length always remains a power of two, but the data structure still remains efficient.

Like an ArrayDeque, the add(i,x), and remove(i) operations should run in O(1+min{i,n-i}) amortized time.

5. Make a RootishArrayStack2 class that uses the same ideas as a RootishArrayStack, but is more efficient.

Instead of storing data in a sequence of r arrays b[0],...,b[r-1], it should store the data in a sequence of r bounded ArrayDeques (the BDeque class is provided in the assignment). If you implement this properly, the add(i,x) and remove(i) operations will run in O(1+min{n1/2, n-i}) time.

Hint: Using a BDeque means that most of the shifting that is done within the arrays of a RootishArrayStack can be replaced with remove(0), remove(size()-1), add(0), or add(size()-1).

Part II: Testing

Within the Tester class, write the following functions, all of which return true if all tests are successful or false if any test fails.

1. testPart1(q) that takes as input a Queue, q, and tests if it satisfies the requirements for Question 1.

This function should do all kinds of correctness tests on q.

This function should make a decent effort to test if the output order of the queue is indeed random.

This function should check that add(x) and remove(x) run in constant time.

2. testPart2(ad) function that tests if the addAll(i,c) operation works for the List ad and satisifies the requirements for Question 2:

This function should do all kinds of correctness tests on ad.

This function should test if the running time of the addAll(i,c) operation is indeed O(1+c.size() + n - i).

3. testPart3(ad) function that tests if a the list ad satisfies the requirements for Question 3:

This function should do all kinds of correctness tests.

This function should test that the performance of all operations is what it should be for an ArrayDeque

4. There is no Question 4, since the test for Question 3 also works for Question 4.

5. testPart5(ras) function that tests if the List ras satisifies the requirements for Question 5.

The function should do all kinds of correctness tests on ras.

The function should do its best to check that the running time of the add(i,x) and remove(i) operations is indeed O(1+min{n1/2, n-i}).
Some notes:

When applied to a correct and efficient implementation, your test functions should not take long to execute, no more than 2 seconds.

You can test elapsed times using System.nanoTime(). This is encapsulated by the Stopwatch class that can be found in the zip file; feel free to use that.

Your test functions will be run against buggy/slow implementations as well as correct/fast implementations. In the former case, your functions may return false, crash, or take more than 2 seconds to execute. In the latter case, your functions must return true and take less than 2 seconds to execute.

Attachment:- comp.zip

Reference no: EM13338610

Questions Cloud

Determine what is the weight of the motor : Two people carry a heavy electric motor by placing it on a light board 2.00 m long. One person lifts at one end with a force of 100 N, and the other lifts the opposite end with a force of 900 N.
What angle does the exiting ray make : A light ray in the core (n = 1.32) of a cylindrical optical fiber (the figure below) travels at an angle ?1 = 45.9° with respect to the axis of the fiber. What angle ?2 does the exiting ray make with the outside surface of the cladding
How much work did the weightlifter perform : A weightlifter lifts 200kg from the floor to a height of 2m in 2s. How much work did the weightlifter perform
What is the increase in the temperature of the water : Consider Joule's experiment described in the insight below. Assume the mass m = 2.5 kg and the fluid is water with a volume of 1.19 L. What is the increase in the temperature of the water
Delete a random element from an arraylist : Elements in an ArrayList and picking one at random to remove. Unfortunately, this slow, since deleting a random element from an ArrayList is slow because of all the shifting. For this question, you should modify the poll()method so that it runs in..
What are the instruction format : What are the instruction format and the equivalent assembly instruction of 0x21080040(with steps)?
Calculate the q value in mev for the double beta decay : Calculate the Q value in MeV for the double Beta decay of 128Te (atomic mass 127.904461 a.m.u.) to 128Xe (atomic mass 127.903531 a.m.u.).
What is the specific heat of the material : While working in a research lab, you find that it takes 211 J of heat to increase the temperature of 1.53 kg of a material by 44 K
Find the final temperature of the system : You drop 470 g of solid ice at ?35°C into a bucket of water (1,200 g) at 45°C. Find the final temperature of the system

Reviews

Write a Review

JAVA Programming Questions & Answers

  Finding java code error

Indicate in the given code segment if an error occurs. If so, point to whether it is syntax, runtime or logic and how you would fix it.

  Write java program to reverse contents of original array

Write down the Java program method named reversal which returns the new array which is a reversal of original array. Use [5.0, 4.4, 1.9, 2.9, 3.4, 3.5] to test method.

  A remote web-based file storage application

Develop a web-based application. The server-side part of the application must be developed using Java Servlets (possibly including JSP). The development of the client (browser) side will typically be a combination of HTML and Javascript.

  Redesign the grading program as an object-oriented design

redesign the grading program as an object-oriented design. We will start with a simple Student class. The student will have a name, an id (as a String) and a set of grades (as doubles)

  Check wether two appointments overlap

in a scheduling program, we want to check wether two appointments overlap. For simplicity, appointments start at a full hour,

  Write program in java-calculate and display mortgage payment

Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage.

  Object from question 1 manage its display

Write a Java statement to have the object from question 1 manage its display area by dividing it into 3 rows which have 4 columns each.

  The ai player will not be asking

The AI player will not be asking the user for any input. This means that placeShips() and fire() should not ask the user for locations. The AI player should choose locations of ships and automatically place them

  Pronouns, and prepositions

Type addKeyword(): for each word on each page, add to a (keyword, URLs) list or dictionary, excluding articles, pronouns, and prepositions. For example:

  Hypothetical situation

Hypothetical Situation - A company lowers its price of an economy car a small amount   ($15,000 to $14,000) Sales go from 10,000 units to 50,000 units. Another company lowers its price on a luxury car a large amount ($80,000 to $40,000). Its sales go..

  Write a program to simulate the behavior of the philosophers

Write a program to simulate the behavior of the philosophers, where each philosopher is a thread and the chopsticks are shared objects.

  What aspects of judaism are rejected or reinterpreted

What theology does Jesus present in the two excerpts from the Gospels that are a part of the readings? What aspects of Judaism are strongly represented? What aspects of Judaism are rejected or reinterpreted?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd