In: Computer Science
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL:
Tutorialspoint - Stack (Links to an external site.)
The stack description above uses an array implementation for examples. You will be not be using arrays, you will be using linked-lists. PostScript is known as a "page description" programming language, because it is commonly used to specify how a page should be printed. PostScript is a sequence of commands in the PostScript language. You can learn more about PostScript from this URL:
PostScript (Wikipedia) (Links to an external site.)
PostScript commands and data are processed by using a stack. You'll see that stacks have a distinctive characteristic that all of its operations are done at the beginning or "top" of the structure. Your job will be to implement the four basic stack methods which are push(), pop(), peek() and isEmpty() along with other specific methods to handle PostScript commands.
PostScript data are pushed onto the stack. Below are the PostScript commands you must implement along with their descriptions and examples, where "->" signifies the top of he stack (blue designates input, red designates output):
35 -100 22 pop -> -100 35
13 12 dup -> 12 12 13
13 -50 exch -> 13 -50
1 -50 22 100 99 6 clear -> <empty>
34 12 92 2 index -> 12 92 12 34
12 6 2 copy -> 12 6 6 12
10 20 30 40 50 count -> 5 50 40 30 20 10
Programming Notes:
Programming Rules:
Please use starter code:
import java.util.Scanner; // Import the Scanner class
public class Homework6 {
public static void main(String[] args) {
PSStack a = makeStack();
a.displayStack();
}
public static PSStack makeStack() {
PSStack temp = new PSStack();
Scanner input = new Scanner(System.in);
String s = input.nextLine();
String[] array = s.split(" ");
for (String command : array){
temp.evaluate(command);
}
return temp;
}
}
class StackNode {
public String data;
public StackNode next;
public StackNode (String s) {
data = s;
next = null;
}
}
class Stack {
public StackNode top;
public int size;
public Stack() {
top = null;
}
public void push(String command) {
StackNode temp = null;
if (top == null) {
top = new StackNode(command);
}
else {
temp = top;
top = new StackNode(command);
top.next = temp;
}
}
public StackNode pop() {
if (isEmpty()) {
System.out.println("Not enough items on the stack to perform this operation.");
return null;
}
else {
StackNode temp = top;
top = top.next;
return temp;
}
}
public String peek() {
return null;
}
public boolean isEmpty() {
return true;
}
}
class PSStack extends Stack {
public void displayStack() {
System.out.print("-> ");
System.out.print("<empty>");
System.out.println("Not enough items on the stack to perform this operation."); //copy and paste this anywhere you need
// put your solution here
}
public void exch() {
// put your solution here
}
public void dup() {
// put your solution here
}
public void clear() {
// put your solution here
}
public void count() {
// put your solution here
}
public void index() {
// put your solution here
}
public void copy() {
// put your solution here
}
public void evaluate(String command) {
// put your solution here
}
}
Hello! :)
Here's the documented code to solve the assignment:
Homework6.java:
import java.util.Scanner; // Import the Scanner class
public class Homework6 {
/**
* Driver to test out PSStack class.
*/
public static void main(String[] args) {
PSStack a = makeStack();
a.displayStack();
}
/**
* Makes stack according to the input.
* @return The processed PSStack instance.
*/
public static PSStack makeStack() {
PSStack temp = new PSStack();
Scanner input = new Scanner(System.in);
String s = input.nextLine();
String[] array = s.split(" ");
for (String command : array){
temp.evaluate(command);
}
return temp;
}
}
class StackNode {
public String data;
public StackNode next;
/**
* Constructor to initialize the member variables of StackNode.
* @param s The String to initialize data with.
*/
public StackNode (String s) {
data = s;
next = null;
}
}
class Stack {
public StackNode top;
public int size;
/**
* Constructor to initialize top to null and size to 0.
*/
public Stack() {
top = null;
size = 0;
}
/**
* Pushes the String command onto the stack top.
* @param command The String to be pushed on top of the stack.
*/
public void push(String command) {
StackNode temp = null;
if (top == null) {
top = new StackNode(command);
}
else {
temp = top;
top = new StackNode(command);
top.next = temp;
}
++size;
}
/**
* Pops the top StackNode instance from the stack and returns it, otherwise returns null.
* @return The topmost StackNode instance or null.
*/
public StackNode pop() {
if (isEmpty()) {
System.out.println("Not enough items on the stack to perform this operation.");
return null;
}
else {
StackNode temp = top;
top = top.next;
--size;
return temp;
}
}
/**
* Returns the data of the topmost StackNode instance.
* @return The data of the topmost StackNode instance.
*/
public String peek() {
if (isEmpty()) {
System.out.println("Not enough items on the stack to perform this operation.");
return null;
}
else {
return top.data;
}
}
/**
* Returns whether stack is empty
* @return True if stack is empty and false otherwise.
*/
public boolean isEmpty() {
return size == 0;
}
}
class PSStack extends Stack {
/**
* Displays the contents of the stack.
*/
public void displayStack() {
System.out.print("-> ");
if (top == null) {
System.out.print("<empty>");
}
else {
while (top != null) {
System.out.print(top.data + ' ');
top = top.next;
}
}
// System.out.println("Not enough items on the stack to perform this operation."); //copy and paste this anywhere you need
// put your solution here
System.out.println();
}
/**
* Exchanges the top two items.
*/
public void exch() {
// put your solution here
String firstCommand = pop().data;
String secondCommand = pop().data;
push(firstCommand);
push(secondCommand);
}
/**
* Replicates the top element of the stack and pushes it on the stack.
*/
public void dup() {
// put your solution here
push(top.data);
}
/**
* Empties out the stack.
*/
public void clear() {
// put your solution here
top = null;
}
/**
* The number of items on the stack and push that value onto the stack.
*/
public void count() {
// put your solution here
push(Integer.toString(size));
}
/**
* Pops the n, counts down n items into the stack and then pushes a copy of the nth element to the stack; clear the stack if there are not enough elements.
*/
public void index() {
// put your solution here
String nString = pop().data;
if (nString != null) {
int n = Integer.parseInt(nString);
if(n > size) {
// not enough elements
clear();
return;
}
// counting down n items into the stack
StackNode temp = top;
while (--n > 0) {
temp = temp.next;
}
// pushing a copy of the nth elemenst to the stack
push(temp.data);
}
}
/**
* Pops the n, and then pushes a copy of the top n elements onto the stack; clear the stack if there are not enough elements.
*/
public void copy() {
// put your solution here
String nString = pop().data;
if (nString != null) {
int n = Integer.parseInt(nString);
if(n > size) {
// not enough elements
clear();
return;
}
// pushing copies of the top n elements onto the stack
StackNode temp = top;
while (n-- > 0) {
push(temp.data);
temp = temp.next;
}
}
}
/**
* Routes the commands parsed to the correct function calls.
*/
public void evaluate(String command) {
// put your solution here
if (command.equals("pop")) {
pop();
}
else if (command.equals("dup")) {
dup();
}
else if (command.equals("exch")) {
exch();
}
else if (command.equals("clear")) {
clear();
}
else if (command.equals("index")) {
index();
}
else if (command.equals("copy")) {
copy();
}
else if (command.equals("count")) {
count();
}
else {
push(command);
}
}
}
Here is a snapshot of a demo run:
Hope this helps! :)