Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer in Java For this question you must implement a BlockedList class that implements the List interface. You may use any of the classes

Please answer in Java

For this question you must implement a BlockedList class that implements the List interface. You may use any of the classes in JCF or in the textbook code. The constructor for this class takes an integer block size b and the implementation should have the following performance characteristics: a) get(i) and set(i,x) should run in O(1) time per operation b) add(i,x) and remove(i) should run in O(b+ min{i, n-i}/b) amortized time per operation. Any solution matching this specification is acceptable. However, the runtime would suggest a data structure that contains other data structures, or blocks of size at most b. For one possible solution, recall that an array backed Deque (an ArrayDeque in the textbook, or a Deque in the JCF) implements add(i,x) and remove(i) in O(min{i, n-i}) amortized time per operation. We want a data structure with performance characteristics similar to an ArrayDeque (within a factor of b). We might therefore consider an ArrayDeque of data structures. It is then left to you to determine the inner data structure. Also note, if we choose a block size of b=1, then we simply have an ArrayDeque running in O(b+ min{i, n-i}/b) = O(min{i, n-i}) amortized time per operation, as required, regardless of the inner data structure chosen.

import java.util.AbstractList; import java.util.ArrayDeque; import java.util.Collection; import java.util.Deque; /**  * @author morin  *  * @param <T> the type of objects stored in the List  */ public class BlockedList extends AbstractList { /**  * keeps track of the class of objects we store  */  Factory f; /**  * The number of elements stored  */  int n; /**  * The block size  */  int b; /**  * Constructor  * @param t the type of objects that are stored in this list  * @param b the block size  */  public BlockedList(Class t, int b) { f = new Factory(t); n = 0; // TODO: Implement this  } public int size() { return n; } public T get(int i) { // TODO: Implement this  if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); int s = i/b; int j = i%b; return blocks.get(s)[j]; } public T set(int i, T x) { // TODO: Implement this  if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); return null; } public void add(int i, T x) { // TODO: Implement this  if (i < 0 || i > n) throw new IndexOutOfBoundsException(); } public T remove(int i) { // TODO: Implement this  if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); return null; } } 

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

Step: 3

blur-text-image

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

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions

Question

1. Describe the types of power that effective leaders employ

Answered: 1 week ago