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 " +...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
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; }...
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() {       ...
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;...
public class Square {    public static final int NUM_OF_SIDES = 4;    private float length;...
public class Square {    public static final int NUM_OF_SIDES = 4;    private float length;       public Square(float l) {        setLength(l);    }       public void setLength(float l) {        if(l >= 0) {            length = l;        }    }       public float getLength() {        return length;    }           //TODO - Add method to calculate add return area          //TODO -...
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...
public class Clock { private int hr; private int min; private int sec; public Clock() {...
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec =...
public class GroceryCart { private static final int DEFAULT_CAPACITY = 10; /* * Default constructor with...
public class GroceryCart { private static final int DEFAULT_CAPACITY = 10; /* * Default constructor with zero arguments. This constructs a grocery * cart with a default capacity of ten items. */ public GroceryCart() { } /* * Alternate constructor which takes in a maxCapacity. This maxCapacity * determines how many items can fit inside of this groceryCart. */ public GroceryCart(int maxCapacity) { } /* * Adds an item to the grocery cart. Returns true if the item was added...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT