Implements the queue interface

Assignment Help JAVA Programming
Reference no: EM13336981

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: EM13336981

Questions Cloud

Explain a tcp connection between host a and host b : Is it possible for an application to enjoy reliable data transfer even when the application runs over UDP? If so, please be specific in your answer of how this takes place.
Explain the magnitude and direction of the bond dipoles : Which geometry and central atom hybridization would you expect in the series BH4-, CH4, NH4+? (b)What would you expect for the magnitude and direction of the bond dipoles in this series
Calculate the total energy in the center-of-mass system : a proton with a momentum of 200 GeV/c collides with a proton at rest. Calculate the total energy in the center-of-mass system
How many times does the sound reflect back and forth : That is the sound from the organ bounces back and forth off the church walls many times before the sound inentsity decays away completely. If the reverberation time is 10s and the church has the approximate shape
Implements the queue interface : 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 - implementations as well as correct/fast implementations. In the former case, your functions may..
How much work is done on the astronaut : A helicopter lifts a 72 kg astronaut 19 m vertically from the ocean by means of a cable. The acceleration of the astronaut is g/12. How much work is done on the astronaut
Explain what mass of sodium cyanide must be added : What mass of sodium cyanide must be added to 250ml of water in order to obtain a solution having a pH of 10.50? Ka(HCN) = 4.9 x 10 ^-10
What is the volume and surface area of cubes after each cut : A cube is 1 cm X 1 cm X 1cm. It is cut 10 times along x,y,z, axis. What is the volume and surface area, and number of cubes after each cut
What is surface area and volume of each set of cubes made : A cube is 1 cm X 1 cm X 1cm. It is cut 10 times along x,y,z, axis. What is the surface area and volume of each set of cubes made

Reviews

Write a Review

JAVA Programming Questions & Answers

  Java application to permit user to enter ten numbers

Write down Java application which permits a user to enter 10 numbers (double precision) into the array and then sorts and displays numbers from lowest to highest.

  What is overloading and what is overriding

What is overloading and what is overriding? Please use code to explain it.

  Java application prompt user to put in integer from keyboard

Write a java application that performs the following task: prompt user to put in an integer from the keyboard, search for the user input from the array created in step 1.

  Simulate a simple version of a game called liar dice

The first player claims a Poker hand (see "Hand Ranks" below for legal calls). When making a claim, a player says what all five dice are, not just the name of the hand. For example, a player should say, "I have a full house: three 4s and two 5s," ..

  Create and code a program that uses three parallel numeric

Create and code a program that uses three parallel numeric arrays of size 6. The program searches one of the arrays and then displays the corresponding values from the other two arrays.

  Write an application that uses string method region

Write an application that uses String method region - Matches to compare two strings input by the user. The application should input the number of characters to be compared and the starting index of the comparison.

  Create the working applet to compute tax

Write down a java applet. Your applet assits to find out how much federal tax we require to pay suppose the tax rate is 12%. Create and implement the working applet to compute tax.

  Compilation and Execution - JAVA problem

Style marks are based on how your submitted code looks. Remember that good code is easily readable (and understandable) by someone who did not write it. For assignments, you should also include your name, student number, what course this is, what ass..

  Java tv''s newest owner! java tv is the greatest tv channel

Java TV's newest owner! Java TV is the greatest TV channel ever, and it prides itself in its 24 hour interesting ' programming'. To help you run this channel you will need to write a  dynamic  program that uses  Data Lists  to store all its TV show i..

  Write an application that uses an array

Write an application that uses an Array to store 10 messages of type String. You will store this Array with 10 messages of your choosing.

  Solve computer science java validation problem

Computer Science, Operating Systems - Year 1Description:java validationProblem: Using NetBeans 7.2

  Write a method called alldigitsodds that returns

Write a method called AllDigitsOdds that returns whether every digit of a positive integer is odd. Return true if the numbers consist entirely of odd digits (1,3,5,7,9)

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