Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*********** Homework B Edit the following file and save it as Bxxxxxxxx.java whe

ID: 3860276 • Letter: #

Question

***********
Homework B

Edit the following file and save it as Bxxxxxxxx.java where xxxxxxxx is
replaced by your 8 digit ID number. Change the class name to reflect this
file name. Include a comment with your name at the top of the program in case
there is a problem with automatic recognition of your ID number. Submit this
one file as an email attachment to kmahavadi@qc.cuny.edu

Remove any initial package declaration that might be added to your
file in case you edit it in eclipse.

The file B00000000.java contains :

a class B00000000 with a main method --- which you should change to report your name
you should also rename this to match your file name
an interface Deque --- no changes should be made to this
an implementation class ArrayDeque --- no changes are to be made to this
a class DNode for doubly linked nodes --- no changes should be made to this
an implementation class LinkedDeque --- the methods of this class are missing.

The homework assignment is to complete the methods:
LinkedDeque(), size(), empty(), removeFront(), removeRear(),
addRear(), addFront(), toString(), main().

The only change needed in main() is to make it print your name and
8-digit ID in addition to the other tasks which are already programmed.

When your homework is correctly implemented, the two deques will operate
identically and produce duplicate prompts when the main program is run.

In the other methods, you should supply an implementation using doubly linked
nodes in such a way that a client would not see a difference between the
operation of the classes ArrayDeque and Linked Deque. You must not add any
additional instance variables to the class LinkedDeque.

********************************************************************/

import java.util.Scanner;

public class B00000000 {
public static void main(String args[]) {
System.out.println("Homework B by xxxxxx "); // fill in your name here
Deque<String> q = new ArrayDeque<>();
Deque<String> qLink = new LinkedDeque<>();
testDeque(q, qLink); // test both together
}

public static void testDeque(Deque<String> q, Deque<String> qLink) {
boolean done = false;
Scanner sc = new Scanner(System.in);
while (!done) {
try {
System.out.println(" Array queue: " + q + " ");
System.out.print("Linked queue: " + qLink + "      ");
System.out.println(" cmds are a r A R Q: >>");
String cmd = sc.next();
String entry = null;
char command = cmd.charAt(0);
if (command == 'a' || command == 'A')
entry = sc.next();
switch (cmd.charAt(0)) {
case 'Q':
done = true;
break;
case 'a':
q.addFront(entry);
qLink.addFront(entry);
break;
case 'A':
q.addRear(entry);
qLink.addRear(entry);
break;
case 'r':
q.removeFront();
qLink.removeFront();
break;
case 'R':
q.removeRear();
qLink.removeRear();
break;
}
} catch (Exception e) {
System.out.println("Error " + e.toString());
}
}
sc.close();
}
}

interface Deque<T> {
public T removeFront() throws Exception;

public T removeRear() throws Exception;

public void addFront(T x) throws Exception;

public void addRear(T x) throws Exception;

public boolean empty();

public int size();
}

class ArrayDeque<T> implements Deque<T> {
private T data[];
private int front, rear, size, capacity;

public ArrayDeque() {
capacity = 1000;
data = (T[]) new Object[capacity];
front = size = 0;
rear = 1;
}

public ArrayDeque(int c) {
capacity = c;
data = (T[]) new Object[capacity];
front = size = 0;
rear = 1;
}

public int size() {
return size;
}

public boolean empty() {
return size == 0;
}

public void addFront(T x) throws Exception {
if (size() == capacity)
throw new Exception("Deque Full");
data[front--] = x;
if (front < 0)
front = capacity - 1;
size++;
}

public void addRear(T x) throws Exception {
if (size() == capacity)
throw new Exception("Deque Full");
data[rear++] = x;
if (rear == capacity)
rear = 0;
size++;
}

public T removeFront() throws Exception {
if (empty())
throw new Exception("Deque Empty");
front = front + 1;
if (front == capacity)
front = 0;
size--;
return data[front];
}

public T removeRear() throws Exception {
if (empty())
throw new Exception("Deque Empty");
rear = rear - 1;
if (rear == -1)
rear = capacity - 1;
size--;
return data[rear];
}

// methods for testing purposes
public String toString() {
int i, j;
String ans = "";
for (i = 0, j = front + 1; i < size; i++, j++) {
if (j == capacity)
j = 0;
ans += data[j];
if (i < size - 1) ans += " -> ";
}
return ans;
}

}

class DNode<T> {
private T data;
private DNode<T> prev, next;

public DNode(T x, DNode<T> p, DNode<T> n) {
data = x;
prev = p;
next = n;
}

public T getData() {
return data;
}

public DNode<T> getNext() {
return next;
}

public DNode<T> getPrev() {
return prev;
}

public void setData(T x) {
data = x;
}

public void setNext(DNode<T> n) {
next = n;
}

public void setPrev(DNode<T> p) {
prev = p;
}
}

class LinkedDeque<T> implements Deque<T> {
private DNode<T> front, rear;
private int size;

// add implementations for the following methods
// your code should perform in exactly the same way as the corresponding
// ArrayDeque<T> methods
// you should not add any extra instance variables to the class and must
// use a doubly linked list implementation.

public LinkedDeque() {
// add implementation
}

public int size() {
// add implementation
return 0;
}

public boolean empty() {
// add implementation
return true;
}

public T removeFront() throws Exception {
// add implementation
throw new Exception("Unimplemented");
}

public T removeRear() throws Exception {
// add implementation
throw new Exception("Unimplemented");
}

public void addRear(T x) throws Exception {
// add implementation
throw new Exception("Unimplemented");
}

public void addFront(T x) throws Exception {
// add implementation
throw new Exception("Unimplemented");
}

public String toString() {
// add implementation
return "";
}
}

Explanation / Answer

import java.util.Scanner;

public class B00000000 {
public static void main(String args[]) {
System.out.println("Homework B by xxxxxx "); // fill in your name here
Deque<String> q = new ArrayDeque<>();
Deque<String> qLink = new LinkedDeque<>();
testDeque(q, qLink); // test both together
}

public static void testDeque(Deque<String> q, Deque<String> qLink) {
boolean done = false;
Scanner sc = new Scanner(System.in);
while (!done) {
try {
System.out.println(" Array queue: " + q + " ");
System.out.print("Linked queue: " + qLink + "      ");
System.out.println(" cmds are a r A R Q: >>");
String cmd = sc.next();
String entry = null;
char command = cmd.charAt(0);
if (command == 'a' || command == 'A')
entry = sc.next();
switch (cmd.charAt(0)) {
case 'Q':
done = true;
break;
case 'a':
q.addFront(entry);
qLink.addFront(entry);
break;
case 'A':
q.addRear(entry);
qLink.addRear(entry);
break;
case 'r':
q.removeFront();
qLink.removeFront();
break;
case 'R':
q.removeRear();
qLink.removeRear();
break;
}
} catch (Exception e) {
System.out.println("Error " + e.toString());
}
}
sc.close();
}
}

interface Deque<T> {
public T removeFront() throws Exception;

public T removeRear() throws Exception;

public void addFront(T x) throws Exception;

public void addRear(T x) throws Exception;

public boolean empty();

public int size();
}

class ArrayDeque<T> implements Deque<T> {
private T data[];
private int front, rear, size, capacity;

public ArrayDeque() {
capacity = 1000;
data = (T[]) new Object[capacity];
front = size = 0;
rear = 1;
}

public ArrayDeque(int c) {
capacity = c;
data = (T[]) new Object[capacity];
front = size = 0;
rear = 1;
}

public int size() {
return size;
}

public boolean empty() {
return size == 0;
}

public void addFront(T x) throws Exception {
if (size() == capacity)
throw new Exception("Deque Full");
data[front--] = x;
if (front < 0)
front = capacity - 1;
size++;
}

public void addRear(T x) throws Exception {
if (size() == capacity)
throw new Exception("Deque Full");
data[rear++] = x;
if (rear == capacity)
rear = 0;
size++;
}

public T removeFront() throws Exception {
if (empty())
throw new Exception("Deque Empty");
front = front + 1;
if (front == capacity)
front = 0;
size--;
return data[front];
}

public T removeRear() throws Exception {
if (empty())
throw new Exception("Deque Empty");
rear = rear - 1;
if (rear == -1)
rear = capacity - 1;
size--;
return data[rear];
}

// methods for testing purposes
public String toString() {
int i, j;
String ans = "";
for (i = 0, j = front + 1; i < size; i++, j++) {
if (j == capacity)
j = 0;
ans += data[j];
if (i < size - 1) ans += " -> ";
}
return ans;
}

}

class DNode<T> {
private T data;
private DNode<T> prev, next;

public DNode(T x, DNode<T> p, DNode<T> n) {
data = x;
prev = p;
next = n;
}

public T getData() {
return data;
}

public DNode<T> getNext() {
return next;
}

public DNode<T> getPrev() {
return prev;
}

public void setData(T x) {
data = x;
}

public void setNext(DNode<T> n) {
next = n;
}

public void setPrev(DNode<T> p) {
prev = p;
}
}

class LinkedDeque<T> implements Deque<T> {
private DNode<T> front, rear;
private int size;

// add implementations for the following methods
// your code should perform in exactly the same way as the corresponding
// ArrayDeque<T> methods
// you should not add any extra instance variables to the class and must
// use a doubly linked list implementation.

public LinkedDeque() {
// add implementation
}

public int size() {
// add implementation
return 0;
}

public boolean empty() {
// add implementation
return true;
}

public T removeFront() throws Exception {
// add implementation
throw new Exception("Unimplemented");
}

public T removeRear() throws Exception {
// add implementation
throw new Exception("Unimplemented");
}

public void addRear(T x) throws Exception {
// add implementation
throw new Exception("Unimplemented");
}

public void addFront(T x) throws Exception {
// add implementation
throw new Exception("Unimplemented");
}

public String toString() {
// add implementation
return "";
}
}