2008 Encoding

<< 2008TripFlights | APQuestionsTrailIndex | 2008 OpossumCritter >>

Question from College Board

StringCoder

import java.util.ArrayList;
/** 
 * @author Chris Thiel,OFMCap
 * @version 8 May 2008
 */
public class StringCoder
{

    private String masterString;

    /**
     * Constructor for objects of class StringCoder
     */
    public StringCoder()
    {
        masterString="sixtyzipperswerequicklypickedfromthewovenjutebag";
    }
    public StringCoder(String master)
    {
        masterString=master;
    }

    public String decodeString(ArrayList<StringPart> parts)
    {
        // part a

    }
    private StringPart findPart(String str)
    {
        int start=0;
        int length=0;
        for (int len=1; len<=str.length(); len++)
          {
              int found=masterString.indexOf(str.substring(0,len));
              if (found!=-1)
              {
                  start=found;
                  length=len;
              }
          }
        return new StringPart(start, length);
    }

    public ArrayList<StringPart> encodeString(String word)
    {
        //part b

    }
}

StringPart


/**
 * Write a description of class StringPart here.
 * 
 * @author Chris Thiel, OFMCap
 * @version 8 May 2008
 */
public class StringPart
{

    private int s;
    private int len;
	/**
	 * Constructor for objects of class StringPart
	 */
	public StringPart(int start, int length)
	{
		// initialise instance variables
		s=start;
		len=length;
	}
    public StringPart()
	{
		// initialise instance variables
		s=0;
		len=0;
	}

	public int getStart()
	{
		return s;
	}

	public int getLength()
	{
	    return len;
	}
	public String toString()
	{
	    return "("+getStart()+", "+getLength()+")";
	}
}

StringCoderDriver

import java.util.ArrayList;
/**
 * 
 * @author Chris Thiel, oFMCap
 * @version 8 May 2008
 */
public class StringCoderDriver
{
	public static void main(String[] args)
	{
	   ArrayList<StringPart> overeager=new ArrayList<StringPart>();
	   overeager.add(new StringPart(37,3) );
	   overeager.add(new StringPart(14,2) );
	   overeager.add(new StringPart(46,2) );
	   overeager.add(new StringPart(9,2) );
	   System.out.println("overeager is encoded as "+overeager);
	   StringCoder key=new StringCoder("sixtyzipperswerequicklypickedfromthewovenjutebag");
	   System.out.println("Decoding we get "+key.decodeString(overeager));
	   System.out.println("Using part b overeager is encoded as "+key.encodeString("overeager"));
	}
}