fork download
  1. Tape.java
  2.  
  3. /*
  4. A Turing machine works on a "tape" that is used for both input and output. The tape is made up of little squares called cells lined up in a horizontal row that stretches, conceptually, off to infinity in both directions. Each cell can hold one character. Initially, the content of a cell is a blank space. One cell on the tape is considered to be the current cell. This is the cell where the machine is located. As a Turing machine computes, it moves back and forth along the tape, and the current cell changes.
  5. A Turing machine tape can be represented by a doubly-linked list where each cell has a pointer to the previous cell (to its left) and to the next cell (to its right). The pointers will allow the machine to advance from one cell to the next cell on the left or to the next cell on the right. Each cell can be represented as an object of type Cell as defined by the class:
  6. public class Cell {
  7. public char content; // The character in this cell.
  8. public Cell next; // Pointer to the cell to the right of this one.
  9. public Cell prev; // Pointer to the cell to the left of this one.
  10. }
  11. This class is already defined in the file Cell.java, so you don't have to write it yourself.
  12. ________________________________________
  13. Your task is to write a class named Tape to represent Turing machine tapes. The class should have an instance variable of type Cell that points to the current cell. To be compatible with the classes that will use the Tape class, your class must include the following methods:
  14. ג€¢ public Cell getCurrentCell() -- returns the pointer that points to the current cell.
  15. ג€¢ public char getContent() -- returns the char from the current cell.
  16. ג€¢ public void setContent(char ch) -- changes the char in the current cell to the specified value.
  17. ג€¢ public void moveLeft() -- moves the current cell one position to the left along the tape. Note that if the current cell is the leftmost cell that exists, then a new cell must be created and added to the tape at the left of the current cell, and then the current cell pointer can be moved to point to the new cell. The content of the new cell should be a blank space. (Remember that the Turing machine's tape is conceptually infinite, so your linked list must must be prepared to expand on demand, when the machine wants to move past the current end of the list.)
  18. ג€¢ public void moveRight() -- moves the current cell one position to the right along the tape. Note that if the current cell is the rightmost cell that exists, then a new cell must be created and added to the tape at the right of the current cell, and then the current cell pointer can be moved to point to the new cell. The content of the new cell should be a blank space.
  19. ג€¢ public String getTapeContents() -- returns a String consisting of the chars from all the cells on the tape, read from left to right, except that leading or trailing blank characters should be discarded. The current cell pointer should not be moved by this method; it should point to the same cell after the method is called as it did before. You can create a different pointer to move along the tape and get the full contents. (This method is the hardest one to implement.)
  20. It is also useful to have a constructor that creates a tape that initially consists of a single cell. The cell should contain a blank space, and the current cell pointer should point to it. (The alternative -- letting the current cell pointer be null to represent a completely blank tape -- makes all the methods in the class more difficult to implement.)
  21. To test your Tape class, you can should run the programs that are defined by the files TestTape.java, TestTapeGUI.java, and TestTuringMachine.java. The first two programs just do things with a tape, to test whether it is functioning properly. TestTuringMachine actually creates and runs several Turing machines, using your Tape class to represent the machines' tapes.
  22. */
  23.  
  24. package turing;
  25.  
  26. public class Tape {
  27.  
  28. private Cell currentCell; // Current cell pointer
  29.  
  30. public Tape() { //Constructor to create a blank tape with a single cell, which contains a blank space.
  31. Cell newCell = new Cell();
  32. newCell.content = ' ';
  33. newCell.prev = null;
  34. newCell.next = null;
  35. currentCell = newCell;
  36. }
  37.  
  38. public Cell getCurrentCell() { //The pointer to current cell.
  39. return currentCell;
  40. }
  41.  
  42. public char getContent() { //The content of current cell.
  43. return currentCell.content;
  44. }
  45.  
  46. public void setContent(char ch) { //ch The character to be set into the current cell.
  47. currentCell.content = ch;
  48. }
  49.  
  50. public void moveLeft() { //Moves the current cell one position to the left along the tape.
  51. if (currentCell.prev == null) {
  52. Cell newCell = new Cell();
  53. newCell.content = ' ';
  54. newCell.prev = null;
  55. newCell.next = currentCell;
  56. currentCell.prev = newCell;
  57. }
  58. currentCell = currentCell.prev;
  59. }
  60.  
  61. public void moveRight() { //Moves the current cell one position to the right along the tape.
  62. if (currentCell.next == null) {
  63. Cell newCell = new Cell();
  64. newCell.content = ' ';
  65. newCell.next = null;
  66. newCell.prev = currentCell;
  67. currentCell.next = newCell;
  68. }
  69. currentCell = currentCell.next;
  70. }
  71.  
  72. public String getTapeContents() { //Returns a String consisting of the chars from all the cells on the tape.
  73. Cell pointer = currentCell;
  74. while (pointer.prev != null)
  75. pointer = pointer.prev;
  76. String strContent = "";
  77. while (pointer != null) {
  78. strContent += pointer.content;
  79. pointer = pointer.next;
  80. }
  81. strContent = strContent.trim(); //Returns a copy of the string, with leading and trailing whitespace omitted.
  82. return strContent;
  83. }
  84. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Derik
compilation info
Main.java:1: error: class, interface, or enum expected
Tape.java
^
1 error
stdout
Standard output is empty