CS1 Course Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
CS1 Course Site  
 
 
 
Wait , WAIT !!What's that word ? ( Class Notes ( without definitions ) )

Monday November 6 , 2000
 
String Processing
specification (very partial) of java.lang.String class.
  1. < string > .length () -- > < int >
    ``CS1 exams are fun!   '' .length = 18
  2. < string > .indexOf (< string >) -- > < int >
    ``frim fram sauce''.indexOf ( ``fr'') = 0
    ``frim fram sauce''.indexOf ( ``fra'') = 5
  3. < string > .substring (< int >) -- > < string >
    returns the substring from the position specified by the parameter to the end of the string.
    ``Monday''.substring (3) =  ``day''}
  4. < string > .substring (< int >,  < int >) -- > < string >
    returns the substring from the position of the first parameter to the position one less than the second parameter.
    ``tired hand''.substring (2,  7) =  ``red h''
``Holliday,  Billy'' singer.indexOf ( ``,  '' )
static private string firstName (string dsname)
{
    < compute portion of comma >;
    < compute first name >;
    return the first name;
}
 

static private void computeTheAverage ()
{
    int sum = 0;
    for (int x = 0;  x < nrElements;  x + +)
    {
        sum = sum + a [x];
    }
    average = (double) sum / (double) nrElements;
}
 

static private void displayTheNumberAboveAverage ()
{
    for (int x = 0;  x < nrElements;  x = x + 1)
    {
        if (a [x] > average)
        {
            IO.println (a [x]);
        };
    };
};
More String Functionality...

  1. < string > .equals (< string >) -- > < boolean >
    ``Cat''.equals ( ``dog'') = false
    ``cat''.equals ( ``cat'') = true
    ``cat''.equals ( ``Cat'') = False
  2. < string > .equalsIgnoreCase (< string >) -- > < boolean >
  3. < string > .compareTo (< string >) -- > < int >
    Returns...
    • 0 - if the two strings are equal
    • Something negative if the recieving string is alphabetically less than the parameter.
    • Something positive if the recieving string is alphabetically greater than the parameter.
    Ex
    ``cat''.compareTo ( ``bat'') -- > +
    ``bat''.compareTo ( ``bats'') -- > -