mirror of
https://github.com/RustyRaptor/CS-272.git
synced 2026-12-03 15:55:22 -07:00
Almost done with lab 9 woohoo
This commit is contained in:
1 parent
b86f780237
commit
23e6ff5bbe
3 files changed
+147
-6
No files matched your search
+81
-1
@@ -1,3 +1,6 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.EmptyStackException;
|
||||
|
||||
public class ArrayQueue <E> implements QueueInterface {
|
||||
private E[] data;
|
||||
private int front, rear;
|
||||
@@ -13,6 +16,7 @@ public class ArrayQueue <E> implements QueueInterface {
|
||||
|
||||
@Override
|
||||
public void enqueue(Object o) {
|
||||
ensureCapacity();
|
||||
data[rear] = (E) o;
|
||||
rear++;
|
||||
size++;
|
||||
@@ -20,12 +24,47 @@ public class ArrayQueue <E> 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 <E> 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Palindrome {
|
||||
public static void main(String[] args) {
|
||||
LinkedQueue<String> wordq = new LinkedQueue();
|
||||
LinkedQueue<String> 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");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+19
-5
@@ -112,7 +112,11 @@ public class QueueTest {
|
||||
|
||||
static void testArrDequeue(){
|
||||
ArrayQueue<Integer> test = new ArrayQueue<Integer>();
|
||||
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<Integer> test = new ArrayQueue<Integer>();
|
||||
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++) {
|
||||
|
||||
Reference in new issue
Block a user