Introduction
This blog implements a Turing machine tape using a doubly-linked list, and an object of type Cell
can represent each cell in the tape. Besides, there is a pointer to the current cell in this tape, and one can move this pointer by calling the moveRight()
or moveLeft()
method. In this blog, I’ll discuss the circumstances under which a given moveRight()
method can correctly perform its duties and the cases under which it cannot.
1. Questions
Does the following moveRight()
method perform its duties correctly?Specifically, there are two code snippets (UoPeople, n.d.):
1 | /** |
1 | /** |
Suppose we have a ten cells tape with the contents “HelloWorld”. If the current cell points to the head of the tape, can the moveRight()
method above perform its duties correctly? In other words, after executing the moveRight()
method, will the current cell point to “e” instead of “H”? The images below show what we expect from the moveRight()
method:
Before executing the moveRight()
:
After executing the moveRight()
:
2. Answers
The moveRight()
method can not perform its duties correctly. The reasons are at follows: The current cell is at the head of the list, and the list has ten cells, so currentCell.next
is not null. As a result, the statement currentCell = currentCell.next;
in the if-clause will not be executed, the current cell will not move to the next cell.
To further confirm the above explanation, I tested this moveRight()
method, and the results show that it did not move the current cell to the right. The detailed test procedure and outputs are shown below.
3. Proving process
3.1. Codes and Outputs
Codes:
Tape.java
1 | package turing; |
Cell.java
1 | package turing; |
TestTape.java
1 | package turing; |
Outputs:
1 | Create a list that has ten cells - "HelloWorld" |
3.2. Explanation
In Tape.java, I created a list with ten cells - “HelloWorld”. Then, I let the current cell pointer point to the head - “H”. Next, in TestTape.java, I called the moveRightInQuiz()
method in the quiz, and the current cell pointer should point to the “e”. However, the results show that the current cell pointer still points to the “H”, which means the moveRightInQuiz()
method does not perform its duties correctly.
Why doesn’t this moveRightInQuiz()
perform its duties correctly? The reasons are at follows: The current cell is at the head of the list, and the list has ten cells, so currentCell.next
is not null. As a result, the statement currentCell = currentCell.next;
in the if-clause will not be executed, the current cell will not move to the next cell.
If we want the moveRight()
method to perform its duties correctly, we can move the statement currentCell = currentCell.next;
outside the if statement. In this case, the statement currentCell = currentCell.next;
will always be executed regardless of whether the currentCell.next
is null
or not. As a result, the moveRight()
method will let the currentCell’s pointer point to its next cell, thus performing its duties correctly. The correct moveRight() method is as follows:
1 | /** |
4. In Conclusion
1.If the tape has only one head cell, and the current cell points to it, then the moveRight()
method in the quiz can perform its duties correctly.
2.However, if the tape has more than one cell, and the current cell points to this head cell, then the moveRight()
method in the quiz cannot perform its duties because, in this case, the statement currentCell = currentCell.next;
will not be executed.
3.If one wants the moveRight()
method to perform its duties correctly in any case, he/she needs to move the statement currentCell = currentCell.next;
outside the if statement.
Word count: 2342
References
UoPeople. (2020). Lab 5: Tape for a Turing Machine (Doubly-linked List).
https://my.uopeople.edu/mod/page/view.php?id=279538
UoPeople. (2022). Code Unit 3.
https://my.uopeople.edu/mod/folder/view.php?id=279540