Java/Queues
** Task: Write a JUnit test that shows a failure in some part of the ADT
-----ArrayQueue.java-------
public class ArrayQueue {
private static final int INITIAL_CAPACITY = 2; // to
permit easier testing
private Object[] contents;
private int front, rear;
/**
* Create an empty queue with an initial
capacity.
*/
public ArrayQueue() {
contents = new
Object[INITIAL_CAPACITY];
}
/**
* Add an element to the back of the queue.
* The queue's capacity is increased as
necessary.
* @param element element added
*/
public void enqueue(E element) {
contents[rear] = element;
++rear;
if (rear >= INITIAL_CAPACITY)
{
Object[]
newContents = new Object[contents.length*2];
for (int i=0; i
< contents.length; ++i) {
newContents[i] = contents[i];
}
contents =
newContents;
}
}
/**
* Remove and return the front of the queue.
* @return first element in the queue.
*/
public E dequeue() {
E result = peek();
++front;
if (front >= INITIAL_CAPACITY)
front = 0;
return result;
}
/**
* Return the front of the queue without removing
it.
* @return the first element in the queue.
*/
public E peek() {
@SuppressWarnings("unchecked")
E result =
(E)contents[front];
return result;
}
/**
* Return the number of elements in the queue.
* @return number of elements in the queue.
*/
public int size() {
return front + rear;
}
/**
* Return the capacity of the queue.
* It shouldn't more than twice
* the maximum size (or max size + 1) the queue ever
had.
* @return current capacity of the queue
*/
public int capacity() {
return contents.length;
}
}
-------TestQueue.java-------
import junit.framework.TestCase;
import edu.uwm.apc430.ArrayQueue;
public class TestQueue extends TestCase {
private ArrayQueue queue;
protected void setUp() {
queue = new
ArrayQueue<>();
}
public void test00() {
assertEquals(0,queue.size());
}
public void test01() {
queue.enqueue(42);
assertEquals(1,queue.size());
}
}
package com.HomeworkLib;
import junit.framework.TestCase;
public class TestQueue extends TestCase {
@SuppressWarnings("rawtypes")
private ArrayQueue queue;
protected void setUp() {
queue = new
ArrayQueue<>();
}
public void test00() {
assertEquals(0,
queue.size());
}
public void test01() {
queue.enqueue(42);
assertEquals(1,
queue.size());
}
public void test02() {
queue.enqueue(42);
queue.enqueue(43);
queue.enqueue(44);
queue.enqueue(45);
assertEquals(3,
queue.size());
}
//To test dequeue function
public void test03() {
queue.enqueue(42);
queue.enqueue(43);
queue.enqueue(44);
queue.enqueue(45);
//Dequeue should remove one element
from queue so size should be 3
queue.dequeue();
assertEquals(3,
queue.size());
}
public void test04() {
queue.enqueue(42);
queue.enqueue(43);
queue.enqueue(44);
queue.enqueue(45);
System.out.println(queue.capacity());
//Dequeue should remove one element
from queue so size should be 3
int expectedFirstElement =
42;
assertEquals(expectedFirstElement,
queue.peek());
}
}
----------
package com.HomeworkLib;
public class ArrayQueue<E> {
private static final int INITIAL_CAPACITY = 2; // to
permit easier testing
private Object[] contents;
private int front, rear;
/**
* Create an empty queue with an initial
capacity.
*/
public ArrayQueue() {
contents = new
Object[INITIAL_CAPACITY];
}
/**
* Add an element to the back of the queue. The queue's
capacity is increased as
* necessary.
*
* @param element element added
*/
public void enqueue(E element) {
contents[rear] = element;
++rear;
if (rear >= INITIAL_CAPACITY)
{
Object[]
newContents = new Object[contents.length * 2];
for (int i = 0;
i < contents.length; ++i) {
newContents[i] = contents[i];
}
contents =
newContents;
}
}
/**
* Remove and return the front of the queue.
*
* @return first element in the queue.
*/
public E dequeue() {
E result = peek();
++front;
if (front >=
INITIAL_CAPACITY)
front = 0;
return result;
}
/**
* Return the front of the queue without removing
it.
*
* @return the first element in the queue.
*/
public E peek() {
@SuppressWarnings("unchecked")
E result = (E)
contents[front];
return result;
}
/**
* Return the number of elements in the queue.
*
* @return number of elements in the queue.
*/
public int size() {
return front + rear;
}
/**
* Return the capacity of the queue. It shouldn't more
than twice the maximum
* size (or max size + 1) the queue ever had.
*
* @return current capacity of the queue
*/
public int capacity() {
return contents.length;
}
}
Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the...
My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...
Revision Question Consider the following Java class: { public static void main (String [ ] args) { ArrayQueue<Integer> queue; queue = new ArrayQueue<Integer> () ; Integer x, y ; x = 3; y = 6; queue.offer (x) ; queue.offer (12) ; queue.offer (y) ; y = queue.peek () ; queue.poll () ; queue. offer (x - 2) ; queue.offer (x) ; queue.offer (y + 4) ; System.out.println ("Queue Elements: ") ; while (! queue.empty() ) System.out.print (queue.poll () + "...
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...
I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item) Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...
create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s) to return the max element in the queu. min(SinglyLinkedQueue<Integer> s) to return the min element in the queue. sum(SinglyLInkedQueue<Integer> s) to return the sum of elements in the queu. median(SinglyLinkedQueue<Integer> s) to return the median of elements in the queue. split(SinglyLinkedQueue<Integer> s) to separate the SinglyLinkedQueue into two ArrayQueues based on whether the element values are even or odd. package Stack_and_Queue; import java.util.Iterator; import...
IN JAVA: Write a class, ZeroException, which is an Exception, and is used to signal that something is zero when it shouldn't be. Write the class ArrayManipulator which creates an array and provides several methods to manipulate values taken from an array. ZeroException Task: Exceptions are used to signal many types of problems in a program. We can write our own as well to describe specific exceptional conditions which may arise. The advantage of doing so is that when exceptions...
JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...
Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> { // Data fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] theData; /** The current size */ private int size = 0; /** The current capacity */ private int capacity = 0; @SuppressWarnings("unchecked") public KWArrayList() { capacity...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...
e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...