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

CS 1103 Programming Assignment Unit 3

This document contains code examples for a Tape.java class that models a Turing machine tape. The Tape class uses a linked list of Cell objects to represent the tape. It provides methods to get/set the current cell content, move the current cell pointer left/right by adding new cells if needed, and get the full tape contents as a string.

Uploaded by

Patrick Maina
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

CS 1103 Programming Assignment Unit 3

This document contains code examples for a Tape.java class that models a Turing machine tape. The Tape class uses a linked list of Cell objects to represent the tape. It provides methods to get/set the current cell content, move the current cell pointer left/right by adding new cells if needed, and get the full tape contents as a string.

Uploaded by

Patrick Maina
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CS 1103 programming assignment unit 3

Codes examles

Tape.java

public class Tape {

// Instance variables

private Cell currentCell;

/**

* Default constructor

*/

public Tape() {

// Create a cell with blank content

Cell blankCell = new Cell();

blankCell.content = ' ';

blankCell.next = null;

blankCell.prev = null;

// Set blankCell as current cell

this.currentCell = blankCell;

/**

* This method returns the pointer that points to the current cell.

*/

public Cell getCurrentCell() {

return this.currentCell;

/**
* This method returns the char from the current cell.

*/

public char getContent() {

return this.currentCell.content;

/**

* This method changes the char in the current cell to the specified value.

*/

public void setContent(char ch) {

this.currentCell.content = 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 (this.currentCell.prev == null) {

// Create a cell with blank content


Cell blankCell = new Cell();

blankCell.content = ' ';

blankCell.prev = null;

// Set next of blankCell as currentCell

blankCell.next = this.currentCell;

// Set prev of currentCell as blankCell

this.currentCell.prev = blankCell;

// Move currentCell to the left

this.currentCell = this.currentCell.prev;

/**

* 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 (this.currentCell.next == null) {

// Create a cell with blank content

Cell blankCell = new Cell();

blankCell.content = ' ';

blankCell.next = null;
// Set prev of blankCell as currentCell

blankCell.prev = this.currentCell;

// Set next of currentCell as blankCell

this.currentCell.next = blankCell;

// Move currentCell to the right

this.currentCell = this.currentCell.next;

/**

* 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 ((this.currentCell.next == null) && (this.currentCell.prev == null))

return "";

else {

StringBuffer res = new StringBuffer();

// Add content of currentCell to content

res.append(currentCell.content);
// Get reference of the cell left of the currentCell

Cell cell = this.currentCell.prev;

// Get all the chars to the left of the currentCell

while (cell != null) {

// Append content of cell to content

res.insert(0, cell.content);

// Move cell to the left

cell = cell.prev;

// Get reference of the cell right of the currentCell

cell = this.currentCell.next;

// Get all the chars to the right of the currentCell

while (cell != null) {

// Append content of cell to content

res.append(cell.content);

// Move cell to the right

cell = cell.next;

// Return content

return res.toString().trim();

You might also like