Question: Hello please read the question properly and provide appropriate code answer to it. and show result on screen that the code works. It should shay

Hello please read the question properly and provide appropriate code answer to it. and show result on screen that the code works. It should shay true after compiling. all the classes required are given. This requires java to solve.

Question

Implement a Treque (triple-ended queue). This is an implementation of the List interface that allows for fast modifications at the front, back, and middle. You can make your Treque a subclass of AbstractList so that you don't have to write all the List methods from scratch. The performance requirements for a Treque are:

size(), get(i), and set(i,x) should each take constant time (O(1)).

add(i,x) and remove(i) should be fast if the value of i is close to 0, n or n/2. More precisely, the amortized running time of these operations should be O(1 + min{i, n-i, |n/2-i|}).

Testing Part

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

testPart1(ell) that takes as input a List called ell, and tests if it satisfies the requirements for Question 1:

This function should do all kinds of correctness tests on ell

This function should check that all methods run in the specified time. In particular, it should check that add(i,x) and remove(i) are fast when O(1 + min{i, n-i, |n/2 - i|}) is small.

Treque.java

package comp2402a2;

import java.util.AbstractList; import java.util.List;

/** */ public class Treque extends AbstractList { /** * You decide on the instance variables you need. */

public Treque(Class t) { // Put your own code here throw new UnsupportedOperationException("Constructor not yet implemented"); }

public T get(int i) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here instead of throwing this exception throw new UnsupportedOperationException("get(i) not yet implemented"); }

public T set(int i, T x) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here instead of throwing this exception throw new UnsupportedOperationException("set(i,x) not yet implemented"); }

public void add(int i, T x) { if (i < 0 || i > size()) throw new IndexOutOfBoundsException(); // Put your own code here throw new UnsupportedOperationException("add(i,x) not yet implemented"); }

public T remove(int i) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here throw new UnsupportedOperationException("remove(i) not yet implemented"); }

public int size() { // Put your own code here throw new UnsupportedOperationException("size() not yet implemented"); }

public static void main(String[] args) { //List tr = new ArrayDeque(Integer.class); List tr = new Treque(Integer.class); int K = 1000000; Stopwatch s = new Stopwatch(); System.out.print("Appending " + K + " items..."); System.out.flush(); s.start(); for (int i = 0; i < K; i++) { tr.add(i); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)");

System.out.print("Prepending " + K + " items..."); System.out.flush(); s.start(); for (int i = 0; i < K; i++) { tr.add(0, i); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)");

System.out.print("Midpending(?!) " + K + " items..."); System.out.flush(); s.start(); for (int i = 0; i < K; i++) { tr.add(tr.size()/2, i); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)");

System.out.print("Removing " + K + " items from the back..."); System.out.flush(); s.start(); for (int i = 0; i < K; i++) { tr.remove(tr.size()-1); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)");

System.out.print("Removing " + K + " items from the front..."); System.out.flush(); s.start(); for (int i = 0; i < K; i++) { tr.remove(0); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)");

System.out.print("Removing " + K + " items from the middle..."); System.out.flush(); s.start(); for (int i = 0; i < K; i++) { tr.remove(tr.size()/2); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); }

}

Abstraction Table

package comp2402a2;

/** */ public interface AbstractTable { /** * You decide on the instance variables you need. */

public int rows(); public int cols(); public T get(int i, int j); public T set(int i, int j, T x); public void addRow(int i); public void removeRow(int i); public void addCol(int j); public void removeCol(int j); public String toString(); }

Tester.java

package comp2402a2; import java.util.List;

/** */ public class Tester { public static boolean testPart1(List ell) { return true; }

public static boolean testPart2(List ell) { return true; }

public static boolean testPart3(AbstractTable ell) { return true; }

}

Stopwatch.java

package comp2402a2;

public class Stopwatch { protected long start, stop; public void start() { start = System.nanoTime(); } public void stop() { stop = System.nanoTime(); } public double elapsedSeconds() { return (stop-start)*1e-9; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!