0% found this document useful (0 votes)
23 views

CS 1103-01 Assignment Unit 3

This code defines a Tape class that represents the tape of a Turing machine. The Tape class contains methods to get and set the content of the current cell, move the current cell pointer left and right, and retrieve the full contents of the tape. The constructor initializes a new tape with a single blank cell. Other methods allow accessing the current cell, changing its content, navigating the tape, and retrieving the entire tape contents as a string.

Uploaded by

KILLIAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

CS 1103-01 Assignment Unit 3

This code defines a Tape class that represents the tape of a Turing machine. The Tape class contains methods to get and set the content of the current cell, move the current cell pointer left and right, and retrieve the full contents of the tape. The constructor initializes a new tape with a single blank cell. Other methods allow accessing the current cell, changing its content, navigating the tape, and retrieving the entire tape contents as a string.

Uploaded by

KILLIAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Sure, here is the code for the Tape class:

Java

public class Tape {

private Cell currentCell;

public Tape() {

// Create a new cell with a blank space and set the current cell pointer to it.

Cell cell = new Cell();

cell.content = ' ';

currentCell = cell;

public Cell getCurrentCell() {

return currentCell;

public char getContent() {

return currentCell.content;

}
public void setContent(char ch) {

currentCell.content = ch;

public void moveLeft() {

if (currentCell.prev == null) {

// Create a new cell to the left of the current cell.

Cell newCell = new Cell();

newCell.content = ' ';

newCell.next = currentCell;

currentCell.prev = newCell;

currentCell = newCell;

} else {

currentCell = currentCell.prev;

public void moveRight() {

if (currentCell.next == null) {
// Create a new cell to the right of the current cell.

Cell newCell = new Cell();

newCell.content = ' ';

newCell.prev = currentCell;

currentCell.next = newCell;

} else {

currentCell = currentCell.next;

public String getTapeContents() {

// Create a temporary pointer to move along the tape without changing the current cell

pointer.

Cell tempCell = currentCell;

StringBuilder sb = new StringBuilder();

// Skip leading blank characters.

while (tempCell != null && tempCell.content == ' ') {

tempCell = tempCell.next;

}
// Append the contents of each cell to the string builder.

while (tempCell != null) {

sb.append(tempCell.content);

tempCell = tempCell.next;

// Return the string without trailing blank characters.

return sb.toString().trim();

This code implements all of the required methods for the Tape class. The constructor creates a

new tape with a single cell containing a blank space. The other methods allow the user to

access the current cell, change its content, move the current cell left or right, and get the

contents of the entire tape.

You might also like