Assignment 4 SimpleSBuilderjava httppeoplecspitteduramirezc

Assignment 4

SimpleSBuilder.java - http://people.cs.pitt.edu/~ramirez/cs401/assigs/assig4/SimpleSBuilder.java

MySBuilder.java - http://people.cs.pitt.edu/~ramirez/cs401/assigs/assig4/MySBuilder.java

SBuilderTest.java - http://people.cs.pitt.edu/~ramirez/cs401/assigs/assig4/SBuilderTest.java

a4out.txt - http://people.cs.pitt.edu/~ramirez/cs401/assigs/assig4/a4out.txt
ooo Verizon 10:01 PM Touch to ret people.cs.pitt.edu CS 0401 Intermediate Programming Topics: More arrays and inheritance Online: Sunday, November 6, 2016 Due: All source (java) files (including any test files) and a completed Assignment Information Sheet zipped into a single file and submitted via the submission sits by 11:59PM on Friday November 18, 2016 Late Due Date: 1:59PM on Monday, November 21, 2016 Submission note: Your zip file should contain all of your .java files (including any provided for you, such as SimpleSBuilder.java and SBuilderTest.java), and your Assignment Information Sheet. There should be no-class files, no project files or project subdirectories in the zip file. Also, your TA must be able to compile your program from the command line using the javac command. Test your files to make sure this will work (especially if you use an IDE such as NetBeans for development). If the TA cannot compile your program you will receive minimal credit! We have discussed encapsulation, abstraction and inheritance in lecture, and have seen several examples illustrating these principles. We have also discussed arrays and the logical vs. physical size when an array is used to store data In this assignment you will implement your own (slightly simplified) version of a StringBuilder, called MySBuilder, using an underlying array of characters to represent your string. The class is specified as follows: publie elass MySBuilder extends SimpleSBuilder I have implemented SimpleSBuilder for you using an array of char for the underlying data. Look it over in file SimpleSBuildrjava. This class has some basic functionality but clearly is very limited. The class you will write will inherit this basic functionality, but you must add many additional constructors, accessors and mutators. I have listed the required methods for MySBuilder with comments in file MySBuilder.java Read over these comments very carefully, as they describe what you need to de in each of the methods. To complete this assignment you can simply download the provided MySBuilder.java file and fill in the missing code for each of the methods. Once completed, your MySBuilder class must run with the driver program SBuilderTest.java and produce output identical to that shown in adout.txt. All code in your MySBuilder class must be your own. You may not copy code from the Internet or any predefined Jav a source code -You may not use the predefined StringBuilder or StringBuffer or any similar classes in your - You may not use the String class other than in a very restricted way (as an argument or returm -Some methods have additional requirements /restrictions. Read over the comments for each code. type, possibly accessing the characters in the String but not using any other String methods) method carefully before implementing it.

Solution

import java.util.ArrayList;

// CS 0401 Fall 2016
// MySBuilder class. You must implement this class for Assignment 4. A shell of
// the class is already provided -- you must fill in the method bodies.

// Note 1: All code for these methods must be your own! Do not copy these methods
// from code on the internet. If you do so it will be a violation of the student
// academic integrity code!

// Note 2: You may NOT use StringBuilder or StringBuffer or any similar class in
// any of these methods! You also may not use String for anything other than the
// argument and return object types (when needed) - i.e. you may not create String
// objects in order to use String methods to perform any of the methods here.

// Note 3: Some of the methods have additional requirements / restrictions. Read
// the comments for each method carefully before implementing it.

public class MySBuilder extends SimpleSBuilder
{
   // Data is inherited
   // See SimpleSBuilder for inherited methods
  
   // Redefining inherited constructors to get correct type
   public MySBuilder(int capacity)
   {
       super(capacity);
   }
  
   public MySBuilder(String str)
   {
       super(str);
   }
  
   public MySBuilder(char [] str)
   {
       // Build a new MySBuilder from an array of char
       // Array length should be twice the length of str
       super(String.valueOf(str));
   }
  
   public MySBuilder(MySBuilder old)
   {
       // Copy constructor
       // Array length should be twice the logical size of old
       super(String.valueOf(old.data));
   }
  
   // For all append methods, if the additional characters will
   // fit in the array, just add them. If they will not fit, resize
   // the array to twice the logical size of the resulting SBuilder
   // (so it is exactly 1/2 full following the operation)
  
   public MySBuilder append(String str)
   {
       // Append str to end of this MySBuilder
       // return this
      
       if(str.length() > this.length() / 2){  
          
           this.resize(2*(str.length()+this.length()));
       }
      
       for(int i = 0; i<str.length(); i++){
           this.data[this.length()+i] = str.charAt(i);
           this.len++;
       }
       return this;
   }
  
   public MySBuilder append(MySBuilder S)
   {
       // Append S to end of this MySBuilder
       // return this
       return append(new String(S.data, 0, S.length()));
   }
  
   public MySBuilder append(char [] str)
   {
       // append str to end of this MySBuilder
       // return this
       return append(new String(str, 0, str.length));
   }
  
   public MySBuilder append(char c)
   {
       // append c to end of this MySBuilder
       // return this
       char[] t = new char[1];
       t[0] = c;
       return append(new String(t, 0, 1));
   }
  
   public MySBuilder delete(int start, int end)
   {
       // delete characters from start (inclusive) to end (exclusive)
       // from this MySBuilder, shifting to fill in the gap. If range
       // is invalid do nothing.
       // return this
       if(end<start || end > this.length() || start < 1)
           return this;
      
       int j = 0;
       for(int i = end-1; i<this.length(); i++){
           this.data[start-1+j] = this.data[i];
           this.data[i] = \' \';
           j++;
           this.len--;
       }
       return this;
   }
  
   public MySBuilder deleteCharAt(int index)
   {
       // delete char at index from this MySBuilder, shifting to fill in the
       // gap. If index is invalid do nothing
       // return this
       return delete(index,index+1);
      
   }
  
   public int indexOf(String str)
   {
       // return the beginning index where str matches a substring within
       // this MySBuidler. If there is no match return -1.
      
       int r = -1;
      
       for(int i = 0; i<this.length(); i++){
           int j = 0, k=i,tmp = i;
           if(str.charAt(j) == this.data[k]){
              
               while(str.charAt(j) == this.data[k] && j < str.length()
                       && k < this.length()){
                   j++;
                   k++;
               }
               if(j == str.length()) {
                   r = tmp+1;
                   break;
               }
           }
       }
      
       return r;
   }

   public int indexOf(String str, int fromIndex)
   {
       // return the beginning index where str matches a substring within
       // this MySBuilder, starting the search at location fromIndex.
       // If there is no match return -1.
       int r = -1;
      
       for(int i = fromIndex-1; i<this.length(); i++){
           int j = 0, k=i,tmp = i;
           if(str.charAt(j) == this.data[k]){
              
               while(str.charAt(j) == this.data[k] && j < str.length()
                       && k < this.length()){
                   j++;
                   k++;
               }
               if(j == str.length()) {
                   r = tmp+1;
                   break;
               }
           }
       }
      
       return r;
   }

   public MySBuilder insert(int offset, String str)
   {
       // insert str into this MySBuilder, beginning at index offset. Shift
       // any existing characters to the right to make space. If offset < 0
       // or offset > len do nothing. If the array must be resized, it should
       // be twice the size of the resulting string.
       // return this
      
       if(this.capacity() < this.length()+str.length())
           this.resize(2*(this.length()+str.length()));
      
       int j = this.length()+(this.length()-offset)-1;
       for(int i=this.length()-1;i>offset;i--){
           data[j] = data[i];
           j--;
       }
       for(int i = 0;i<str.length();i++){
           data[-1+offset++] = str.charAt(i);
           this.len++;
       }
       return this;
   }

   public MySBuilder insert(int offset, char [] str)
   {
       // Same as above but with array of char argument
       return this.insert(offset, new String(str,0,str.length));
   }
  
   public MySBuilder insert(int offset, char c)
   {
       // Same as above but with char argument
       char[] t = new char[1];
       t[0] = c;
       return this.insert(offset,t);
   }
  
   public MySBuilder insert(int offset, MySBuilder S)
   {
       // Same as above but with MySBuilder argument
       return this.insert(offset,S.data);
   }

   public MySBuilder replace(int start, int end, String str)
   {
       // Remove the characters from start (inclusive) to end (exclusive)
       // and insert str starting at start. Do not call delete followed
       // by insert, since this will require shifting twice within the
       // array and is very inefficient. You should shift only one time
       // (the direction depends on the relative lengths of the substring
       // removed vs the string inserted). Note that if the lengths of
       // the removed and inserted strings are the same, you should not
       // shift at all.
       // return this
       if(end-start != str.length()) return this;
       for(int i = 0;i<str.length();i++){
           data[-1+start++] = str.charAt(i);
           this.len++;
       }
       return this;
   }
  
   public String substring(int start, int end)
   {
       // return a new String which is the substring of this MySBuilder
       // from start (inclusive) to end (exclusive). If the range is
       // invalid return null
       if(start<0 || end > this.length() || start > end) return null;
      
       char[]tmp = new char[end-start];
      
       for(int i = 0;i<end-start;i++)
           tmp[i] = this.data[start-1+i];
      
       return new String(tmp,0,end-start);
      
   }
  
   public String [] split(char delim)
   {
       // Simplified split method returns array of String which the delim
       // character will divide the MySBuilder into. Be careful of special
       // cases where delim occurs in consecutive locations or at the front
       // or end of the MySBuilder. In these cases the extra delim characters
       // should be ignored. See examples in SBuilderTest.java. You MAY NOT
       // use any predefined split() method or variant thereof to do this method.
       // It must be done from scratch on the underlying array of characters.
       // You may use an ArrayList<String> to store your temporary results, and
       // the ArrayList method toArray(T [] a) method to return them. See the
       // ArrayList API for details.
      
       ArrayList<String> s = new ArrayList<String>();
      
       String[] s1 = new String[s.size()];
       return s.toArray(s1);
   }
}

Assignment 4 SimpleSBuilder.java - http://people.cs.pitt.edu/~ramirez/cs401/assigs/assig4/SimpleSBuilder.java MySBuilder.java - http://people.cs.pitt.edu/~ramir
Assignment 4 SimpleSBuilder.java - http://people.cs.pitt.edu/~ramirez/cs401/assigs/assig4/SimpleSBuilder.java MySBuilder.java - http://people.cs.pitt.edu/~ramir
Assignment 4 SimpleSBuilder.java - http://people.cs.pitt.edu/~ramirez/cs401/assigs/assig4/SimpleSBuilder.java MySBuilder.java - http://people.cs.pitt.edu/~ramir
Assignment 4 SimpleSBuilder.java - http://people.cs.pitt.edu/~ramirez/cs401/assigs/assig4/SimpleSBuilder.java MySBuilder.java - http://people.cs.pitt.edu/~ramir
Assignment 4 SimpleSBuilder.java - http://people.cs.pitt.edu/~ramirez/cs401/assigs/assig4/SimpleSBuilder.java MySBuilder.java - http://people.cs.pitt.edu/~ramir

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site