/* CSI2114 Lab 4 - Sentence.java * * A driver program to test the implementation of the Sequence. * * Usage: java Sentence * * by Jeff Souza * */ class Sentence { public static void main (String args[]) { // Construct a sample sentence. Sequence sentence = new NodeSequence(); // Start out with "This sentence." sentence.insertFirst("This "); sentence.insertLast("sentence."); printSentence( sentence ); // Insert to make it "This sample dumb sentence." sentence.insertAfter( sentence.first(), "sample "); sentence.insertBefore( sentence.last(), "dumb "); printSentence( sentence ); // Swap words to get "This dumb sample sentence." Position second = sentence.atRank( 1 ); Position secondLast = sentence.before( sentence.last()); sentence.swapElements( second, secondLast ); printSentence( sentence ); // Replace "dumb" to get "This is a sample sentence." sentence.replaceElement( second, "is "); Position middle = sentence.insertAfter( second, "a "); printSentence( sentence ); // Remove "sample", leaving "This is a sentence." sentence.remove( secondLast ); printSentence( sentence ); // Note that "This is a self-referential sentence." sentence.insertAtRank( 3, "self-referential "); printSentence( sentence ); // Now deconstruct the sentence. sentence.removeAtRank( sentence.rankOf( middle )); sentence.remove( sentence.last()); sentence.remove( sentence.first()); sentence.remove( sentence.last()); sentence.remove( sentence.first()); if ( sentence.isEmpty()) System.out.println("\nThe sentence is now empty."); System.out.println("\nLet's end by throwing an exception:"); int nonexistent = sentence.rankOf( second ); } public static void printSentence( Sequence sentence ) { // Display the current sentence. Position pos = sentence.first(); for ( int i = 1; i < sentence.size(); i++ ) { System.out.print( pos.element()); pos = sentence.after( pos ); } System.out.println( pos.element()); } }