Instructions: 1. Create a class called MyStructure with one instance variable- a
ID: 3890846 • Letter: I
Question
Instructions: 1. Create a class called MyStructure with one instance variable- a LinkedList- and pop() & isEmpty() methods that will work for both MyStack and MyQueue.
2. Rewrite MyStack and MyQueue as subclasses of MyStructure, but this time, only give each of them one method: push(String s) (why do they only need one method now?)
3. Now you will write a driver to test your new MyStack and MyQueue structures. Make sure your pop() methods are popping in the correct order!
4. Copy these lines into the beginning of your test driver • String test = "This is the string that you will use to test the stack and queue classes."; • String currentWord; • char currentChar; • myQueue testQueue = new myQueue(); • myStack testStack = new myStack();
5. Write a program that parses the sentence into individual words, pushing each full word (with or without punctuation) onto the queue. print the full queue.**
6. Pop each element from the Queue and push each onto the stack. Then pop and print each item off of the stack. • DO NOT use the split() function of class String. You may use the substring function.
Explanation / Answer
import java.awt.List;
import java.util.*;
import java.util.EmptyStackException;
//Abstract class MyStructure definition
abstract class MyStructure
{
//Linked list object created to store string
protected LinkedList <String>item;
//Abstract method to pop a string from list
abstract public String pop();
//Method to check empty or not
abstract public boolean isEmpty();
}//End of class
//Class MyStack derived from MyStructure class
class MyStack extends MyStructure
{
//Constructor
public MyStack()
{
//Creates the linked list to store string data
item = new LinkedList<String>();
}//End of constructor.
//Overrides the pop() method for stack
public String pop()
{
//Checks if the list is empty throws an exception
if (item.isEmpty())
throw new EmptyStackException();
//Otherwise returns the data from the last
return item.remove(item.size()-1);
}//End of method
//Overrides the isEmpty() method for stack
public boolean isEmpty()
{
//returns true if list is empty
return item.isEmpty();
}//End of method
//Adds an element
public void push(String data)
{
// Add item to the stack top
item.add(data);
}//End of method
}//End of class
//Class MyQueue derived from MyStructure
class MyQueue extends MyStructure
{
//Constructor
public MyQueue()
{
//Creates the linked list to store string data
item = new LinkedList<String>();
}//End of constructor.
//Overrides pop() method for queue
public String pop()
{
//Checks if the list is empty throws an exception
if (item.isEmpty())
throw new EmptyStackException();
//Otherwise returns the data from the first
return item.removeFirst();
}//End of method
//Overrides the isEmpty() method for queue
public boolean isEmpty()
{
// Test if the queue is empty.
return item.isEmpty();
}//End of method
//Adds an item to the queue
public void push(String data)
{
// Add string to the queue at the end position
item.addLast(data);
}//End of method
}//End of class
//Driver class
public class MyStackQueue
{
//Main method definition
public static void main(String[] args)
{
//To store current word from the whole string
String currentWord="";
//Creates a string and assigns data
String test = "This is the string that you will use to test the stack and queue classes.";
//To store current character
char currentChar;
//Creates a MyStack class object
MyStack ms = new MyStack();
//Creates a MyQueue class object
MyQueue mq = new MyQueue();
//Adds space at the end of the string
test += " ";
//Loops till length of the rest string
for(int counter = 0; counter < test.length(); counter++)
{
//Extracts a character at the counter position
currentChar = test.charAt(counter);
//Checks if the character is not space
if(currentChar != ' ')
//Concatenate the character to the currentWord
currentWord += currentChar;
//Otherwise if it is space
else
{
//Add the word to the stack using push method
ms.push(currentWord);
//Add the word to the queue using push method
mq.push(currentWord);
//Re initialize the currentWord to null
currentWord = "";
}//End of else
}//End of for loop
System.out.println("************** STACK **************");
//Displays the stack contents till empty
while(!ms.isEmpty())
System.out.println("Popped Item: " + ms.pop());
System.out.println("************** QUEUE **************");
//Displays the queue contents till empty
while(!mq.isEmpty())
System.out.println("Popped Item: " + mq.pop());
}//End of method
}//End of class
Sample Run:
************** STACK **************
Popped Item: classes.
Popped Item: queue
Popped Item: and
Popped Item: stack
Popped Item: the
Popped Item: test
Popped Item: to
Popped Item: use
Popped Item: will
Popped Item: you
Popped Item: that
Popped Item: string
Popped Item: the
Popped Item: is
Popped Item: This
************** QUEUE **************
Popped Item: This
Popped Item: is
Popped Item: the
Popped Item: string
Popped Item: that
Popped Item: you
Popped Item: will
Popped Item: use
Popped Item: to
Popped Item: test
Popped Item: the
Popped Item: stack
Popped Item: and
Popped Item: queue
Popped Item: classes.