Question

In: Computer Science

import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5;...

import java.util.NoSuchElementException;

public class ArrayBasedDeque<AnyType> implements deque<AnyType> {
  
   private static int MAX_SIZE = 5; // initial array size
   //DO NOT CHANGE this, it is set to 5 to make sure your code
   //will pass all the tests and works with no issue.

  
   // add all data fields which are required

   /**
   * ArrayBasedDeque() constructs an empty deque.
   */
   public ArrayBasedDeque(){
       //complete
   }
  
   /**
   * Returns the size of the deque
   * @return the number of elements in this deque
   */
   public int size() {
       //complete
      
   }
  
   /**
   * Removes all elements from this deque
   */
   public void clear() {
       //complete
   }
  
   /**
   * Tests if the deque contains no element
   * @return true if the deque contains no element
   */
   public boolean isEmpty() {
   //complete
   }
  
   /**
   * Adds an item to this front of the deque
   * @param x any object
   */
   public void addFirst(AnyType x) {
       //complete
   }
  
   /**
   * Adds an item to this rear of the deque
   * @param x any object
   */
   public void addLast(AnyType x) {
       //complete
   }
  
   /**
   * Remove and return the item at the front of the deque.
   * @return the item that was removed from the deque
   * @throws NoSuchElementException if the deque is empty
   */
   public AnyType removeFirst() throws NoSuchElementException{
       //complete
   }
  
   /**
   * Remove and return the item at the rear of the deque.
   * @return the item that was removed from the deque
   * @throws NoSuchElementException if the deque is empty
   */
   public AnyType removeLast() throws NoSuchElementException{
       //complete
   }
  
   /**
   * Returns the item at position index in deque
   * front of the deque will be considered as index 0,
   * index 1 is the next item, and so on
   * @param index the index to search in
   * @return return the item in index
   * @throws IndexOutOfBoundsException if index is out ot bound
   */
   public AnyType get(int index) throws IndexOutOfBoundsException {
       //complete
   }
  
   /**
   * Return the item at the rear of the deque.
   * @return the item the rear of the deque
   * @throws NoSuchElementException if the deque is empty
   */
   public AnyType getLast() throws NoSuchElementException{
       //complete
   }
  
   /**
   * Return the item at the front of the deque.
   * @return the item the front of the deque
   * @throws NoSuchElementException if the deque is empty
   */
   public AnyType getFirst() throws NoSuchElementException{
       //complete
   }
  
   public String toString() {
       //complete
       String output = "[ ";
       for(int i = 0; i < numItems; i++)
           output += items[i] + " ";
   output += "]";
       return output;
   }
}

if the following methods,

can someone help me what should i use/input in the method- size, clear, isempty, addfirst, addlast, removefirst, removelast, get , getlast, getfirst?(method fields are empty and there is a comment to complet them)

we have to use recursion and these are the requirements,

Your operations are subject to the following rules:

● All four operations on add and remove must not involve any looping or recursion. Each operation must take “constant time”, except during expanding the array.

● size, get, getFirst and getLast operations must take “constant time”

Solutions

Expert Solution

Following are implementation method that is asked for.

import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.NoSuchElementException;


public class ArrayBasedDeque<AnyType> implements Deque<AnyType> {
  
   private static int MAX_SIZE = 5; // initial array size
   //DO NOT CHANGE this, it is set to 5 to make sure your code
   //will pass all the tests and works with no issue.
   
   private Object [] items;
   
   private int numItems;
  
   // add all data fields which are required

   /**
   * ArrayBasedDeque() constructs an empty deque.
   */
   public ArrayBasedDeque(){
       //complete
       items = new Object[MAX_SIZE];
       numItems = 0;
   }
  
   /**
   * Returns the size of the Deque
   * @return the number of elements in this deque
   */
   public int size() {
       //complete
      return numItems;
   }
  
   /**
   * Removes all elements from this deque
   */
   public void clear() {
       //complete
       for(int i = 0; i < numItems; i++)
       {
           items[i] = null;
       }
   }
  
   /**
   * Tests if the deque contains no element
   * @return true if the deque contains no element
   */
   public boolean isEmpty() {
   //complete
   
   return numItems == 0;
   }
  
   /**
   * Adds an item to this front of the deque
   * @param x any object
   */
   public void addFirst(AnyType x) {
       //complete
       if(items.length == numItems)
       {
           Object []temp = new Object[items.length + MAX_SIZE];
           
           System.arraycopy(items, 0, temp, 1,items.length );
           
           items = temp;
       }
       else if(numItems > 0)
       {
           System.arraycopy(items, 0, items, 1,numItems );
       }
       
       numItems++;
       
       items[0] = x;
   }
  
   /**
   * Adds an item to this rear of the deque
   * @param x any object
   */
   public void addLast(AnyType x) {
       //complete
       
       if(items.length == numItems)
       {
           Object [] temp = new Object[items.length + MAX_SIZE];
           
           System.arraycopy(items, 0, temp, 0,items.length );
           
           items = temp;
       }
       
       numItems++;
       
       items[numItems - 1] = x;
   }
  
   /**
   * Remove and return the item at the front of the deque.
   * @return the item that was removed from the deque
   * @throws NoSuchElementException if the deque is empty
   */
   public AnyType removeFirst() throws NoSuchElementException{
       //complete
     
       if(numItems == 0)
           throw new NoSuchElementException();
       
       Object first = items[0];
       
       System.arraycopy(items, 1, items, 0,items.length - 1 );
       
       items[numItems - 1] = null;
       
       numItems--;
       
       return (AnyType)first;
   }
  
   /**
   * Remove and return the item at the rear of the deque.
   * @return the item that was removed from the deque
   * @throws NoSuchElementException if the deque is empty
   */
   public AnyType removeLast() throws NoSuchElementException{
       //complete
       
       if(numItems == 0)
           throw new NoSuchElementException();
       
       Object last = items[numItems-1];
       
       items[numItems-1] = null;
       
       numItems--;
       
       return (AnyType)last;
   }
  
   /**
   * Returns the item at position index in deque
   * front of the deque will be considered as index 0,
   * index 1 is the next item, and so on
   * @param index the index to search in
   * @return return the item in index
   * @throws IndexOutOfBoundsException if index is out ot bound
   */
   public AnyType get(int index) throws IndexOutOfBoundsException {
       //complete
       if(index > numItems)
           throw new IndexOutOfBoundsException();
       return (AnyType)items[index];
   }
  
   /**
   * Return the item at the rear of the deque.
   * @return the item the rear of the deque
   * @throws NoSuchElementException if the deque is empty
   */
   public AnyType getLast() throws NoSuchElementException{
       //complete
       
       if(numItems == 0)
           throw new NoSuchElementException();
       return (AnyType)items[numItems - 1];
   }
  
   /**
   * Return the item at the front of the deque.
   * @return the item the front of the deque
   * @throws NoSuchElementException if the deque is empty
   */
   public AnyType getFirst() throws NoSuchElementException{
       //complete
       if(numItems == 0)
           throw new NoSuchElementException();
       return (AnyType)items[0];
   }
  
   public String toString() {
       //complete
       String output = "[ ";
       for(int i = 0; i < numItems; i++)
           output += items[i] + " ";
       output += "]";
       return output;
   }
}

If you don't implement Deque<AnyType> then above code is complete.

Since aove code not implemented all mehtod of Deque, is not complete and give error. Below are complete code.

import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.NoSuchElementException;


public class ArrayBasedDeque<AnyType> implements Deque<AnyType> {
  
   private static int MAX_SIZE = 5; // initial array size
   //DO NOT CHANGE this, it is set to 5 to make sure your code
   //will pass all the tests and works with no issue.
   
   private Object [] items;
   
   private int numItems;
  
   // add all data fields which are required

   /**
   * ArrayBasedDeque() constructs an empty deque.
   */
   public ArrayBasedDeque(){
       //complete
       items = new Object[MAX_SIZE];
       numItems = 0;
   }
  
   /**
   * Returns the size of the Deque
   * @return the number of elements in this deque
   */
   public int size() {
       //complete
      return numItems;
   }
  
   /**
   * Removes all elements from this deque
   */
   public void clear() {
       //complete
       for(int i = 0; i < numItems; i++)
       {
           items[i] = null;
       }
   }
  
   /**
   * Tests if the deque contains no element
   * @return true if the deque contains no element
   */
   public boolean isEmpty() {
   //complete
   
   return numItems == 0;
   }
  
   /**
   * Adds an item to this front of the deque
   * @param x any object
   */
   public void addFirst(AnyType x) {
       //complete
       if(items.length == numItems)
       {
           Object []temp = new Object[items.length + MAX_SIZE];
           
           System.arraycopy(items, 0, temp, 1,items.length );
           
           items = temp;
       }
       else if(numItems > 0)
       {
           System.arraycopy(items, 0, items, 1,numItems );
       }
       
       numItems++;
       
       items[0] = x;
   }
  
   /**
   * Adds an item to this rear of the deque
   * @param x any object
   */
   public void addLast(AnyType x) {
       //complete
       
       if(items.length == numItems)
       {
           Object [] temp = new Object[items.length + MAX_SIZE];
           
           System.arraycopy(items, 0, temp, 0,items.length );
           
           items = temp;
       }
       
       numItems++;
       
       items[numItems - 1] = x;
   }
  
   /**
   * Remove and return the item at the front of the deque.
   * @return the item that was removed from the deque
   * @throws NoSuchElementException if the deque is empty
   */
   public AnyType removeFirst() throws NoSuchElementException{
       //complete
     
       if(numItems == 0)
           throw new NoSuchElementException();
       
       Object first = items[0];
       
       System.arraycopy(items, 1, items, 0,items.length - 1 );
       
       items[numItems - 1] = null;
       
       numItems--;
       
       return (AnyType)first;
   }
  
   /**
   * Remove and return the item at the rear of the deque.
   * @return the item that was removed from the deque
   * @throws NoSuchElementException if the deque is empty
   */
   public AnyType removeLast() throws NoSuchElementException{
       //complete
       
       if(numItems == 0)
           throw new NoSuchElementException();
       
       Object last = items[numItems-1];
       
       items[numItems-1] = null;
       
       numItems--;
       
       return (AnyType)last;
   }
  
   /**
   * Returns the item at position index in deque
   * front of the deque will be considered as index 0,
   * index 1 is the next item, and so on
   * @param index the index to search in
   * @return return the item in index
   * @throws IndexOutOfBoundsException if index is out ot bound
   */
   public AnyType get(int index) throws IndexOutOfBoundsException {
       //complete
       if(index > numItems)
           throw new IndexOutOfBoundsException();
       return (AnyType)items[index];
   }
  
   /**
   * Return the item at the rear of the deque.
   * @return the item the rear of the deque
   * @throws NoSuchElementException if the deque is empty
   */
   public AnyType getLast() throws NoSuchElementException{
       //complete
       
       if(numItems == 0)
           throw new NoSuchElementException();
       return (AnyType)items[numItems - 1];
   }
  
   /**
   * Return the item at the front of the deque.
   * @return the item the front of the deque
   * @throws NoSuchElementException if the deque is empty
   */
   public AnyType getFirst() throws NoSuchElementException{
       //complete
       if(numItems == 0)
           throw new NoSuchElementException();
       return (AnyType)items[0];
   }
  
   public String toString() {
       //complete
       String output = "[ ";
       for(int i = 0; i < numItems; i++)
           output += items[i] + " ";
       output += "]";
       return output;
   }

        @Override
        public boolean offerFirst(AnyType e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean offerLast(AnyType e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public AnyType pollFirst() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public AnyType pollLast() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public AnyType peekFirst() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public AnyType peekLast() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean removeFirstOccurrence(Object o) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean removeLastOccurrence(Object o) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean add(AnyType e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean offer(AnyType e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public AnyType remove() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public AnyType poll() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public AnyType element() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public AnyType peek() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void push(AnyType e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public AnyType pop() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean remove(Object o) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean contains(Object o) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public Iterator<AnyType> iterator() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public Iterator<AnyType> descendingIterator() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public Object[] toArray() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public <T> T[] toArray(T[] a) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean containsAll(Collection<?> c) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean addAll(Collection<? extends AnyType> c) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean removeAll(Collection<?> c) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public boolean retainAll(Collection<?> c) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }

If you find this helpful give thumb up.

if you have any query, ask in comment section.

Regard.


Related Solutions

import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put code here return myint; } public int getInt() { int f = 0; // put code here return f; } } public class Assignment06 { public static void main(String args[]){ System.out.println("Assignmemnt 06 Written by Matt Weisfeld"); int myInt = 0; // create a Factorial object Factorial factorial = new Factorial(); // get a number from the console myInt = factorial.getInt(); System.out.println("Factorial of " +...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class BowlerReader { private static final String FILE_NAME =...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class BowlerReader { private static final String FILE_NAME = "bowler.txt"; public static void main(String[] args) throws FileNotFoundException { System.out.println("Reading Data from file"); Scanner fileReader = new Scanner(new File(FILE_NAME)); System.out.printf("%-20s%-10s%-10s%-10s%-10s\n", "Sample Data", "Game 1", "Game 2", "Game 3", "Average"); int bowler = 1; while (fileReader.hasNext()) { String scores[] = fileReader.nextLine().split("\\s+"); double average = Integer.parseInt(scores[0]) + Integer.parseInt(scores[1]) + Integer.parseInt(scores[2]); average /= 3; System.out.printf("%-20s%-10s%-10s%-10s%-10.2f\n", "Bowler " + bowler, scores[0], scores[1], scores[2], average); bowler += 1; }...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2;...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2; long ris; public ProductThreads(String name, int [] v1, int [] v2, int begin, int end) { setName(name); this.v1 = v1; this.v2 = v2; this.begin = begin; this.end = end; this.ris = 0; } public void run() { System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started"); ris = 1; for(int i = begin; i <= end; i++) ris...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
needed asap !!! 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand  ...
needed asap !!! 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand   6. private int calc;   7.   8. public Sequen(int numStand) 9. { 10.         stand = numStand; 11.         skip; 12.         } 13.           14.         public void skip() 15.         { 16.         Random sit = new Random(); 17.         calc = sit.nextInt(stand) + 1; 18.         } 19.           20.         public getStand() 21.         { 22.         return stand; 23.         } 24.           25.         int getCalc() 26.         { 27.         return calc; 28.         } 29.         } One error is already identified for you as shown below: Line Number: 5 Error description: Missing semicolon...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT