Assignment 2 PDF

Title Assignment 2
Author Ajay Chauhan
Course Programming 2
Institution University of the People
Pages 7
File Size 73.5 KB
File Type PDF
Total Downloads 71
Total Views 142

Summary

Assignment...


Description

University of the People Computer Science CS1103 Java Assignment 2

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 be prepared to expand ondemand 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.)

package turing;

// A test program for the TuringMachine class. It creates three machines // and runs them. The output from the program indictes the expected behavior.

public class TestTuringMachine {

public static void main(String[] args) {

TuringMachine writeMachine = new TuringMachine();

writeMachine.addRules( new Rule[] { // writes Hello on the tape then halts new Rule(0,' ',1,'H',false), new Rule(1,' ',2,'e',false), new Rule(2,' ',3,'l',false), new Rule(3,' ',4,'l',false), new Rule(4,' ',-1,'o',false) });

System.out.println("Running machine #1. Output should be: Hello");

String writeMachineOutput = writeMachine.run(new Tape()); System.out.println( "Actual output is:

"+

writeMachineOutput );

TuringMachine badMachine = new TuringMachine(); badMachine.addRules( new Rule[] { // writes ERROR on the tape then fails new Rule(0,' ',1,'R',true), new Rule(1,' ',2,'O',true), new Rule(2,' ',3,'R',true), new Rule(3,' ',4,'R',true), new Rule(4,' ',5,'E',true) // no rule for state 5! });

System.out.println("\nRunning machine #2. Should throw an IllegalStateExcpetion."); try { badMachine.run( new Tape() ); System.out.println("No Error was thrown.");

} catch (IllegalStateException e) { System.out.println("Caught Illegal Argument Exception, with error message:"); System.out.println(e.getMessage()); }

String input = "aababbbababbabbaba"; // a string of a's and b's for input to the copy machine Tape tape = new Tape(); for (int i = 0; i < input.length(); i++) { tape.setContent(input.charAt(i)); tape.moveRight(); } tape.moveLeft(); // now, input is written on the tape, with the machine on the rightmost character

TuringMachine copyMachine = new TuringMachine(); // copies a string of a's and b's;

// machine must start on leftmost char in the string copyMachine.addRules(new Rule[] { new Rule(0,'a',1,'x',true), // rules for copying an a new Rule(1,'a',1,'a',true), new Rule(1,'b',1,'b',true), new Rule(1,' ',2,' ',true), new Rule(2,'a',2,'a',true), new Rule(2,'b',2,'b',true), new Rule(2,' ',3,'a',false), new Rule(3,'a',3,'a',false), new Rule(3,'b',3,'b',false), new Rule(3,' ',3,' ',false), new Rule(3,'x',0,'x',true), new Rule(3,'y',0,'y',true),

new Rule(0,'b',4,'y',true), // rules for copying a b new Rule(4,'a',4,'a',true), new Rule(4,'b',4,'b',true),

new Rule(4,' ',5,' ',true), new Rule(5,'a',5,'a',true), new Rule(5,'b',5,'b',true), new Rule(5,' ',7,'b',false), new Rule(7,'a',7,'a',false), new Rule(7,'b',7,'b',false), new Rule(7,' ',7,' ',false), new Rule(7,'x',0,'x',true), new Rule(7,'y',0,'y',true),

new Rule(0,' ',8,' ',false), // rules that change x and y back to a and b, then halt new Rule(8,'x',8,'a',false), new Rule(8,'y',8,'b',false), new Rule(8,' ',-1,' ',true) });

System.out.println("\nRunning machine #3. Output should be: " + input + " " + input);

String copyMachineOutput = copyMachine.run(tape); System.out.println("Actual output is: copyMachineOutput);

}

}

"+...


Similar Free PDFs