Class MyString

java.lang.Object
  extended by MyString

public class MyString
extends java.lang.Object

Project #0 CS 2334, Section 010 Sep 10, 2008

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:
3.0
Author:
Java Joe

Field Summary
private  char[] data
          A private array to store the characters in the string.
static int DEFAULT_SIZE
          A constant that contains the standard number of characters in a string.
private  int size
          The number of characters that are actually stored in the string.
 
Constructor Summary
MyString(int currentSize)
          A constructor for an empty string of currentSize elements.
 
Method Summary
 char[] getData()
          The accessor for the character array data.
 int getLength()
          An accessor for the variable size.
 void setData(char[] newData)
          A mutator that changes the contents of the character array data.
 void setLength(int newLength)
          A mutator that sets the size of the array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SIZE

public static int DEFAULT_SIZE
A constant that contains the standard number of characters in a string.


data

private char[] data
A private array to store the characters in the string.


size

private int size
The number of characters that are actually stored in the string.

Constructor Detail

MyString

public MyString(int currentSize)
A constructor for an empty string of currentSize elements.

Parameters:
currentSize - The number of characters in the string.
Throws:
java.lang.IllegalArgumentException - if currentSize < 0
Conditions:
PRE - currentSize contains a non-negative integer.
POST - The character array data is of length currentSize.
Method Detail

getLength

public int getLength()
An accessor for the variable size.

Returns:
The number of characters in the current string.

getData

public char[] getData()
The accessor for the character array data.

Returns:
A reference to the array data.

setLength

public void setLength(int newLength)
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.

Parameters:
newLength - The desired length for the modified string.
Throws:
java.lang.IllegalArgumentException - if newLength < 0
Conditions:
PRE - newLength contains a non-negative integer.
POST - The character array data is of length newLength.

setData

public void setData(char[] newData)
A mutator that changes the contents of the character array data.

Parameters:
newData - A reference to the character array to be used.
Conditions:
PRE - The character array data has the same length as newData.
POST - Both data and newData refer to the same character array.