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

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

import java.util.Scanner;

 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);
    }
    input.close();
    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;
    }
    size++;
  }

  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;
    }
  }

  public String peek() {
    if (isEmpty()) {
      System.out.println(
        "Not enough items on the stack to perform this operation."
      );
      return null;
    } else {
      StackNode temp = top;
      return temp.data;
    }
  }

  public boolean isEmpty() {
    if (size == 0) return true;
    return false;
  }
}

class PSStack extends Stack {

  public void displayStack() {
    System.out.print("-> ");

    if (size == 0) {
      System.out.print("<empty>");
      return;
    }

    StackNode temp = top;
    while (temp != null) {
      System.out.print(temp.data + " ");
      temp = temp.next;
    }
  }

  public void exch() {
    String top_element = pop().data;
    String second_element = pop().data;
    push(top_element);
    push(second_element);
  }

  public void dup() {
    String top_element = peek();
    push(top_element);
  }

  public void clear() {
    while (size > 0) {
      pop();
    }
  }

  public void count() {
    //count: the number of items on the stack and push that value onto the stack.
    push(Integer.toString(size));
  }

  public void index() {
    // 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.
    int n = Integer.parseInt(pop().data);
    int i = 0;

    if (size < n) {
      clear();
      return;
    }

    StackNode element = top;
    for (i = 0; i < n - 1; i++) {
      element = element.next;
    }
    push(element.data);
  }

  public void copy() {
    //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.
    int n = Integer.parseInt(pop().data);
    int i = 0;

    if (size < n) {
      clear();
      return;
    }

    //store n elements into a temporary stack temp
    PSStack temp = new PSStack();

    StackNode element = top;
    for (i = 0; i < n - 1; i++) {
      temp.push(element.data);
      element = element.next;
    }
    temp.push(element.data);

    // now we will push all elements stored into original stack
    if (temp.isEmpty() == true) {
      return;
    }
    StackNode e = temp.pop();
    while (!temp.isEmpty()) {
      push(e.data);
      e = temp.pop();
    }
    push(e.data);
  }

  public void evaluate(String command) {
    if (command.equalsIgnoreCase("pop")) {
      pop();
    } else if (command.equalsIgnoreCase("dup")) {
      dup();
    } else if (command.equalsIgnoreCase("exch")) {
      exch();
    } else if (command.equalsIgnoreCase("index")) {
      index();
    } else if (command.equalsIgnoreCase("copy")) {
      copy();
    } else if (command.equalsIgnoreCase("count")) {
      count();
    } else if (command.equalsIgnoreCase("clear")) {
      clear();
    } else if (command.matches("-?(0|[1-9]\\d*)")) {
      push(command);
    } else {
      System.out.println("Invalid Input");
    }
  }
}

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...
The second assignment involves writing a Python program to compute the price of a theater ticket....
The second assignment involves writing a Python program to compute the price of a theater ticket. Your program should prompt the user for the patron's age and whether the movie is 3D. Children and seniors should receive a discounted price. There should be a surcharge for movies that are 3D. You should decide on the age cutoffs for children and seniors and the prices for the three different age groups. You should also decide on the amount of the surcharge...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT