/** * Project #5.6 * CS 2334, Section 001 * May 12, 2004 *
* This is an example class that shows how to test * preconditions and postconditions using assert and exceptions. *
* @author John Coffee * @version 2.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-1];
// Copy existing data into the new array.
for( int i = 0; i< Math.min( newLength-1, 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
.
* * @param args Contains the command line arguments. */ public static void main( String args[] ) { // Create a string wth a default size of 10. MyString str = new MyString(10); // Add some data. String dummy = new String( "Hello World!" ); str.setData( dummy.toCharArray() ); // Print the string. System.out.println( str.getData() ); // Change the size. str.setLength( -5 ); } } // End of class MyString