Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name: Date:
- import java.util.*;
- public class StringCoder_shell
- {
- public static void main(String[] args)
- {
- StringCoder sc = new StringCoder("sixtyzipperswerequicklypickedfromthewovenjutebag");
- StringPart[] sp = sc.encodeString("overeager");
- for(int i=0; i<sp.length; i++)
- System.out.print(sp[i]+", ");
- System.out.println();
- String s = sc.decodeString(sp);
- System.out.println(s);
- StringPart[] sp2 = sc.encodeString("kippers");
- for(int i=0; i<sp2.length; i++)
- System.out.print(sp2[i]+", ");
- System.out.println();
- String s2 = sc.decodeString(sp2);
- System.out.println(s2);
- StringPart[] sp3 = sc.encodeString("colonials");
- for(int i=0; i<sp3.length; i++)
- System.out.print(sp3[i]+", ");
- System.out.println();
- String s3 = sc.decodeString(sp3);
- System.out.println(s3);
- StringPart[] sp4 = sc.encodeString("werewolf");
- for(int i=0; i<sp4.length; i++)
- System.out.print(sp4[i]+", ");
- System.out.println();
- String s4 = sc.decodeString(sp4);
- System.out.println(s4);
- }
- }
- class StringCoder
- {
- private String masterString;
- /** @param master the master string for the StringCoder
- * Precondition: the master string contains all the letters of the alphabet
- */
- public StringCoder(String master)
- {
- masterString = master;
- }
- /** @param parts an array of string parts that are valid in the master string
- * Precondition: parts.length > 0
- * @return the string obtained by concatenating the parts of the master string
- */
- //PART A:
- public String decodeString(StringPart[] parts)
- {
- String word = "";
- for(int x = 0; x < parts.length; x++)
- word += parts[x];
- return word;
- }
- /** @param str the string to encode using the master string
- * Precondition: all of the characters in str appear in the master string;
- * str.length() > 0
- * @return a string part in the master string that matches the beginning of str.
- * The returned string part has length at least 1.
- */
- private StringPart findPart(String str)
- {
- int x = 0;
- String s = str.substring(0, x);
- while( masterString.contains(s) )
- {
- x++;
- if(x > str.length())
- break;
- s = str.substring(0, x);
- }
- s = str.substring(0, x - 1);
- int start = masterString.indexOf(s);
- StringPart sp = new StringPart(start, s.length());
- return sp;
- // int length = 0;
- // int start = 0;
- // String word = "";
- // for(int b = 0; b < masterString.length(); b++)
- // {
- // if(masterString.charAt(b) == str.charAt(0))
- // {
- // for(int c = 0; c < str.length(); c++)
- // if(masterString.charAt(b+c) == str.charAt(c))
- // word += str.charAt(b+c);
- // else
- // {
- // length = word.length();
- // start = b;
- // StringPart part = new StringPart(start, length);
- // return part;
- // }
- // }
- // }
- // StringPart part1 = new StringPart(0,0);
- // return part1;
- }
- /** @param word the string to be encoded
- * Precondition: all of the characters in word appear in the master string;
- * word.length() > 0
- * @return an array of string parts of the master string that can be combined
- * to create word
- */
- // Part B
- public StringPart[] encodeString(String word)
- {
- StringPart[] temp = new StringPart[100];
- String temp1 = word;
- int count = 0;
- int length = 0;
- for(int x = 0; x < word.length(); x++)
- {
- temp[x] = findPart(temp1);
- count += temp[x].getLength();
- temp1 = temp1.substring(temp[x].getLength(), temp1.length());
- if(count > word.length()-1)
- {
- length = x;
- break;
- }
- }
- StringPart[] temp2 = new StringPart[length];
- for(int y = 0; y < temp2.length; y++)
- temp2[y] = temp[y];
- return temp2;
- }
- }
- class StringPart
- {
- private int startIndex, lengthPart;
- //private data fields--what does a StringPart know?
- /** @param start the starting position of the substring in a master string
- * @param length the length of the substring in a master string
- */
- public StringPart(int start, int length)
- {
- startIndex = start;
- lengthPart = length;
- }
- /** @return the starting position of the substring in a master string
- */
- public int getStart()
- {
- return startIndex;
- }
- /** @return the length of the substring in a master string
- */
- public int getLength()
- {
- return lengthPart;
- }
- public String toString()
- {
- return "(" + startIndex + ", " + lengthPart + ")";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement