/** * class Pirate is a English to Pirate translator * * @author Chris Thiel * @version 15 September 2007 * A "nifty assignment" from Dave Reed at Creighton University * presented at SIGCSE 2004 * * Talk like a Pirate Day is September 19th * http://www.talklikeapirate.com/ * * A fun way to introduce 2 dimensional arrays, Scanner class, * and String comparison * * Improvements that can be made: * more words * less sensitive to capitalization ( perhaps lowerCase() ) * addition of random (50% chance) of "Arrrr"s in the translations * (care should be taken to avoid consecutive "Arrr"s) * * Bugs to be repaired: * "hi" is translated as "yo-ho-ho" but * "this" is translated "tyo-ho-hos" */ import java.util.Scanner; public class Pirate { // instance variables static final int ENGLISH=0; static final int PIRATE=1; static String [] [] dictionary = {{"hello", "ahoy"}, {"hi", "yo-ho-ho"}, {"pardon me", "avast"}, {"excuse me", "arrr"}, {"my", "me"}, {"friend", "me bucko"}, {"sir", "matey"}, {"madam", "proud beauty"}, {"miss", "comely wench"}, {"stranger", "scurvy dog"}, {"officer", "foul blaggart"}, {"where", "whar"}, {"is", "be"}, {"the", "th'"}, {"you", "ye"}, {"tell", "be tellin'"}, {"know", "be knowin'"}, {"how far", "how many leagues"}, {"old", "barnacle-covered"}, {"attractive", "comely"}, {"happy", "grog-filled"}, {"nearby", "broadside"}, {"restroom", "head"}, {"restaurant", "galley"}, {"hotel", "fleabag inn"}, {"pub", "Skull & Scuppers"}, {"bank", "buried treasure"} }; static String in=""; static String translation=""; public static void main(String[]args) { Scanner keyboard=new Scanner(System.in); System.out.print("Pirate translator\n(Type \"quit\" to end)\n"); while (!in.equals("quit")) { System.out.print("Type English Here: "); in = keyboard.nextLine(); translation=in; for (int i = 0; i< dictionary.length; i++) translation=translation.replaceAll( dictionary[i][ENGLISH], dictionary[i][PIRATE]); System.out.println("Pirate Translation: "+translation); } } } |