This lab is another relatively short exercise in which you will code part of an existing project. In this case, you will be working with a doubly-linked list. To make this topic more interesting, the list that you work on will be part of a Turing Machine. A Turing Machine is a kind of very simple, abstract computing device that was introduced in the 1930's by Alan Turing as a way of studying computation and its limitations.

The classes that you need for the lab are in a package named turing. You will define a new class named Tape in this package. You can find the Java source in the code directory. You should copy this directory into an Eclipse project. There will be errors, since the existing classes depend on the Tape class, which you have not yet written.

Respuesta :

Answer:

Below is the code for Tape.java. Since it is in the package turing, you have to define it in the beginning.

Tape.java:

package turing;

public class Tape {

private Cell currentCell; // Current cell pointer

public Tape() { //Constructor to create a blank tape with a single cell, which contains a blank space.

Cell newCell = new Cell();

newCell.content = ' ';

newCell.prev = null;

newCell.next = null;

currentCell = newCell;

}

public Cell getCurrentCell() { //The pointer to current cell.

return currentCell;

}

public char getContent() { //The content of current cell.

return currentCell.content;

}

public void setContent(char ch) { //ch The character to be set into the current cell.

currentCell.content = ch;

}

public void moveLeft() { //Moves the current cell one position to the left along the tape.

if (currentCell.prev == null) {

Cell newCell = new Cell();

newCell.content = ' ';

newCell.prev = null;

newCell.next = currentCell;

currentCell.prev = newCell;

}

currentCell = currentCell.prev;

}

public void moveRight() { //Moves the current cell one position to the right along the tape.

if (currentCell.next == null) {

Cell newCell = new Cell();

newCell.content = ' ';

newCell.next = null;

newCell.prev = currentCell;

currentCell.next = newCell;

}

currentCell = currentCell.next;

}

public String getTapeContents() { //Returns a String consisting of the chars from all the cells on the tape.

Cell pointer = currentCell;

while (pointer.prev != null)

pointer = pointer.prev;

String strContent = "";

while (pointer != null) {

strContent += pointer.content;

pointer = pointer.next;

}

strContent = strContent.trim(); //Returns a copy of the string, with leading and trailing whitespace omitted.

return strContent;

}

}

Explanation:

ACCESS MORE