CS 1103 Programming Assignment Unit 3
CS 1103 Programming Assignment Unit 3
Codes examles
Tape.java
// Instance variables
/**
* Default constructor
*/
public Tape() {
blankCell.next = null;
blankCell.prev = null;
this.currentCell = blankCell;
/**
* This method returns the pointer that points to the current cell.
*/
return this.currentCell;
/**
* This method returns the char from the current cell.
*/
return this.currentCell.content;
/**
* This method changes the char in the current cell to the specified value.
*/
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.
* your linked list must must be prepared to expand on demand, when the
*/
if (this.currentCell.prev == null) {
blankCell.prev = null;
blankCell.next = this.currentCell;
this.currentCell.prev = blankCell;
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.
*/
if (this.currentCell.next == null) {
blankCell.next = null;
// Set prev of blankCell as currentCell
blankCell.prev = this.currentCell;
this.currentCell.next = blankCell;
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
* move along the tape and get the full contents. (This method is the
*/
return "";
else {
res.append(currentCell.content);
// Get reference of the cell left of the currentCell
res.insert(0, cell.content);
cell = cell.prev;
cell = this.currentCell.next;
res.append(cell.content);
cell = cell.next;
// Return content
return res.toString().trim();