Lab 11: Working with the Object Class

due Thursday, February 20 at 11:59pm on Canvas

Pair Programming Required (or No Credit)
  • Both partners fill in, sign, date, and submit the pair programming contract
  • Upload the document along with your Lab code to Canvas.
  • Only ONE partner submits the Lab code on Canvas.
  • BOTH partners submit the contract.
  • Please make sure both your names are on all the files.
  • If you need help finding a partner, please contact me as soon as possible.


The StringCreator Class

  • In this assignment you will write a class similar to Java's StringBuilder class, which we will cover at the end of this quarter.
  • You will write many identical or similar methods to those contained in StringBuilder.
  • Begin by copying and pasting the below starter code into a file named StringCreator.java.
  • Then, implement the methods according to the Javadoc comment provided for each method.
  • As you write each method, run the test file to verify that your code is working properly.
  • Hint: Comment out any method that you have not yet written, and then comment in only the tests for each newly-written method.
  • Note that any error messages should be identical to those in the sample output.
  • Note that you are not allowed to alter the starter code for the StringCreator class. Instead, you must only write each method.
StringCreator.java Starter Code:

/**
 * @author
 * @author
 * CIS 36B, Lab 11
 */
public class StringCreator {
    private char[] array;
    private int numChars;
    
    /**
     * Default Constructor for StringCreator
     * Initializes array to be of length 10
     * Calls the StringCreator(capacity) constructor
     * Hint: Use this()!

     * and numChars to 0
     */
    public StringCreator() {
       
    }
    
    /**
     * Constructor for StringCreator
     * @param capacity the length of array
     * initializes numChars to 0;
     */
    public StringCreator(int capacity) {
       
    }
    
    /**
     * Constructor for StringCreator
     * @param s a String to convert
     * to a StringCreator, whose
     * capacity is the length of the String + 10
     */
    public StringCreator(String s) {
       
    }
    
    
    /**
     * Copy constructor for the StringCreator class
     * @param s another StringCreator
     * Makes a DEEP copy
     */
    public StringCreator(StringCreator s) {
       
    }
    
    /**
     * Inserts the String representation
     * of a boolean at the end of the StringCreator
     * Hint: use concatenation to create a String
     * @param b a value to append
     * Calls increaseCapacity when
     * necessary to provide sufficient
     * space to append b
     * @return this StringCreator
     */
    public StringCreator append(boolean b) {
       
        return this;
    }
    
    /**
     * Inserts a char at the
     * end of the StringCreator
     * @param c a value to append
     * Calls increaseCapacity when
     * necessary to provide sufficient
     * space to append c
     * @return this StringCreator
     */
    public StringCreator append(char c) {
       
        return this;
    }
    
    /**
     * Inserts the given char array
     * at the end of the StringCreator
     * @param c an array to append
     * Calls increaseCapacity when
     * necessary to provide sufficient
     * space to append c
     * @return this StringCreator
     */
    public StringCreator append(char[] c) {
       
        return this;
    }
    
    
    /**
     * Inserts the String representation
     * of a double at the end of the StringCreator
     * Hint: use concatenation to create a String
     * @param d a double to append
     * Calls increaseCapacity as many
     * times as necessary to provide sufficient
     * space to append d
     * @return this StringCreator
     */
    public StringCreator append(double d) {
       
        return this;
    }
    
    /**
     * Inserts the String representation
     * of an int at the end of the StringCreator
     * Hint: use concatenation to create a String
     * @param i an int to append
     * Calls increaseCapacity as many
     * times as necessary to provide sufficient
     * space to append i
     * @return this StringCreator
     */
    public StringCreator append(int i) {
       
        return this;
    }
    /**
     * Appends the String representation of
     * the Object argument.
     * @param o the Object to append
     * Calls increaseCapacity as many
     * times as necessary to provide sufficient
     * space to append o
     * @return this StringCreator
     */
    public StringCreator append(Object o) {
       
        return this;
    }
    
    /**
     * Returns whether array is
     * at its maximum capacity
     * @return whether array is full
     */
    private boolean atCapacity() {
        return false;
    }

    /**
     * Returns the char at the given position
     * @param i the position of the char
     * @return the char located at i
     * Prints an error message for an index
     * which is out of bounds
     * CharAt error: Index <index> is out of bounds.
     * Index must be in the range 0 to <end>.
     * and returns the empty char \0
     */
    public char charAt(int index) {       

        return '\0';
    }
    
    /**
     * Determines if a specified char is
     * contained within the Strung
     * @param c the char to locate
     * @return whether c is in Strung
     */
    public boolean contains(char c) {
       
        return false;    
    }
    
    /**
     * removes the chars from index start
     * up to but no including end by creating a new array
     * @param start the starting index
     * @param end the ending index
     * The new array should have the
     * same capacity as the old array
     * Prints an error message for any index
     * which is out of bounds
     * Delete error: Starting index <index> is out of bounds.
     * Index must be in the range 0 to <end>
     * then returns the StringCreator unchanged
     * or
     * Delete error: Ending index <index> is out of bounds.
     * Index must be in the range 0 to <end>
     * then returns the StringCreator unchanged
     * @return this StringCreator
     */
    public StringCreator delete(int start, int end) {
       
        return this;
    }
    
    /**
     * Determines whether this StringCreator
     * is equal to another Object o
     * Two StringCreators are equal if they contain
     * the same chars in the same order
     * Hint: compare the sizes first and then
     * the data in the two arrays one-by-one
     * using a for loop
     * Note that you must use the equals formula
     * provided in Lesson 11 to receive credit
     * for this method
     */
    @Override public boolean equals(Object o){
        return false;
    }
    
    /**
     * Returns the location of a char
     * @param c the char to locate
     * @return the location of c
     */
    public int indexOf(char c) {
       
        return -1;    
    }
    
    /**
     * Increases the capacity of array
     * by creating a new array whose length
     * is 10 more than array's initial length
     * Helper method for append and insert
     * when array is at maximum capacity
     */
    private void increaseCapacity() {
       
    }
    /**
     * Inserts a new char at a given index
     * @param position the index in the StringCreator
     * @param c the new char to insert
     * Prints an error message for any index
     * which is out of bounds
     * Insert error: Index <index> is out of bounds.
     * Index must be in the range 0 to <end>
     * then returns the StringCreator unchanged
     * @return this StringCreator
     */
    public StringCreator insert(int position, char c) {
       
        return this;
    }
    
    /**
     * Returns the amount of space left
     * to insert at the end of the array
     * @return the remaining capacity in the
     * array
     */
    private int remainingCapacity() {
        return -1;
    }
    
    /**
     * Returns the number of characters
     * @return the number of characters
     */
    public int size() {
        return -1;
    }
    
    /**
     * Converts this StringCreator to
     * all lower case characters
     */
    public StringCreator toLowerCase() {
       
        return this;
    }
    
    /**
     * Converts this StringCreator to
     * all upper case characters
     */
    public StringCreator toUpperCase() {
       
        return this;
    }

    
    /**
     * toString method for the StringCreator class
     * @return a String of all characters
     * with no spaces between any character
     */
    @Override public String toString(){
        return "";
    }   
}



StringCreatorTest.java:

/**
 * @author parrishj
 * CIS 36B, Lab 11
 */

public class StringCreatorTest {
    public static void main(String[] args) {
        /*Constructors*/
        System.out.println("\n***Testing Constructors***");
        StringCreator s = new StringCreator("ABC");
        System.out.println("Testing String Constructor: Should print ABC: " + s);
        StringCreator s1 = new StringCreator("");
        System.out.println("Testing String Constructor: Should print an empty String: " + s1);
        StringCreator s2 = new StringCreator(s);
        System.out.println("Testing Copy Constructor: Should print ABC: " + s2);
        s2.toLowerCase();
        System.out.println("Testing For Deep Copy: Copy should print abc: " + s2);
        System.out.println("Testing For Deep Copy: Original should print ABC: " + s);
       
        //append
        System.out.println("\n***Testing Append()***");
       
        System.out.println("Should print ABCtrue: " + s.append(true));
        System.out.println("Size should be 7: " + s.size());
        System.out.println("Should print ABCtruefalse: " + s.append(false));
        System.out.println("Size should be 12: " + s.size());
        System.out.println("Should print ABCtruefalse!: " + s.append('!'));
        System.out.println("Size should be 13: " + s.size());
        char[] abc = {'A', 'B', 'C'};
        System.out.println("Should print ABCtruefalse!ABC: " + s.append(abc));
        System.out.println("Size should be 16: " + s.size());
        System.out.println("Should print ABCtruefalse!ABC3.1415926535898: "
                + s.append(3.1415926535898));
        System.out.println("Size should be 31: " + s.size());
        System.out.println("Should print ABCtruefalse!ABC3.14159265358980: "
                + s.append(0));
        System.out.println("Size should be 32: " + s.size());
        System.out.println("Should print ABCtruefalse!ABC3.14159265358980abc: "
                + s.append(s2));
        System.out.println("Size should be 35: " + s.size());
       
        //charAt
        System.out.println("\n***Testing CharAt()***");
        System.out.println("Should print B: " + s.charAt(1));
        System.out.println("Should print b: " + s2.charAt(1));
        System.out.println("Should print C: " + s.charAt(15));
        System.out.print("Should print error: ");
        s2.charAt(15);
        System.out.print("Should print error: ");
        s2.charAt(-1);
       
        //contains
        System.out.println("\n***Testing Contains***");
        System.out.println("Should print true: " + s2.contains('a'));
        System.out.println("Should print false: " + s2.contains('f'));
       
        //delete
        System.out.println("\n***Testing Delete***");
        System.out.println("Should print ABCt3.14159265358980abc: " + s.delete(4, 16));
        System.out.println("Should print size of 23: " + s.size());
        System.out.println("Should print t3.14159265358980abc: " + s.delete(0, 3));
        System.out.println("Should print size of 20: " + s.size());
        System.out.println("Should print abc: " + s.delete(0, 17));
        System.out.println("Should print size of 3: " + s.size());
        System.out.println("Should print an error: ");
        s.delete(-1, 2);
        System.out.println("Should print an error: ");
        s.delete(0, 60);
       
        //equals
        System.out.println("\n***Testing Equals***");
        System.out.println("Should print true: " + s.equals(s2));
        System.out.println("Should print false: " + s.equals(s1));
        System.out.println("Should print false: " + s.equals("abc"));
        System.out.println("Should print false: " + s.equals(null));
       
        //indexOf
        System.out.println("\n***Testing IndexOf***");
        System.out.println("Should print 0: " + s.indexOf('a'));
        System.out.println("Should print 2: " + s.indexOf('c'));
        System.out.println("Should print -1: " + s1.indexOf('d'));
        System.out.println("Should print -1: " + s2.indexOf('d'));
       
        //insert
        System.out.println("\n***Testing Insert***");
        System.out.println("Should print aabc: " + s.insert(1, 'a'));
        System.out.println("Should print aabcc: " + s.insert(3, 'c'));
        System.out.println("Should print 1abc: " + s2.insert(0, '1'));
        System.out.println("Should print an error: ");
        s.insert(-1, 'A');
        System.out.println("Should print an error: ");
        s.insert(10, 'A');
       
        //size
        System.out.println("\n***Testing Size***");
        System.out.println("Should print 5: " + s.size());
        System.out.println("Should print 4: " + s2.size());
       
        //toLowerCase
        System.out.println("\n***Testing ToLowerCase***");
        System.out.println("Should print aabcc: " + s.toLowerCase());
        System.out.println("Should print 1abc: " + s2.toLowerCase());
       
        //toUpperCase
        System.out.println("\n***Testing ToUpperCase***");
        System.out.println("Should print AABCC: " + s.toUpperCase());
        System.out.println("Should print 1ABC: " + s2.toUpperCase());
       
    }
}


StringCreatorTest Output: Your Output must be **IDENTICAL** to the output below for full credit:




***Testing Constructors***
Testing String Constructor: Should print ABC: ABC
Testing String Constructor: Should print an empty String:
Testing Copy Constructor: Should print ABC: ABC
Testing For Deep Copy: Copy should print abc: abc
Testing For Deep Copy: Original should print ABC: ABC

***Testing Append()***
Should print ABCtrue: ABCtrue
Size should be 7: 7
Should print ABCtruefalse: ABCtruefalse
Size should be 12: 12
Should print ABCtruefalse!: ABCtruefalse!
Size should be 13: 13
Should print ABCtruefalse!ABC: ABCtruefalse!ABC
Size should be 16: 16
Should print ABCtruefalse!ABC3.1415926535898: ABCtruefalse!ABC3.1415926535898
Size should be 31: 31
Should print ABCtruefalse!ABC3.14159265358980: ABCtruefalse!ABC3.14159265358980
Size should be 32: 32
Should print ABCtruefalse!ABC3.14159265358980abc: ABCtruefalse!ABC3.14159265358980abc
Size should be 35: 35

***Testing CharAt()***
Should print B: B
Should print b: b
Should print C: C
Should print error: CharAt error: Index 15 is out of bounds.
Index must be in the range 0 to 2.
Should print error: CharAt error: Index -1 is out of bounds.
Index must be in the range 0 to 2.

***Testing Contains***
Should print true: true
Should print false: false

***Testing Delete***
Should print ABCt3.14159265358980abc: ABCt3.14159265358980abc
Should print size of 23: 23
Should print t3.14159265358980abc: t3.14159265358980abc
Should print size of 20: 20
Should print abc: abc
Should print size of 3: 3
Should print an error:
Delete error: Starting index -1 is out of bounds.
Index must be in the range 0 to 2.
Should print an error:
Delete Error: Ending index 60 is out of bounds.
Index must be in the range 0 to 2.

***Testing Equals***
Should print true: true
Should print false: false
Should print false: false
Should print false: false

***Testing IndexOf***
Should print 0: 0
Should print 2: 2
Should print -1: -1
Should print -1: -1

***Testing Insert***
Should print aabc: aabc
Should print aabcc: aabcc
Should print 1abc: 1abc
Should print an error:
Insert error: Index -1 is out of bounds.
Index must be in the range 0 to 4.
Should print an error:
Insert error: Index 10 is out of bounds.
Index must be in the range 0 to 4.

***Testing Size***
Should print 5: 5
Should print 4: 4

***Testing ToLowerCase***
Should print aabcc: aabcc
Should print 1abc: 1abc

***Testing ToUpperCase***
Should print AABCC: AABCC
Should print 1ABC: 1ABC


What to Submit:

  • Submit your completed StringCreator.java file (make sure your both names are on the file) to Canvas.
  • If working with a parnter: One partner submits StringCreator.java and the pair programming contract.
    • The other other partner only submits the pair programming contract.
  • 5 point deduction for missing pair programming contract
  • 5 point deduction for code that is not properly formatted (Note: Go to Source->Format on Eclipse)
  • 5 point deduction if you do not remove any package statements
  • 5 point deduction for submitting your code as a zip file

How You Will Be Graded:

  • 100 points: The assignment works as shown in the sample output
  • 80-100 points: The assignment works mostly as shown in the sample output. Most required features are implemented and implemented correctly.
  • 40-80 points: Some of the assignment works as shown in the sample output. Some required features are implemented and implemented correctly.
  • 10-40 points: Serious problems in implementation and little correspondence between sample output and what is shown. Few of the features implemented or implemented correctly.
  • 0 points: Code does not compile or outputs an error at runtime.