Question

In: Computer Science

For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...

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):

  • pop: This command is the same as the definition of the pop() method for a stack.
    35 -100 22 pop
    -> -100 35
    
  • dup: replicates the top element of the stack and pushes it on the stack.
    13 12 dup
    -> 12 12 13
    
  • exch: exchanges the top two items.
    13 -50 exch
    -> 13 -50 
    
  • clear: empties out the stack.
    1 -50 22 100 99 6 clear
    -> <empty>
    
  • n index: 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.
    34 12 92 2 index
    -> 12 92 12 34
    
  • n copy: 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.
    12 6 2 copy
    -> 12 6 6 12
    
  • count: the number of items on the stack and push that value onto the stack.
    10 20 30 40 50 count
    -> 5 50 40 30 20 10
    

Programming Notes:

  1. You need not add any methods you don't need.
  2. You can add or delete any methods. You can add or delete any code.
  3. You must use a singly-linked list but you can choose your own list implementation such-as with or without sentinels, with head and/or tail points, etc.
  4. Do not assume any maximum length for any input string.
  5. You need not test for the null or empty string ("") cases.

Programming Rules:

  1. You are not allowed to change the signature of the three methods above.
  2. You are not allowed to add any arrays or ArrayLists or any Java built-in (ADTs), such as Lists, Sets, Maps, Stacks, Queues, Deques, Trees, Graphs, Heaps, etc. Or add any class that inherits any of those ADTs.
  3. For your node and list class you can use the code that was used in the book, video and lecture notes related to the node and lists class examples.
  4. You are not allowed to use Java Generics.
  5. You can use any data type to represent the digits (in the node). However, each node must represent one and only one digit.
  6. If hard-coding detected in any part of your solution your score will be zero for the whole assignment.

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

}

}

Solutions

Expert Solution

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! :)


Related Solutions

For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
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...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
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...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
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...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented with an infix notation), then outputs this expression in prefix form and also outputs the result of the calculation. The program will first convert the input infix expression to a prefix expression (using the Stack ADT) and then calculate the result (again, using the Stack ADT). The details are provided in the following sections.
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going to implement a simple C version of the UNIX cat program called lolcat. The cat program allows you to display the contents of one or more text files. The lolcat program will only display one file. The correct usage of your program should be to execute it on the command line with a single command line argument consisting of the name you want to...
Describe a branded fitness program (P90X®, Bodypump®, Zumba®, Crossfit, etc.) in which you have participated. Describe...
Describe a branded fitness program (P90X®, Bodypump®, Zumba®, Crossfit, etc.) in which you have participated. Describe the short term and long term adaptations of these programs, including their benefits and limitations. Explain how the program you chose could fit into a comprehensive fitness plan.
which tips about reading and writing from the readings have you used specifically in your writing...
which tips about reading and writing from the readings have you used specifically in your writing process?
The sixth assignment involves writing a Python program to read in the temperatures for ten consecutive...
The sixth assignment involves writing a Python program to read in the temperatures for ten consecutive days in Celsius and store them into an array. The entire array should then be displayed. Next each temperature in the array should be converted to Fahrenheit and the entire array should be again be displayed. The formula for converting Celsius to Fahrenheit is °F = (°C × 1.8) + 32. Finally, the number of cool, warm and hot days should be counted and...
This writing assignment will involve implementing and analyzing a behavior modification program on yourself. The behavior...
This writing assignment will involve implementing and analyzing a behavior modification program on yourself. The behavior is ANXIETY OF MEETING NEW PEOPLE. This assignment is to develop your own functional behavior assessment. A minimum of 3 paragraphs per question is required. Modification Plan: In this section, you need to describe your assessment and intervention plan. How will you collect data on the behavior in order to evaluate whether or not it is changing? Describe any forms or graphs you plan...
Create a C program that has a sequence of at least 8 linux commands you would...
Create a C program that has a sequence of at least 8 linux commands you would execute to display what each command does. Explain what each command is doing and show the output of them.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT