Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 KB | None | 0 0
  1. //Name:    Date:  
  2.  
  3.    import java.util.*;
  4.     public class StringCoder_shell
  5.    {
  6.        public static void main(String[] args)
  7.       {
  8.          StringCoder sc = new StringCoder("sixtyzipperswerequicklypickedfromthewovenjutebag");
  9.          StringPart[] sp = sc.encodeString("overeager");
  10.          for(int i=0; i<sp.length; i++)
  11.             System.out.print(sp[i]+", ");
  12.          System.out.println();
  13.          String s = sc.decodeString(sp);
  14.          System.out.println(s);
  15.          
  16.          StringPart[] sp2 = sc.encodeString("kippers");
  17.          for(int i=0; i<sp2.length; i++)
  18.             System.out.print(sp2[i]+", ");
  19.          System.out.println();
  20.          String s2 = sc.decodeString(sp2);
  21.          System.out.println(s2);
  22.        
  23.          StringPart[] sp3 = sc.encodeString("colonials");
  24.          for(int i=0; i<sp3.length; i++)
  25.             System.out.print(sp3[i]+", ");
  26.          System.out.println();
  27.          String s3 = sc.decodeString(sp3);
  28.          System.out.println(s3);
  29.        
  30.          StringPart[] sp4 = sc.encodeString("werewolf");
  31.          for(int i=0; i<sp4.length; i++)
  32.             System.out.print(sp4[i]+", ");
  33.          System.out.println();
  34.          String s4 = sc.decodeString(sp4);
  35.          System.out.println(s4);
  36.       }
  37.    }
  38.  
  39.  
  40.     class StringCoder
  41.    {
  42.       private String masterString;
  43.    /** @param master the master string for the StringCoder
  44.    * Precondition: the master string contains all the letters of the alphabet
  45.    */
  46.        public StringCoder(String master)
  47.       {
  48.          masterString = master;
  49.       }
  50.    
  51.    /** @param parts an array of string parts that are valid in the master string
  52.    * Precondition: parts.length > 0
  53.    * @return the string obtained by concatenating the parts of the master string
  54.    */
  55.       //PART A:
  56.        public String decodeString(StringPart[] parts)
  57.       {
  58.          String word = "";
  59.          for(int x = 0; x < parts.length; x++)
  60.             word += parts[x];
  61.          return word;
  62.       }
  63.    
  64.    
  65.    /** @param str the string to encode using the master string
  66.    * Precondition: all of the characters in str appear in the master string;
  67.    * str.length() > 0
  68.    * @return a string part in the master string that matches the beginning of str.
  69.    * The returned string part has length at least 1.
  70.    */
  71.        private StringPart findPart(String str)
  72.       {
  73.            int x = 0;
  74.             String s = str.substring(0, x);
  75.             while( masterString.contains(s) )
  76.             {
  77.                x++;
  78.                if(x > str.length())
  79.                break;
  80.                s = str.substring(0, x);
  81.             }
  82.            s = str.substring(0, x - 1);
  83.             int start = masterString.indexOf(s);
  84.             StringPart sp = new StringPart(start, s.length());
  85.             return sp;
  86.          //  int length = 0;
  87.          //             int start = 0;
  88.          //             String word = "";
  89.          //             for(int b = 0; b < masterString.length(); b++)
  90.          //             {
  91.          //                if(masterString.charAt(b) == str.charAt(0))
  92.          //                {
  93.          //                   for(int c = 0; c < str.length(); c++)
  94.          //                      if(masterString.charAt(b+c) == str.charAt(c))
  95.          //                         word += str.charAt(b+c);
  96.          //                      else
  97.          //                      {
  98.          //                         length = word.length();
  99.          //                         start = b;
  100.          //                         StringPart part = new StringPart(start, length);
  101.          //                         return part;
  102.          //                      }
  103.          //                }
  104.          //             }
  105.          //             StringPart part1 = new StringPart(0,0);
  106.          //             return part1;
  107.       }
  108.    
  109.    
  110.    /** @param word the string to be encoded
  111.    * Precondition: all of the characters in word appear in the master string;
  112.    * word.length() > 0
  113.    * @return an array of string parts of the master string that can be combined
  114.    * to create word
  115.    */
  116.    // Part B
  117.        public StringPart[] encodeString(String word)
  118.       {
  119.          StringPart[] temp = new StringPart[100];
  120.          String temp1 = word;
  121.          int count = 0;
  122.          int length = 0;
  123.          for(int x = 0; x < word.length(); x++)
  124.          {
  125.             temp[x] = findPart(temp1);
  126.             count += temp[x].getLength();
  127.             temp1 = temp1.substring(temp[x].getLength(), temp1.length());
  128.             if(count > word.length()-1)
  129.             {
  130.                length = x;
  131.                break;
  132.             }
  133.          }
  134.          
  135.          StringPart[] temp2 = new StringPart[length];
  136.          for(int y = 0; y < temp2.length; y++)
  137.             temp2[y] = temp[y];
  138.          return temp2;
  139.       }
  140.    }
  141.  
  142.  
  143.     class StringPart
  144.    {
  145.       private int startIndex, lengthPart;
  146.    
  147.    //private data fields--what does a StringPart know?
  148.    
  149.    
  150.    /** @param start the starting position of the substring in a master string
  151.    * @param length the length of the substring in a master string
  152.    */
  153.        public StringPart(int start, int length)
  154.       {
  155.          startIndex = start;
  156.          lengthPart = length;
  157.       }
  158.    
  159.    /** @return the starting position of the substring in a master string
  160.    */
  161.        public int getStart()
  162.       {
  163.          return startIndex;
  164.       }
  165.    
  166.    /** @return the length of the substring in a master string
  167.    */
  168.        public int getLength()
  169.       {
  170.          return lengthPart;
  171.       }
  172.        public String toString()
  173.       {
  174.          return "(" + startIndex + ", " + lengthPart + ")";
  175.       }
  176.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement