Question

In: Computer Science

Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial...

Language: Java

Topic: Deques

Using the following variables/class:

public class ArrayDeque<T> {

/**
* The initial capacity of the ArrayDeque.
*
* DO NOT MODIFY THIS VARIABLE.
*/
public static final int INITIAL_CAPACITY = 11;

// Do not add new instance variables or modify existing ones.
private T[] backingArray;
private int front;
private int size;

Q1: write a method called "public void addLast(T data)" which adds an element to the back of a Deque. If sufficient space is not available in the backing array, resize it to double the current capacity. When resizing, copy elements to the beginning of the new array and reset front to 0. Must be amortized O(1).
  
* @param data the data to add to the back of the deque
* @throws java.lang.IllegalArgumentException if data is null

Q2: write a method called "public T removeFirst()" that removes and returns the first element of the deque. Do not grow or shrink the backing array. If the deque becomes empty as a result of this call, do not reset front to 0.Replace any spots that you remove from with null. Failure to do so can result in the loss of points. Must be in O(1) and:

* @return the data formerly located at the front of the deque
* @throws java.util.NoSuchElementException if the deque is empty

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//method to add an element to the last

public void addLast(T data) {

      // checking if array is full

      if (size == backingArray.length) {

            // creating an array of twice capacity

            T[] temp = (T[]) new Object[backingArray.length * 2];

            int index = front;

            // copying elements from old array to new array

            for (int i = 0; i < size; i++) {

                  temp[i] = backingArray[index];

                  index++;

                  if (index >= backingArray.length) {

                        // wrapping around

                        index = 0;

                  }

            }

            // replacing old array with new

            backingArray = temp;

            // resetting front to 0

            front = 0;

      }

      // (front + size) % backingArray.length will return the index of next

      // available location, adding data to this index

      backingArray[(front + size) % backingArray.length] = data;

      // updating size

      size++;

}

//remove and return element at front

public T removeFirst() {

      // throwing exception if dequeue is empty

      if (size == 0) {

            throw new NoSuchElementException("Empty Dequeue!");

      }

      // storing element at front

      T data = backingArray[front];

      // setting element at front index to null

      backingArray[front] = null;

      // updating front index, wrapping around from 0 if necessary.

      front = (front + 1) % backingArray.length;

      // updating size

      size--;

      //returning removed data

      return data;

}


Related Solutions

Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add...
Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add new instance variables or modify existing ones. private LinkedNode<T> head; private LinkedNode<T> tail; private int size; Q1: Write a method called "public void addFirst(T data)" that does the following: * Adds the element to the front of the deque. * Must be O(1) * @param data the data to add to the front of the deque * @throws java.lang.IllegalArgumentException if data is null Q2:...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    *...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    * Gets the current number of entries in this set.    *    * @return The integer number of entries currently in the set.    */    public int getSize();    /**    * Sees whether this set is empty.    *    * @return True if the set is empty, or false if not.    */    public boolean isEmpty();    /**    *...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T extends Comparable<T>>    void swap(T[] data, int index1, int index2)    {       T temp = data[index1];       data[index1] = data[index2];       data[index2] = temp;    }    public void sort(T[] data)    {       int position, scan;       for (position = data.length - 1; position >= 0; position--)       {          for (scan = 0; scan <= position - 1; scan++)          {...
***JAVA LANGUAGE, NO CHANGES NEEDED FOR THE Movie CLASS. USE THIS AS A REFERENCE*** public class...
***JAVA LANGUAGE, NO CHANGES NEEDED FOR THE Movie CLASS. USE THIS AS A REFERENCE*** public class Movie { private String title; private int length; private static int numMovies = 0; public Movie() { this("", 0); } public Movie(String title, int length) { this.title = title; this.length = length; ++numMovies; } public String getTitle() { return this.title; } public int getLength() { return this.length; } public static int getNumMovies() { return numMovies; } public void setTitle(String title) { this.title = title;...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements Stack<T> { private Node<T> top; private int numElements = 0; public int size() { return (numElements); } public boolean isEmpty() { return (top == null); } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); return top.info; } public T pop() throws StackException { Node<T> temp; if (isEmpty()) throw new StackException("Stack underflow."); temp = top; top = top.getLink(); return temp.getInfo();...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
Using Java, Complete LinkedListSet: package Homework3; public class LinkedListSet <T> extends LinkedListCollection <T> { LinkedListSet() {...
Using Java, Complete LinkedListSet: package Homework3; public class LinkedListSet <T> extends LinkedListCollection <T> { LinkedListSet() { } public boolean add(T element) { // Code here return true; } } Homework3 class: public class Homework3 { public static void main(String[] args) { ArrayCollection ac1 = new ArrayCollection(); // Calling Default Constructor ArrayCollection ac2 = new ArrayCollection(2); // Calling overloaded constructor ArraySet as1 = new ArraySet(); ac2.add("Apple"); ac2.add("Orange"); ac2.add("Lemon"); // This can't be added into ac2 as collection is full System.out.println(ac2.remove("Apple")); //...
//Using Java language Write a copy instructor for the following class. Be as efficient as possible....
//Using Java language Write a copy instructor for the following class. Be as efficient as possible. import java.util.Random; class Saw {    private int x;    private Integer p; //------------------------------------------ public Saw() { Random r = new Random();        x = r.nextInt();        p = new Integer(r.nextInt()); } }
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void main(String[] args) { Rectangle r1 = new Rectangle(0,0,100,150); System.out.println(r1);    Rectangle r2 = new Rectangle(50,75,100,150); System.out.println(r2);    Rectangle r3 = r1.intersection(r2);    } } Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics {...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics { /** * Calculates and returns the complement of a DNA sequence. In DNA sequences, 'A' and 'T' are * complements of each other, as are 'C' and 'G'. The complement is formed by taking the * complement of each symbol (e.g., the complement of "GTCA" is "CAGT"). * * @param dna a char array representing a DNA sequence of arbitrary length, * containing only...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT