// If there was a package statement, it would go here. // If any import statements existed they would go here. /** * Project #1 * CS 2334, Section 010 * Feb 3, 2011 *
* This is an example class for CS 2334 that shows proper * documentation. It is a class that doesn't do much of anything * useful, which makes it hard to write a really great comment up * here. It is however, properly documented. *
* @version 4.0 */ public class MyString { /** * A constant that contains the standard number of * characters in a string. */ static public int DEFAULT_SIZE = 10; /** A private array to store the characters in the string. */ private char[] data; /** * The number of characters that are actually stored in the * string. */ private int size; /** * A constructor for an empty string of *currentSize
elements.
*
* @param currentSize The number of characters in the
* string.
* @throws IllegalArgumentException if currentSize < 0
* currentSize
contains a non-negative
* integer.
* currentSize
.
*/
public MyString( int currentSize ) {
/* Test preconditions.
* In public methods, preconditions are tested with an "if"
* statement that throws an IllegalArgumentException if the
* the condition fails. In private classes, preconditions
* can be tested using "assert" statements.
*/
if( currentSize < 0 ) {
throw new IllegalArgumentException(
"Illegal currentSize: " + currentSize );
}
data = new char[currentSize];
size = 0; // Set the number of characters stored to zero.
/* Test postconditions.
* In all methods, postconditions are tested using "assert"
* statements.
*/
assert (data.length == currentSize);
} // end MyString
/**
* An accessor for the variable size
.
*
* @return The number of characters in the current string.
*/
public int getLength() {
return size;
} // end getLength
/**
* The accessor for the character array data
.
*
* @return A reference to the array data
.
*/
public char[] getData() {
return data;
} // end getData
/**
* A mutator that sets the size of the array. This is a simple
* method, so it does not have a very sophisticated algorithm.
*
* Algorithm:
* 1. Allocate more space.
* 2. Copy existing data into the new array.
* 3. Record changes in the instance variables.
*
newLength
contains a non-negative
* integer.
* newLength
.
*/
public void setLength( int newLength ) {
/* Test preconditions. */
if( newLength < 0 ) {
throw new IllegalArgumentException(
"Illegal newLength: " + newLength );
}
/*
* Contains the number of characters in the string after it
* has been re-sized.
*/
int newSize;
// Allocate more space.
char[] newData = new char[newLength];
// Copy existing data into the new array.
for( int i = 0; i< Math.min( newLength, size ); i++ ) {
newData[i] = data[i];
} // end for
// Record changes in the instance variables.
data = newData;
size = Math.min( newLength, size );
/* Test postconditions. */
assert (data.length == newLength);
} // end setLength
/**
* A mutator that changes the contents of the character array
* data
.
*
* @param newData A reference to the character array to be
* used.
* newData
.
*