Question

In: Computer Science

How do I implement public class CircularArrayQueue<E> extends AbstractQueue <E> and test the methods with junit?

How do I implement
public class CircularArrayQueue<E> extends AbstractQueue <E> and test the methods with junit?

Solutions

Expert Solution

Well I have created the code for you. For an implementation of queues based on arrays. The head of the queue starts out at the head of the array, allowing the queue to grow and shrink in constant time. This queue implementation is ideal for applications that require a queue with a known maximum size that expands in constant time.

Here's the code for implementation public class CircularArrayQueue<E> extends AbstractQueue <E> and the methods with junit

import java.util.NoSuchElementException;

public class CircularArrayQueue implements MyQueue 
{
private Integer[] array;
//initial size of the array
private int N;
private int front;
private int rear;
private int count = 0;//total number of items currently in queue.
public CircularArrayQueue()
{
    this.N = 10;
    array = new Integer[N];
    front = rear = 0;
}

public CircularArrayQueue(int size)
{
    this.N = size;
    array = new Integer[N];
    front = rear = 0;
}


//enqueues an element at the rear of the queue
// if the queue is already full it is resized, doubling its size
@Override
public void enqueue(int in) 
{
    count++;
    if (isFull())
    {
       resize();
       rear = (rear + 1) % N;
       array[rear] = in;
    }
    else
    {
       rear = (rear + 1) % N;
       array[rear] = in;
    }            
}

public void resize()
{
    Integer[] temp = new Integer[array.length*2];
    N = array.length*2;
    for(int i=0; i<array.length; i++)
    {
        temp[i] = array[i];
    }

    array = temp;
}


//dequeues an element
// if the queue is empty a NoSuchElement Exception is thrown
@Override
public int dequeue() throws NoSuchElementException 
{
    if(isEmpty())
    {
        throw new Exception("The queue is empty");
    }

    front = (front + 1) % N;
    int headElement = array[front];   
    count--;
    return headElement;
}

@Override
public int noItems() 
{   
    return count;
}

@Override
public boolean isEmpty()
{
    return front == rear;
}

@Override
public boolean isFull()
{
    return front == ((rear + 1) % N);
}

//return the number of indexes that are empty
public int getCapacityLeft()
{
    return N - 1 - count;
}   
}

Related Solutions

I'm really confused Junit test case Here is my code. How can I do Junit test...
I'm really confused Junit test case Here is my code. How can I do Junit test with this code? package pieces; import java.util.ArrayList; import board.Board; public class Knight extends Piece {    public Knight(int positionX, int positionY, boolean isWhite) {        super("N", positionX, positionY, isWhite);    }    @Override    public String getPossibleMoves() {        ArrayList<String> possibleMoves = new ArrayList<>();               // check if squares where knight can go are available for it       ...
Implement the Nickel class. Include Javadoc comments for the class, public fields, constructors, and methods of...
Implement the Nickel class. Include Javadoc comments for the class, public fields, constructors, and methods of the class. I have added the Javadoc comments but please feel free to format them if they are done incorrectly. public class Nickel implements Comparable { private int year; /** * The monetary value of a nickel in cents. */ public final int CENTS = 5; /** * Initializes this nickel to have the specified issue year. * * @param year * * @pre....
public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature...
public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature { public void method1() { System.out.println("creature 1"); } public void method2() { System.out.println("creature 2"); } public String toString() { return "ocean-dwelling"; } } public class Whale extends Mammal { public void method1() { System.out.println("spout"); } public String toString() { return "BIG!"; } } public class Squid extends SeaCreature { public void method2() { System.out.println("tentacles"); } public String toString() { return "squid"; } } What...
How can the classes be modified to satisfy the comments: public class InvalidIntegerException extends Exception{    ...
How can the classes be modified to satisfy the comments: public class InvalidIntegerException extends Exception{     // Write a class for an InvalidIntegerException here     //Constructor that takes no arguments public InvalidIntegerException (){ super(); } //Constructor that takes a string message public InvalidIntegerException (String message){ super(message); } } import java.io.InputStream; import java.io.IOException; public class Parser { private InputStream in; public static final int CHAR_ZERO = (int) '0'; public Parser (InputStream in) { this.in = in; } // Complete the following...
Hi, I want to implement the following methods with a driver class In the comment block...
Hi, I want to implement the following methods with a driver class In the comment block for add, give the best possible big-O of the worst-case running time for executing a single add operations and give the best possible big-O of the total worst-case running time of executing a sequence of N add operations. here is the Implement class: import java.util.Iterator; // Do not modify the given code. @SuppressWarnings("unchecked") // Given public class MyArrayList { private T[] data; // Given...
Question 17 (1 point) What is the return type of this method? public <E extends Comparable<E>>...
Question 17 (1 point) What is the return type of this method? public <E extends Comparable<E>> int theMethod(E arg) Question 17 options: E Comparable int arg Question 18 (1 point) What is the primary benefit of writing generic classes and methods? Question 18 options: Generic classes and methods are more type-safe Generic classes and methods are shorter Generic classes and methods are faster Generic classes and methods are backwards compatible Question 19 (1 point) What is wrong with the following...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score (int). A constructor expects a name as a parameter. A method getLevel to get the level(char) of the student. score level table: A: score >= 90 B: score >= 80 and < 90 C: score >= 60 and < 80 D: score < 60 Example:          Student student = new Student("Zack"); student.score = 10; student.getLevel(); // should be 'D'. student.score = 60; student.getLevel(); //...
IN JAVA. I have the following code (please also implement the Tester to test the methods...
IN JAVA. I have the following code (please also implement the Tester to test the methods ) And I need to add a method called public int remove() that should remove the first integer of the array and return it at the dame time saving the first integer and bring down all other elements. After it should decrease the size of the array, and after return and save the integer. package ourVector; import java.util.Scanner; public class ourVector { private int[]...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
(Sort ArrayList) Write the following method that sorts an ArrayList:   public static <E extends Comparable<E>> void...
(Sort ArrayList) Write the following method that sorts an ArrayList:   public static <E extends Comparable<E>> void sort(ArrayList<E> list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT