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

This is java. I have my code for the other two programs just need help making th

ID: 3596243 • Letter: T

Question

This is java. I have my code for the other two programs just need help making this client program.

This is java

Client. You'll now write a client program called FSclient.java. It should use (and test) your implementations of the two classes above. Your code should work like the Unit Test from the ResizingArrayStack from Lab2, so that it takes a sequence of tokens like:

treating non-dash tokens as "push" requests, and dash characters as pop requests. It should run like:

The result of such a command should be (a) the stack is limited to size 4, so it should never contain more than 4 items, and (b) on each pop request, the popped term should be printed. You may use the main function from ResizingArrayStack as a guide:

Once you have implemented it, test with several input files, to ensure that it is working as expected. How does it deal with pop requests on an empty stack? You may find it useful to test the contents of the FS-stack after each action, using its iterator.

Here are my programs

Deque.java

import java.util.Iterator;

import java.util.NoSuchElementException;

import java.util.Scanner;

public class Deque implements Iterable {

class Node

{

Item data;

Node prev;

Node next;

Node(Item t)

{

this(t, null, null);

}

Node(Item t, Node pr, Node nx)

{

data = t;

prev = pr;

next = nx;

}

}

private Node head, tail;

private int size;

public Deque() // construct an empty deque

{

head = null;

tail = null;

size = 0;

}

public void addFirst(Item item) // add the item to the front

{

if(item == null)

throw new NullPointerException("null not allowed");

Node n = new Node(item, null, head);

if(head != null)

head.prev = n;

head = n;

if(tail == null)

tail = n;

size++;

}

public void addLast(Item item) // add the item to the end

{

if(item == null)

throw new NullPointerException("null not allowed");

Node n = new Node(item, tail, null);

if(tail != null)

tail.next = n;

tail = n;

if(head == null)

head = n;

size++;

}

public Item removeFirst() // remove and return the item from the front

{

if(isEmpty())

throw new NoSuchElementException("deque is empty");

Item val = head.data;

head = head.next;

size --;

if(head != null)

{

head.prev = null;

}

else //now queue is empty

tail = null;

return val;

}

public Item removeLast() // remove and return the item from the end

{

if(isEmpty())

throw new NoSuchElementException("deque is empty");

Item val = tail.data;

tail = tail.prev;

size --;

if(tail != null)

tail.next = null;

else //now queue is empty

head = null;

return val;

}

public boolean isEmpty() // is the deque empty?

{

return size == 0;

}

public int size() // return the number of items on the deque

{

return size;

}

public Iterator iterator() // return an iterator over items in order from front to end

{

return new DequeIterator();

}

class DequeIterator implements Iterator

{

Node current;

public DequeIterator() {

current = head;

}

@Override

public boolean hasNext() {

return current != null;

}

@Override

public Item next() {

if(!hasNext())

throw new NoSuchElementException("deque is empty");

Item val = current.data;

current = current.next;

return val;

}

public void remove() {

throw new UnsupportedOperationException();

}

}

public String toString()

{

String str = "";

Node n = head;

while(n != null)

{

str += n.data.toString() + " ";

n = n.next;

}

str += " ";

return str;

}

public static void main(String[] args) // unit testing (required)

{

Deque d = new Deque<>();

for(int i = 1; i <= 5; i++)

{

System.out.println("addLast(" + i + ")");

d.addLast(i);

}

System.out.println("displaying using iterator... ");

Iterator it = d.iterator();

while(it.hasNext())

System.out.print(it.next()+ " " );

System.out.println();

for(int i = 6; i <= 10; i++)

{

System.out.println("addFirst(" + i + ")");

d.addFirst(i);

}

System.out.println(d);

for(int i = 0; i < 4; i ++)

{

System.out.println("removeFirst() = " + d.removeFirst());

}

while(!d.isEmpty())

{

System.out.println("removeLast() = " + d.removeLast());

}

}

FixSizedStack.java

import java.util.Iterator;

public class FixedSizedStack implements Iterable {

private int capacity;

Deque deque;

public FixedSizedStack(int c) // construct an empty FS-stack, with capacity c

{

capacity = c;

deque= new Deque();

}

public boolean isEmpty() // is it empty?

{

return deque.isEmpty();

}

public int size() // return the number of items on the FS-stack

{

return deque.size();

}

public void push(Item item) // add the item to the front, and remove an item from the back if capacity is reached

{

if(deque.size() == capacity) //already stack reached its fixed capacity

deque.removeLast();

deque.addFirst(item);

}

public Item pop() // remove the item from the front

{

return deque.removeFirst();

}

public Iterator iterator() // return an iterator over items in order from front to end

{

return deque.iterator();

}

private static void display(FixedSizedStack stk)

{

System.out.print("Stack contents: ");

Iterator it = stk.iterator();

while(it.hasNext())

System.out.print(it.next() + " ");

System.out.println();

}

public static void main(String[] args) // unit testing (required)

{

FixedSizedStack stk = new FixedSizedStack(4);

String[] words = {"the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"};

for(int i = 0; i < words.length; i++)

{

System.out.println("Pushing '" + words[i] + "'");

stk.push(words[i]);

display(stk);

}

while(!stk.isEmpty())

{

System.out.println("Stack size: " + stk.size());

System.out.println("Popped " + stk.pop());

}

}

}

Explanation / Answer

Given below is the code for FSClient based on the sample code given in question. But to test it , StdIn and StdOut classes are needed which is not provided in the question.

If running on command line, compile all the classes and then run

java FSClient 4 < input.txt

where input.txt represents a file containing the input line with words and dash. That method of running is by file redirection. You can also run using

java FSClient 4

In this case, you will have to type the words / dashes at console. The command line argument 4 represents the fixed size of the stack.

If running in eclipse, make sure to pass the size as a command line argument in Run -> Run Configurations -> FSClient -> Arguments tab

public class FSClient {

public static void main(String[] args) {

if(args.length != 1 )

{

System.out.println("Usage: java FSClient <stack size>");

return;

}

int size = Integer.parseInt(args[0]);

FixedSizedStack<String> stack = new FixedSizedStack<String>(size);

while (!StdIn.isEmpty()) {

String item = StdIn.readString();

if (!item.equals("-")) //if its not a dash push

stack.push(item);

else

if (!stack.isEmpty())

StdOut.print(stack.pop() + " ");

}

StdOut.println("(" + stack.size() + " left on stack)");

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote