From 23e6ff5bbe55e80e6865581c53b87b2621209fa7 Mon Sep 17 00:00:00 2001 From: RustyRaptor Date: Thu, 24 Oct 2019 19:33:57 -0600 Subject: [PATCH] Almost done with lab 9 woohoo --- src/ArrayQueue.java | 82 ++++++++++++++++++++++++++++++++++++++++++++- src/Palindrome.java | 47 ++++++++++++++++++++++++++ src/QueueTest.java | 24 ++++++++++--- 3 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 src/Palindrome.java diff --git a/src/ArrayQueue.java b/src/ArrayQueue.java index 5dc9ce3..83e3626 100644 --- a/src/ArrayQueue.java +++ b/src/ArrayQueue.java @@ -1,3 +1,6 @@ +import java.util.Arrays; +import java.util.EmptyStackException; + public class ArrayQueue implements QueueInterface { private E[] data; private int front, rear; @@ -13,6 +16,7 @@ public class ArrayQueue implements QueueInterface { @Override public void enqueue(Object o) { + ensureCapacity(); data[rear] = (E) o; rear++; size++; @@ -20,12 +24,47 @@ public class ArrayQueue implements QueueInterface { @Override public E dequeue() { + if (isEmpty()) { + throw new EmptyStackException(); + } + size--; - return data[front++]; + if (!isEmpty()) { + return data[front++]; + } + return data[0]; + + } + + + + public void ensureCapacity() { + if (front > 0 && rear >= data.length-1) { + rear = 0; + } + if (rear < 0) { + rear = data.length - 1; + } + if (front == 0 && rear >= data.length - 1) { + E[] bigarr = (E[]) new Object[data.length*2]; + System.arraycopy(getData(), 0, bigarr, 0, data.length); + data = bigarr; + } + if (rear == front && size >= data.length) { + E[] bigarr = (E[]) new Object[data.length*2]; + System.arraycopy(data, front, bigarr, 0, + data.length - front); + System.arraycopy(data, 0, bigarr, + data.length - front, + data.length - (data.length - front)); + } } @Override public E front() { + if (isEmpty()) { + throw new EmptyStackException(); + } return data[front]; } @@ -34,8 +73,49 @@ public class ArrayQueue implements QueueInterface { return size; } + @Override + public String toString() { + return "ArrayQueue{" + + "data=" + Arrays.toString(data) + + ", front=" + front + + ", rear=" + rear + + ", size=" + size + + '}'; + } + @Override public boolean isEmpty() { return size == 0; } + public E[] getData() { + return data; + } + + public void setData(E[] data) { + this.data = data; + } + + public int getFront() { + return front; + } + + public void setFront(int front) { + this.front = front; + } + + public int getRear() { + return rear; + } + + public void setRear(int rear) { + this.rear = rear; + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } } \ No newline at end of file diff --git a/src/Palindrome.java b/src/Palindrome.java new file mode 100644 index 0000000..55cf49b --- /dev/null +++ b/src/Palindrome.java @@ -0,0 +1,47 @@ +import java.util.Arrays; +import java.util.Scanner; + +public class Palindrome { + public static void main(String[] args) { + LinkedQueue wordq = new LinkedQueue(); + LinkedQueue wordj = new LinkedQueue(); + + Scanner inp = new Scanner(System.in); + + System.out.println("enter sentence"); + String nn = inp.nextLine(); + nn = nn.toLowerCase(); + String[] punct = {"!", ".", "?",","}; + System.out.println(nn); + for (int i = 0; i < punct.length; i++) { + nn = nn.replace(punct[i], ""); + } + String[] nn2 = nn.split(" "); + System.out.println(Arrays.toString(nn2)); + for (int i = 0; i < nn2.length; i++) { + wordq.enqueue(nn2[i]); + } + System.out.println(wordq); + for (int i = nn2.length-1; i >= 0; i--) { + wordj.enqueue(nn2[i]); + } + System.out.println(wordj); + boolean palindrome = true; + while(!wordj.isEmpty()) { + if (!wordj.dequeue().equals(wordq.dequeue())){ + palindrome = false; + break; + } + } + + + if (palindrome){ + + System.out.println("This sentence is a palindrome " + + "indeed"); + } else { + System.out.println("This sentence is not a palindrome"); + } + + } +} diff --git a/src/QueueTest.java b/src/QueueTest.java index 5e59ad3..c2782ac 100644 --- a/src/QueueTest.java +++ b/src/QueueTest.java @@ -112,7 +112,11 @@ public class QueueTest { static void testArrDequeue(){ ArrayQueue test = new ArrayQueue(); - test.dequeue(); + try { + test.dequeue(); + } catch (Exception e) { + System.out.println("EXCEPTION FOR ARRAY WORKS"); + } test.isEmpty(); System.out.println(test); for (int i = 0; i < 10; i++) { @@ -120,8 +124,12 @@ public class QueueTest { } test.dequeue(); test.dequeue(); - for (int i = 0; i < 10; i++) { - test.dequeue(); + try { + for (int i = 0; i < 10; i++) { + test.dequeue(); + } + } catch (Exception e) { + System.out.println("EXCEPTION WORKS"); } System.out.println(test); for (int i = 0; i < 10; i++) { @@ -132,8 +140,14 @@ public class QueueTest { } static void testArrFront(){ ArrayQueue test = new ArrayQueue(); - test.front(); - System.out.println(test.front()); + + try { + test.front(); + System.out.println(test.front()); + } catch (Exception e) { + System.out.println("EXCEPTION WORKS"); + } + test.enqueue(0); System.out.println(test.front()); for (int i = 0; i < 10; i++) {