1 / 144

Chapter 13: Stacks and Queues

Chapter 13: Stacks and Queues. Stacks Applications of Stacks Implementation of a Stack Class Queues Implementation of the Queue Class Simulation. Stacks. Common in real world Papers Books CDs Plates Bowls What can we do with a stack? Put new item on top Take topmost item off.

mira
Download Presentation

Chapter 13: Stacks and Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 13: Stacks and Queues Stacks Applications of Stacks Implementation of a Stack Class Queues Implementation of the Queue Class Simulation

  2. Stacks • Common in real world • Papers • Books • CDs • Plates • Bowls • What can we do with a stack? • Put new item on top • Take topmost item off Programming and Problem Solving With Java

  3. Stacks: Operations • Java has a standard Stack class • java.util.Stack • Methods: • push(): put a new item on top of a stack • pop(): get the topmost item from the stack, and remove it from the stack • peek(): get the value of the topmost item from the stack, but don't remove the item from the stack • empty(): return true if the stack is empty • search(): tell how far a value is from the top of the stack, or -1 if the value is not in the stack • A stack is a LIFO structure • Last-in, first-out Programming and Problem Solving With Java

  4. Stacks: Push and Pop Programming and Problem Solving With Java

  5. Stacks: Demonstration • // This program is a simple demonstration of how to • // use Java's standard Stack class. • import java.util.Stack; • public class SimpleStackDemo • { • public static void main(String[] args) • throws java.io.IOException • { • Stack myStack = new Stack(); • // Push some values on the stack (all must be objects) • myStack.push("Hello"); • myStack.push(new Integer(123)); • myStack.push(new Double(5.678)); • // Display the stack • System.out.println("The contents of the stack: " + myStack); • // Pop the objects off the stack and display them • double dValue = ((Double) myStack.pop()).doubleValue(); • System.out.println("Popped " + dValue); • int iValue = ((Integer) myStack.pop()).intValue(); • System.out.println("Popped " + iValue); • String sValue = (String) myStack.pop(); • System.out.println("Popped " + sValue); • // Display the stack • System.out.println("The contents of the stack: " + myStack); • } • } The contents of the stack: [Hello, 123, 5.678] Popped 5.678 Popped 123 Popped Hello The contents of the stack: [] Programming and Problem Solving With Java

  6. Stacks: Demonstration • Longer demonstration of Java’s Stack class • // This program lets you push characters onto the • // stack, and pop them off. It makes sure you don't try to pop • // a character from an empty stack. This is a demonstration of • // how to use the Stack class. • import java.util.Stack; • import TextMenu; • import Keyboard; • public class StackDemoApp extends TextMenu • { • static final int PUSH_COMMAND = 1; • static final int POP_COMMAND = 2; • static final int TOP_COMMAND = 3; • // Constructor • public StackDemoApp() • { • super("Stack Test Menu"); • addSelection("Push a new element", '1', PUSH_COMMAND); • addSelection("Pop an element", '2', POP_COMMAND); • addSelection("Get topmost element", '3', TOP_COMMAND); • addSelection("Quit program", 'q', TextMenu.QUIT); • } Programming and Problem Solving With Java

  7. Stacks: Demonstration • StackDemoApp (continued) • // Handle each event as user selects a command • public int handleEvent(int event) • throws java.io.IOException • { • char character; • switch (event) • { • case PUSH_COMMAND: • character = Keyboard.readChar("Character to push: "); • charStack.push(new Character(character)); • break; • case POP_COMMAND: • if (charStack.empty()) • { • System.out.println("Stack is empty"); • } • else • { • character = ((Character) charStack.pop()).charValue(); • System.out.println("Character popped: " + character); • } • break; • case TOP_COMMAND: • if (charStack.empty()) • { • System.out.println("Stack is empty"); • } • else • { • character = ((Character) charStack.peek()).charValue(); • System.out.println("Character on top: " + character); • } • break; • } • return event; • } Programming and Problem Solving With Java

  8. Stacks: Demonstration • StackDemoApp (continued) • // Instance variables • private Stack charStack = new Stack(); • public static void main(String[] args) • throws java.io.IOException • { • StackDemoApp stackDemo = new StackDemoApp(); • stackDemo.run(); • } • } Programming and Problem Solving With Java

  9. Stacks: Demonstration • StackDemoApp (sample run) • +--- Stack Test Menu --- • | 1) Push a new element 2) Pop an element 3) Get topmost element • | q) Quit program • | Enter selection: 1 • Character to push: X • +--- Stack Test Menu --- • | 1) Push a new element 2) Pop an element 3) Get topmost element • | q) Quit program • | Enter selection: 1 • Character to push: Y • +--- Stack Test Menu --- • | 1) Push a new element 2) Pop an element 3) Get topmost element • | q) Quit program • | Enter selection: 2 • Character popped: Y • +--- Stack Test Menu --- • | 1) Push a new element 2) Pop an element 3) Get topmost element • | q) Quit program • | Enter selection: 2 • Character popped: X • +--- Stack Test Menu --- • | 1) Push a new element 2) Pop an element 3) Get topmost element • | q) Quit program • | Enter selection: q Programming and Problem Solving With Java

  10. Stacks: Postfix Expressions • Postfix notation • Also called reverse Polish notation (RPN) • Enter operator after operands • Example: 6+8 is 6 8 + in Postfix • Infix notation • Conventional notation: 6 + 8 • Advantages of postfix • Parentheses not needed (8 + 7) x (2 - 8)  8 7 + 2 8 - x • Easy to evaluate by computer Programming and Problem Solving With Java

  11. Stacks: Postfix Expressions • Evaluation of postfix expression • Get symbol from expression • If symbol is number, push on stack • If symbol is operator, pop two numbers from stack, do operation, push result back onto stack • Repeat until all symbols read from expression. At end, result is on top of stack Programming and Problem Solving With Java

  12. Example Evaluate 6 8 + Stacks: Postfix Expressions Programming and Problem Solving With Java

  13. Stacks: Postfix Expressions • Example: Program to evaluate postfix expressions • Single digit numbers only • No spaces • No error checking • // This program evaluates postfix expressions. The • // operators +, -, *, and /. This program does NO error checking. • import java.util.Stack; • import Keyboard; • public class EvaluatePostFix • { Programming and Problem Solving With Java

  14. Stacks: Postfix Expressions • // doOperation: Returns firstOperand oper secondOperand • // Note: oper must be '+', '-', '*', or '/'. If oper is '/', • // then secondOperand cannot be zero • static int doOperation(int firstOperand, int secondOperand, • char oper) • { • int returnValue = 0; • switch (oper) • { • case '+': • returnValue = firstOperand + secondOperand; • break; • case '-': • returnValue = firstOperand - secondOperand; • break; • case '*': • returnValue = firstOperand * secondOperand; • break; • case '/': • returnValue = firstOperand / secondOperand; • break; • } • return returnValue; • } Programming and Problem Solving With Java

  15. Stacks: Postfix Expressions • public static void main(String[] args) • throws java.io.IOException • { • Stack stack = new Stack(); • char inputChar; • System.out.println("--- Evaluate postfix expressions ---"); • System.out.println("--- (Single digit integers ONLY) ---"); • System.out.println(); • String expression • = Keyboard.readString("Enter a postfix expression: "); Programming and Problem Solving With Java

  16. Stacks: Postfix Expressions • for (int pos = 0; pos < expression.length(); pos++) • { • inputChar = expression.charAt(pos); • System.out.println(); • System.out.println("Next character is " + inputChar • + ", stack is " + stack); • switch (inputChar) • { • case '0': case '1': case '2': case '3': case '4': • case '5': case '6': case '7': case '8': case '9': • System.out.println(" Push " + inputChar); • stack.push(new Integer(Character.digit(inputChar, 10))); • break; • case '+': case '-': case '*': case '/': • int firstOperand = ((Integer) stack.pop()).intValue(); • System.out.println(" Pop " + firstOperand); • int secondOperand = ((Integer) stack.pop()).intValue(); • System.out.println(" Pop " + secondOperand); • int result = doOperation(secondOperand, firstOperand, • inputChar); • System.out.println(" Compute " + secondOperand • + " " + inputChar + " " • + firstOperand • + " and push result " + result); • stack.push(new Integer(result)); • break; • case ' ': • break; // Do nothing • } • } • System.out.println(" Pop result " + stack.pop()); • } • } (Debugging statements in blue) Programming and Problem Solving With Java

  17. Stacks: Postfix Expressions • Sample run of EvaluatePostFix: 6 8 + • --- Evaluate postfix expressions --- • --- (Single digit integers ONLY) --- • Enter a postfix expression: 6 8 + • Next character is 6, stack is [] • Push 6 • Next character is , stack is [6] • Next character is 8, stack is [6] • Push 8 • Next character is , stack is [6, 8] • Next character is +, stack is [6, 8] • Pop 8 • Pop 6 • Compute 6 + 8 and push result 14 • Pop result 14 Programming and Problem Solving With Java

  18. Stacks: Postfix Expressions • Another evaluation: 8 7 + 2 8 - x --- Evaluate postfix expressions --- --- (Single digit integers ONLY) --- Enter a postfix expression: 8 7 + 2 8 - * Next character is 8, stack is [] Push 8 Next character is , stack is [8] Next character is 7, stack is [8] Push 7 Next character is , stack is [8, 7] Next character is +, stack is [8, 7] Pop 7 Pop 8 Compute 8 + 7 and push result 15 Next character is , stack is [15] Next character is 2, stack is [15] Push 2 Next character is , stack is [15, 2] Next character is 8, stack is [15, 2] Push 8 Next character is , stack is [15, 2, 8] Next character is -, stack is [15, 2, 8] Pop 8 Pop 2 Compute 2 - 8 and push result -6 Next character is , stack is [15, -6] Next character is *, stack is [15, -6] Pop -6 Pop 15 Compute 15 * -6 and push result -90 Programming and Problem Solving With Java

  19. Stacks: Translate Infix to Postfix • Translation of infix to postfix • If we can translate expression to postfix, then can use EvalPostfix to evaluate • Can use a stack to convert infix to postfix • Will require fully parenthesized infix expressions • Fully parenthesized: parentheses for each operator • Example: (1 + (8 x 2)) • Not fully parenthsized: ((9 + 3 - 2) / 5) • Keeps translation algorithm simple Programming and Problem Solving With Java

  20. Stacks: Translate Infix to Postfix • Translation of infix to postfix by hand Programming and Problem Solving With Java

  21. Stacks: Translate Infix to Postfix • Algorithm to translate infix to postfix • For each input character • If character is operand, output character • If character is operator, push character on stack • If character is right parenthesis, pop operator and output operator • If character is left parenthesis, ignore • Resulting output is postfix equivalent of infix expression Programming and Problem Solving With Java

  22. Stacks: Translate Infix to Postfix • Example: translate (1 + (8 * 2)) Programming and Problem Solving With Java

  23. Stacks: Activation Records • Computer uses run-time stack • Each running method has activation record on the stack • Keeps track of method calls and returns • Each method’s activation record contains • Return point • Local variable values • Parameter values • Stack works well here • if A calls B, then B must finish before A can resume • Last-in, first-out  stack Programming and Problem Solving With Java

  24. Stacks: Activation Records (DPL) • Example: Simple programming language DPL • No parameters • No variables • No return values • Basically just method calls • DPL statements • COMMENT: ignored • TYPE: display argument • END: end program (ends automatically if no more statements) • METHOD: Begin method definition • RETURN: Return from method • CALL: Call method Programming and Problem Solving With Java

  25. Stacks: Activation Records (DPL) • Control starts with first statement (ignores COMMENT) • First statement should be TYPE, END, or CALL • Example DPL program • CALL SomeMethod • TYPE Finished! • END • METHOD SomeMethod • TYPE Now in SomeMethod • RETURN • Output • Now in SomeMethod • Finished! • Next slide shows activation records on run-time stack for this program Programming and Problem Solving With Java

  26. Programming and Problem Solving With Java

  27. Stacks: Activation Records (DPL) • COMMENT This is an example of the • COMMENT demonstration programming language DPL. • COMMENT It shows how methods work. • COMMENT ---- Main program ---- • TYPE >> Starting main program • TYPE -- Call FirstMethod from main ... • CALL FirstMethod • TYPE -- Back in main from FirstMethod • TYPE -- Call SecondMethod from main ... • CALL SecondMethod • TYPE -- Back in main from SecondMethod • TYPE << Ending main program • END • METHOD FirstMethod • TYPE >> Entering FirstMethod • TYPE << Leaving FirstMethod • RETURN • METHOD SecondMethod • TYPE >> Entering SecondMethod • TYPE -- Call FirstMethod from SecondMethod ... • CALL FirstMethod • TYPE -- Back in SecondMethod from FirstMethod • TYPE << Leaving SeconMMdMethod • RETURN >> Starting main program -- Call FirstMethod from main ... >> Entering FirstMethod << Leaving FirstMethod -- Back in main from FirstMethod -- Call SecondMethod from main ... >> Entering SecondMethod -- Call FirstMethod from SecondMethod ... >> Entering FirstMethod << Leaving FirstMethod -- Back in SecondMethod from FirstMethod << Leaving SecondMethod -- Back in main from SecondMethod << Ending main program Programming and Problem Solving With Java

  28. Stacks: Activation Records (DPL) • DPL interpreter logic • Read program from file into memory • Set program counter to first statement • While haven't reached END statement or past end • If TYPE: display the argument • If CALL: push address of CALL statement, set program counter to address of METHOD statement • If RETURN: pop program counter • If COMMENT or blank line: ignore line • If METHOD: error • Increment program counter Programming and Problem Solving With Java

  29. Stacks: Activation Records (DPL) • Error if control reaches METHOD statement • TYPE The next statement should give a run-time error • METHOD Wrong • TYPE Control shouldn't reach here! • RETURN • Output • $ java TestDPL test1.dpl • The next statement should give a run-time error • Error: METHOD without CALL • 2: METHOD Wrong • Program halted Programming and Problem Solving With Java

  30. Stacks: Activation Records (DPL) • // This program implements an interpreter for a • // small demonstration programming language (DPL). Each line • // in DPL is a statement. Each statement begins with a keyword • // and may be followed by an argument. The statements are: • // COMMENT: Line is ignored by the interpreter. • // TYPE: Display the argument (rest of the line) on the • // screen. • // END: End the program. The program ends automatically • // if there are no more statements. • // METHOD: Begin a method definition. The name follows. • // RETURN: Return from a method. Each method must have • // a RETURN statement. • // CALL: Call a method by name. • // DPL is case insensitive, and allows blank lines. • import java.io.*; • import java.util.Stack; • // ----------------- DPLstatement class ------------------ • class DPLstatement • { • static final int TYPE_COMMAND = 1; • static final int END_COMMAND = 2; • static final int METHOD_COMMAND = 3; • static final int RETURN_COMMAND = 4; • static final int CALL_COMMAND = 5; • static final int COMMENT_COMMAND = 6; • static final int BLANK_LINE = 7; • static final int UNKNOWN_COMMAND = 8; DPL Interpreter (TestDPL.java) Programming and Problem Solving With Java

  31. Stacks: Activation Records (DPL) • // Constructor: Converts a program line into an internal form • public DPLstatement(String line) • { • String commandString; • // Remove blanks on both ends of line • line = line.trim(); • // Look for first blank (separates command from argument) • int blankLoc = line.indexOf(" "); • // Separate command from argument • if (blankLoc == -1) • { • commandString = line.toUpperCase(); • } • else • { • commandString • = line.substring(0, blankLoc).toUpperCase(); • argument = line.substring(blankLoc + 1).trim(); • } DPL Interpreter (TestDPL.java) Programming and Problem Solving With Java

  32. Stacks: Activation Records (DPL) • // Convert command to internal form • if (commandString.equals("TYPE")) • { • command = TYPE_COMMAND; • } • else if (commandString.equals("END")) • { • command = END_COMMAND; • } • else if (commandString.equals("METHOD")) • { • command = METHOD_COMMAND; • } • else if (commandString.equals("RETURN")) • { • command = RETURN_COMMAND; • } • else if (commandString.equals("CALL")) • { • command = CALL_COMMAND; • } • else if (commandString.equals("COMMENT")) • { • command = COMMENT_COMMAND; • } • else if (commandString.trim().equals("")) • { • command = BLANK_LINE; • } • else • { • command = UNKNOWN_COMMAND; • argument = line; • } • } DPL Interpreter (TestDPL.java) Programming and Problem Solving With Java

  33. Stacks: Activation Records (DPL) • // getCommand: Returns the command as an integer • public int getCommand() • { • return command; • } • // getArgument: Returns the argument as a string (null if no • // argument) • public String getArgument() • { • return argument; • } DPL Interpreter (TestDPL.java) Programming and Problem Solving With Java

  34. Stacks: Activation Records (DPL) • // toString: Returns the statement as a string (for error • // messages) • public String toString() • { • String result = ""; • switch (command) • { • case TYPE_COMMAND: result = "TYPE "; break; • case END_COMMAND: result = "END"; break; • case METHOD_COMMAND: result = "METHOD "; break; • case RETURN_COMMAND: result = "RETURN"; break; • case CALL_COMMAND: result = "CALL "; break; • case COMMENT_COMMAND: result = "COMMENT "; break; • case BLANK_LINE: result = ""; break; • case UNKNOWN_COMMAND: result = ""; break; • } • if (argument != null) • { • result = result + argument; • } • return result; • } • private int command; • private String argument; • } DPL Interpreter (TestDPL.java) Programming and Problem Solving With Java

  35. Stacks: Activation Records (DPL) • // ----------------- DPL class ------------------ • class DPL • { • static final int MAX_STATEMENTS = 100; // Maximum number of • // statements in • // a program • // Constructor: Reads the program from the given file, returns • // true if successful • public DPL(String fileName) • throws java.io.IOException • { • String line; • // Initialize the input file variable • File inFile = new File(fileName); • if (inFile.exists() && inFile.canRead()) • { • // Create an input stream and attach it to the file • BufferedReader fileInStream • = new BufferedReader(new FileReader(inFile)); DPL Interpreter (TestDPL.java) Programming and Problem Solving With Java

  36. Stacks: Activation Records (DPL) • // Read lines from the file into the array • line = fileInStream.readLine(); • while (numStatements < statement.length • && line != null) • { • statement[numStatements] • = new DPLstatement(line); • numStatements++; • line = fileInStream.readLine(); • } • // Make sure all lines were read • if (line != null) • { • error("Too many statements"); • } • // Close the stream • fileInStream.close(); • } • else • { • // Can't open input file • error("Can't open input file " + fileName); • } • } DPL Interpreter (TestDPL.java) Programming and Problem Solving With Java

  37. Stacks: Activation Records (DPL) • // interpretProgram: Interprets the statements of the program • public void interpretProgram() • { • Stack runTimeStack = new Stack(); • boolean stop = false; • String argument; • // Step through the program, executing statements • while (programCounter < numStatements && !stop) • { • switch (statement[programCounter].getCommand()) • { • case DPLstatement.TYPE_COMMAND: • argument • = statement[programCounter].getArgument(); • if (argument == null) • { • argument = ""; • } • System.out.println(argument); • break; • case DPLstatement.END_COMMAND: • stop = true; • break; • case DPLstatement.METHOD_COMMAND: • error("METHOD without CALL"); • break; DPL Interpreter (TestDPL.java) Programming and Problem Solving With Java

  38. Stacks: Activation Records (DPL) • case DPLstatement.RETURN_COMMAND: • if (runTimeStack.empty()) • { • error("RETURN command without CALL"); • } • else • { • programCounter • = ((Integer) runTimeStack.pop()).intValue(); • } • break; • case DPLstatement.CALL_COMMAND: • runTimeStack.push(new Integer(programCounter)); • programCounter • = findMethod(statement • [programCounter].getArgument()); • break; • case DPLstatement.COMMENT_COMMAND: • case DPLstatement.BLANK_LINE: • // Do nothing • break; • case DPLstatement.UNKNOWN_COMMAND: • error("Unknown command"); • break; • } • programCounter++; • } • } DPL Interpreter (TestDPL.java) Programming and Problem Solving With Java

  39. Stacks: Activation Records (DPL) • // findMethod: Returns the location of the METHOD command • // with the given name • private int findMethod(String methodName) • { • methodName.toUpperCase(); • for (int position = 0; • position < numStatements; • position++) • { • if (statement[position].getCommand() • == DPLstatement.METHOD_COMMAND • && statement[position].getArgument(). • equals(methodName)) • { • return position; • } • } • error("Undefined METHOD " + methodName); • return 0; • } DPL Interpreter (TestDPL.java) Programming and Problem Solving With Java

  40. Stacks: Activation Records (DPL) • // error: Displays the error message and halts the program • private void error(String message) • { • System.out.println("Error: " + message); • if (statement[programCounter] != null) • { • System.out.println((programCounter + 1) + ": " • + statement[programCounter]); • } • System.out.println("Program halted"); • System.exit(2); • } • // Instance variables • DPLstatement[] statement = new DPLstatement[MAX_STATEMENTS]; • int programCounter = 0, numStatements = 0; • } DPL Interpreter (TestDPL.java) Programming and Problem Solving With Java

  41. Stacks: Activation Records (DPL) • // ----------------- TestDPL class ------------------ • public class TestDPL • { • public static void main(String[] args) • throws java.io.IOException • { • DPL program = new DPL(args[0]); • program.interpretProgram(); • } • } DPL Interpreter (TestDPL.java) Programming and Problem Solving With Java

  42. Stacks: Activation Records (DPL2) • DPL2: Add a LOOP statement to DPL • Syntax • LOOP • ... • ASK x • ... • AGAIN • LOOP -- marks the top of the loop • ASK x -- user enters character, if it matches x, control goes to statement after AGAIN, otherwise statement after ASK • AGAIN -- jump to LOOP Programming and Problem Solving With Java

  43. Loop statement example LOOP TYPE What's 4 + 5? ASK 9 TYPE No, try again. AGAIN TYPE That's right! Output What's 4 + 5? 8 No, try again. What's 4 + 5? 9 That's right! Nested loop example LOOP TYPE Enter the number 1 ASK 1 LOOP TYPE Do you know your numbers? (y/n) ASK y TYPE A number is between 0 and 9 AGAIN AGAIN TYPE Correct! Output Enter the number 1 X Do you know your numbers? (y/n) n A number is between 0 and 9 Do you know your numbers? (y/n) y Enter the number 1 1 Correct! Stacks: Activation Records (DPL2) Programming and Problem Solving With Java

  44. Stacks: Activation Records (DPL2) • More realistic example of DPL2 (capitals.dpl) • COMMENT This is an example of the second version • COMMENT of the demonstration programming language. This • COMMENT version has loops of the form LOOP ... ASK ... AGAIN. • CALL Introduction • LOOP • CALL USAcapital • CALL NewZealandCapital • CALL GreatBritainCapital • CALL AustraliaCapital • TYPE Do you want to take this quiz again (y/n)? • ASK n • AGAIN • TYPE Bye! • END • METHOD Introduction • TYPE This is a quiz that tests your knowledge of country • TYPE capitals. • TYPE Here is the first question ... • TYPE • RETURN Programming and Problem Solving With Java

  45. Stacks: Activation Records (DPL2) • METHOD AustraliaCapital • LOOP • TYPE What is the capital city of Australia? • TYPE 1. Canberra • TYPE 2. London • TYPE 3. Washington, DC • TYPE 4. Wellington • ASK 1 • TYPE Hint: The design of the capital of Australia won an • TYPE international competition in 1911. • AGAIN • RETURN • METHOD GreatBritainCapital • LOOP • TYPE What is the capital city of Great Britain? • TYPE 1. Canberra • TYPE 2. London • TYPE 3. Washington, DC • TYPE 4. Wellington • ASK 2 • TYPE Hint: The capital of Great Britain is on both sides • TYPE of the Thames River. • AGAIN • RETURN Programming and Problem Solving With Java

  46. Stacks: Activation Records (DPL2) • METHOD NewZealandCapital • LOOP • TYPE What is the capital city of New Zealand? • TYPE 1. Canberra • TYPE 2. London • TYPE 3. Washington, DC • TYPE 4. Wellington • ASK 4 • TYPE Hint: The capital of New Zealand is at the southern tip of • TYPE Cook Strait. • AGAIN • RETURN • METHOD USAcapital • LOOP • TYPE What is the capital city of the United States of America? • TYPE 1. Canberra • TYPE 2. London • TYPE 3. Washington, DC • TYPE 4. Wellington • ASK 3 • TYPE Hint: The capital of the United States of America was named • TYPE after it's first president. • AGAIN • RETURN Programming and Problem Solving With Java

  47. Stacks: Activation Records (DPL2) • Sample run of capitals.dpl • This is a quiz that tests your knowledge of country • capitals. • Here is the first question ... • What is the capital city of the United States of America? • 1. Canberra • 2. London • 3. Washington, DC • 4. Wellington • 2 • Hint: The capital of the United States of America was named • after it's first president. • What is the capital city of the United States of America? • 1. Canberra • 2. London • 3. Washington, DC • 4. Wellington • 3 Programming and Problem Solving With Java

  48. Stacks: Activation Records (DPL2) • Sample run of capitals.dpl (continued) • What is the capital city of New Zealand? • 1. Canberra • 2. London • 3. Washington, DC • 4. Wellington • 4 • What is the capital city of Great Britain? • 1. Canberra • 2. London • 3. Washington, DC • 4. Wellington • 2 • What is the capital city of Australia? • 1. Canberra • 2. London • 3. Washington, DC • 4. Wellington • 1 • Do you want to take this quiz again (y/n)? • n • Bye! Programming and Problem Solving With Java

  49. Stacks: Activation Records (DPL2) • // This program implements DPL2. It extends the DPL • // small demonstration programming language (DPL). Each line • // in DPL is a statement. Each statement begins with a keyword • // and may be followed by an argument. The statements are: • // COMMENT: Line is ignored by the interpreter. • // TYPE: Display the argument (rest of the line) on the • // screen. • // END: End the program. The program ends automatically • // if there are no more statements. • // METHOD: Begin a method definition. The name follows. • // RETURN: Return from a method. Each method must have • // a RETURN statement. • // CALL: Call a method by name. • // LOOP ... ASK ... AGAIN: • // The LOOP command begins a loop. The ASK command • // asks the user for a response, and compares it to • // the single character argument. If equal, control • // continues with the statement following AGAIN. If • // not equal, control continues with the statement • // following ASK. When control reaches the AGAIN • // command, it continues with the statement following • // the associated LOOP command. • // DPL2 is case insensitive, and allows blank lines. DPL2 Interpreter (TestDPL2.java) Programming and Problem Solving With Java

  50. Stacks: Activation Records (DPL2) • import Keyboard; • import java.io.*; • import java.util.Stack; • // ----------------- DPLstatement class ------------------ • class DPLstatement • { • static final int TYPE_COMMAND = 1; • static final int END_COMMAND = 2; • static final int METHOD_COMMAND = 3; • static final int RETURN_COMMAND = 4; • static final int CALL_COMMAND = 5; • static final int COMMENT_COMMAND = 6; • static final int BLANK_LINE = 7; • static final int UNKNOWN_COMMAND = 8; • static final int LOOP_COMMAND = 9; • static final int ASK_COMMAND = 10; • static final int AGAIN_COMMAND = 11; DPL2 Interpreter (TestDPL2.java) Programming and Problem Solving With Java

More Related