Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*******USING ECLIPSE Please just don\'t repost from a previous post. Your task i

ID: 3752294 • Letter: #

Question

*******USING ECLIPSE Please just don't repost from a previous post.

Your task is to write a class named Tape to represent Turing machine tapes. The class should have an instance variable of type Cell that points to the current cell. To be compatible with the classes that will use the Tape class, your class must include the following methods:

public Cell getCurrentCell() -- returns the pointer that points to the current cell.

public char getContent() -- returns the char from the current cell.

public void setContent(char ch) -- changes the char in the current cell to the specified value.

public void moveLeft() -- 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 moveRight() -- 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 String getTapeContents() -- 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.)

It is also useful to have a constructor that creates a tape that initially consists of a single cell. The cell should contain a blank space, and the current cell pointer should point to it. (The alternative -- letting the current cell pointer be null to represent a completely blank tape -- makes all the methods in the class more difficult to implement.)

To test your Tape class, you can should run the programs that are defined by the files TestTape.java, TestTapeGUI.java, and TestTuringMachine.java. The first two programs just do things with a tape, to test whether it is functioning properly. TestTuringMachine actually creates and runs several Turing machines, using your Tape class to represent the machines' tapes.

Explanation / Answer

import java.applet.Applet;

import java.awt.*;

import java.util.*;

public class Ttape1 extends Applet

{

static final int YTOP = 120; /* y-size of margin above tape box */

static final int YBOT = 310;

static final int YHEADER = 30; /* y-size of horz strip */

static final int NCELLX = 20; /* number of cells across */

static final int CELLSIZE = 30; /* size of each square cell */

static final int MARGIN = 8; /* margin from number to cell top, right */

FontMetrics fm; /* to get font info */

int fontAscent; /* character height */

int cellPos; /* y-position of day strings */

int xSize, ySize; /* size of Tape body (cell table) */

int numRows = 4; /* number of rows in cell table (4, 5, 6) */

int xNum, yNum; /* number position at top right of cells */

int numCells = 80; /* number of cells on tape */

String cellStr; /* Cell number as a string */

int marg; /* margin of string baseline from cell table */

String caption; /* string at top center */

int[] intCells = new int[82];

String[] strProg1 = new String[10];

String[] strProg2 = new String[10];

String[] strProg3 = new String[10];

String[] strProg = new String[10];

String[] Register = new String[6];

int instruction = 0;

int instructionLimit = 300;

int GoToggle = 1;

int StateNow = 1;

int curFrame = 30;

String strMsg;

String strHistory = "";

String strStateNext = "";

// Data entry controls at top.

Label l1 = new Label("Select Program >>");

Choice ProgNow = new Choice();

Button Go = new Button("Go");

Button Stop = new Button("Stop");

Button Auto = new Button("Auto");

TextArea Comment = new TextArea("***",5,80);

// Font for controls at top.

Font smallArialFont = new Font("Arial", Font.PLAIN, 15);

// Font for for caption above tape.

Font largeArialFont = new Font("Arial", Font.BOLD, 30);

Font CurrentArialFont = new Font("Arial", Font.BOLD, 15);

Font msgArialFont = new Font("Arial", Font.PLAIN, 12);

public void init()

/*

USE: initialize controls.

NOTE: Called automatically when applet starts.

*/

{

// setBackground(Color.red);

// setForeground(Color.blue);

strProg1[0] = "Program 1: Start At Instruction 1";

strProg1[1] = "1 R U 1 E 2";

strProg1[2] = "2 L M 3 U 2";

strProg1[3] = "3 L M 4 U 3";

strProg1[4] = "4 R U 1 U 4";

strProg1[5] = "END";

//for (int i = 0; i < 6; i++) strProg[i] = strProg1[i];

strProg2[0] = "Program 2: Start At Instruction 4";

strProg2[1] = "1 R U 4 U 1";

strProg2[2] = "2 L M 1 U 2";

strProg2[3] = "3 L M 2 U 3";

strProg2[4] = "4 R U 4 E 3";

strProg2[5] = "END";

strProg3[0] = "Program 3: Start At Instruction 1";

strProg3[1] = "1 R U 8 U 2";

strProg3[2] = "2 R U 3 U 2";

strProg3[3] = "3 L U 5 E 4";

strProg3[4] = "4 L U 5 U 4";

strProg3[5] = "5 L M 6 U 5";

strProg3[6] = "6 L M 7 U 6";

strProg3[7] = "7 R U 1 U 7";

strProg3[8] = "8 H U 8 U 8";

strProg3[9] = "END";

l1.setFont(CurrentArialFont);

add(l1);

ProgNow.addItem("****");

ProgNow.addItem("Program 1");

ProgNow.addItem("Program 2");

ProgNow.addItem("Program 3");

add(ProgNow);

Go.setFont(CurrentArialFont);

add(Go);

Stop.setFont(CurrentArialFont);

add(Stop);

Auto.setFont(CurrentArialFont);

add(Auto);

strMsg = "To run the Turing machine,"

+ "select a program and press the 'Go' Button."

+ " A step by step explanation of the operation"

+ " of the machine will appear in this area."

+ " When the program ends or the Stop button is pushed,"

+ "the entire explanation will appear here"

+ " and a scrollbar will appear on the right of this area."

+ " You may use the scrollbar to scroll through "

+ "the explanation.";

Comment.setText(strMsg);

Auto.disable();

Go.disable();

Stop.disable();

add(Comment);

} // init

public void paint(Graphics g)

/*

USE: Draw Ttape.

NOTE: Called automatically whenever surface needs to be redrawn;

also when user clicks 'Go' button, triggering repaint.

*/

{

// Get font info for number string positioning (default small font).

fm = g.getFontMetrics();

fontAscent = fm.getAscent();

cellPos = YTOP + (YHEADER + fontAscent) / 2;

// Get x-size of Tape body (cell table).

xSize = NCELLX * CELLSIZE;

// Vertical lines of cell table.

ySize = (numRows) * CELLSIZE;

for (int i = 0; i <= xSize; i += CELLSIZE)

g.drawLine(i, YTOP + YHEADER, i, YTOP + YHEADER + ySize);

// Horizontal lines of cell table.

for (int i = 0, j = YTOP + YHEADER; i <= numRows; i++, j += CELLSIZE)

g.drawLine(0, j, xSize, j);

// Init number positions (upper right of cell).

xNum = CELLSIZE - MARGIN;

yNum = YTOP + YHEADER + MARGIN + fontAscent;

for (int cell = 1; cell <= numCells; cell++)

{

cellStr = String.valueOf(intCells[cell]);

if (cell == curFrame) g.setFont(CurrentArialFont);

g.drawString(cellStr, xNum - fm.stringWidth(cellStr), yNum);

if (cell == curFrame) g.setFont(smallArialFont);

xNum += CELLSIZE;

// If xNum to right of tape grid, 'new line'.

if (xNum > xSize)

{

xNum = CELLSIZE - MARGIN;

yNum += CELLSIZE;

} // if

} // for

// Set large font for caption.

g.setFont(CurrentArialFont);

// Get font info for string positioning (large font now current).

fm = g.getFontMetrics();

// Set margin for y-positioning of caption.

marg = 2 * fm.getDescent();

// Set caption to string and center at top.

caption = "Turing Machine";

g.drawString(caption, (xSize-fm.stringWidth(caption))/2, 135 );

g.setFont(CurrentArialFont);

Comment.setText(strMsg);

} // paint

public boolean action(Event e, Object o)

{

if ("Go".equals((String)o)) GoStep();

else if ("Stop".equals((String)o)) StopProg();

else if ("Auto".equals((String)o)) AutoStep();

else if (e.target == ProgNow)

{

instruction = 0;

GoToggle = 1;

strHistory = "";

strMsg = "";

if (ProgNow.getSelectedItem().equals("Program 1"))

{

for (int i = 0;i < 82; i++) intCells[i] = 0;

intCells[curFrame - 3] = 1;

intCells[curFrame - 4] = 1;

intCells[curFrame - 5] = 1;

Go.enable();

Auto.enable();

Stop.enable();

for (int i = 0; i < 10; i ++)

{

strProg[i] = strProg1[i];

if (strProg[i].length() > 0)

strMsg = strMsg + strProg[i] + " ";

}

}

else if(ProgNow.getSelectedItem().equals("Program 2"))

{

for (int i = 0;i < 82; i++) intCells[i] = 0;

intCells[curFrame - 5] = 1;

intCells[curFrame - 6] = 1;

intCells[curFrame - 7] = 1;

Go.enable();

Auto.enable();

Stop.enable();

for (int i = 0; 1 <10; i++)

{

strProg[i] = strProg2[i];

if (strProg[i].length() > 0)

strMsg = strMsg + strProg[i] + " ";

}

}

else if(ProgNow.getSelectedItem().equals("Program 3"))

{

for (int i = 0;i < 82; i++) intCells[i] = 0;

intCells[curFrame] = 1;

intCells[curFrame - 1] = 1;

intCells[curFrame - 2] = 1;

Go.enable();

Auto.enable();

Stop.enable();

for (int i = 0; 1 <10; i++)

{

strProg[i] = strProg3[i];

if (strProg[i].length() > 0)

strMsg = strMsg + strProg[i] + " ";

}

}

}

repaint();

return false;

} // action

void GoStep()

{

if (instruction > instructionLimit) return;

if (instruction == 0)

{

StartProg();

instruction++;

}

else

{

switch(GoToggle)

{

case 1:

loadRegister();

strHistory = strHistory + " " + strMsg;

strMsg = "Step " + instruction + ": State: "

+ strProg[StateNow];

TapeMove();

GoToggle = 2;

instruction++;

if (instruction > instructionLimit) StopProg();

break;

case 2:

TapeMark();

GoToggle = 1;

break;

}

}

return;

}

void loadRegister()

{

String strCur = new String();

String strHold = new String();

int i = 0;

int j = 0;

strCur = strProg[StateNow];

j = strCur.indexOf(' ');

while (j > 0)

{

strHold = strCur.substring(0,j);

strCur = strCur.substring(j + 1);

Register[i] = strHold;

i++;

j = strCur.indexOf(' ');

}

Register[i] = strCur;

return;

}

void TapeMove()

{

char charHolder[] = Register[1].toCharArray();

switch(charHolder[0])

{

case 'L':

moveLeft(1);

break;

case 'R':

moveRight(1);

break;

case 'H':

moveHalt();

break;

}

return;

}

void StartProg()

{

StateNow = 0;

loadRegister();

strHistory = strHistory + strMsg + " ";

strMsg = strProg[0];

StateNow = StrToInt(Register[5]);

repaint();

return;

}

int StrToInt(String strParm)

{

int i;

Integer q = new Integer(strParm);

i = q.intValue();

return i;

}

void StopProg()

{

Go.disable();

Stop.disable();

Auto.disable();

instruction = instructionLimit + 1;

strMsg = strHistory + " Program Halted!";

repaint();

return;

}

void TapeMark()

{

int i;

String strMark = "";

String strState = "";

if (intCells[curFrame] == 0) //Unmarked

{

strMsg = strMsg +

"the current frame in unmarked (0);"

+ " the branching instruction is " + Register[2] + " "

+ Register[3] + ": ";

strMark = Register[2];

strState = Register[3];

strStateNext = strState;

}

else if (intCells[curFrame] == 1) //Marked

{

strMsg = strMsg +

"the current frame in marked (1);"

+ " the branching instruction is " + Register[4] + " "

+ Register[5] + ": " ;

strMark = Register[4];

strState = Register[5];

strStateNext = strState;

}

else

{

strMsg = "*** ILLEGAL TAPE MARK--PROGRAM HALTED ***" +

intCells[curFrame] + " " + strMsg;

StopProg();

return;

}

if (strMark.equals("E"))

{

intCells[curFrame] = 0;

strMsg = strMsg +

" Erase the current frame by setting it to '0' and assume state "

+ strStateNext + ". " ;

}

else if (strMark.equals("M"))

{

intCells[curFrame] = 1;

strMsg = strMsg +

" Mark the current frame by setting it to '1' and assume state "

+ strStateNext + ". ";

}

else if (strMark.equals("U"))

{

strMsg = strMsg +

" Leave the current frame unchanged and assume state "

+ strStateNext + ". ";

}

else

{

strMsg = "*** ILLEGAL TAPE MARK INSTRUCTION--PROGRAM HALTED ***"

+ ": " + strMark + " " + strMsg;

StopProg();

}

StateNow = StrToInt(strState);

return;

}

void AutoStep()

{

for(int dt = 0; dt < 20; dt++) GoStep();

return;

}

void moveLeft(int count)

{

int j, i;

for (i=1; i <= count; i++)

{

for (j = 1; j <= numCells; j++)

{

intCells[j - 1] = intCells[j];

}

strMsg = strMsg + " Move the tape LEFT; ";

repaint();

}

return;

}

void moveRight(int count)

{

int j, i;

for (i=1; i <= count; i++)

{

for (j = numCells; j >= 1; j--)

{

intCells[j+1] = intCells[j];

}

strMsg = strMsg + " Move the tape RIGHT; ";

repaint();

}

return;

}

void moveHalt()

{

strMsg = strMsg + " Halt Tape; ";

if (instruction < instructionLimit - 1)

instruction = instructionLimit - 1;

repaint();

return;

}

  

  

} // class Ttape1