CS 1103 programming assignment unit 3
Codes examles
[Link]
public class Tape {
// Instance variables
private Cell currentCell;
/**
* Default constructor
*/
public Tape() {
// Create a cell with blank content
Cell blankCell = new Cell();
[Link] = ' ';
[Link] = null;
[Link] = null;
// Set blankCell as current cell
[Link] = blankCell;
/**
* This method returns the pointer that points to the current cell.
*/
public Cell getCurrentCell() {
return [Link];
/**
* This method returns the char from the current cell.
*/
public char getContent() {
return [Link];
/**
* This method changes the char in the current cell to the specified value.
*/
public void setContent(char ch) {
[Link] = ch;
/**
* This method 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.)
*/
public void moveLeft() {
// Check if currentCell is the leftmost cell
if ([Link] == null) {
// Create a cell with blank content
Cell blankCell = new Cell();
[Link] = ' ';
[Link] = null;
// Set next of blankCell as currentCell
[Link] = [Link];
// Set prev of currentCell as blankCell
[Link] = blankCell;
// Move currentCell to the left
[Link] = [Link];
/**
* This method 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.
*/
public void moveRight() {
// Check if currentCell is the rightmost cell
if ([Link] == null) {
// Create a cell with blank content
Cell blankCell = new Cell();
[Link] = ' ';
[Link] = null;
// Set prev of blankCell as currentCell
[Link] = [Link];
// Set next of currentCell as blankCell
[Link] = blankCell;
// Move currentCell to the right
[Link] = [Link];
/**
* This method 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.)
*/
public String getTapeContents() {
// Check if there are no cells
if (([Link] == null) && ([Link] == null))
return "";
else {
StringBuffer res = new StringBuffer();
// Add content of currentCell to content
[Link]([Link]);
// Get reference of the cell left of the currentCell
Cell cell = [Link];
// Get all the chars to the left of the currentCell
while (cell != null) {
// Append content of cell to content
[Link](0, [Link]);
// Move cell to the left
cell = [Link];
// Get reference of the cell right of the currentCell
cell = [Link];
// Get all the chars to the right of the currentCell
while (cell != null) {
// Append content of cell to content
[Link]([Link]);
// Move cell to the right
cell = [Link];
// Return content
return [Link]().trim();