import java.lang.*;
public class FunctionTypeMethod2
{
public static void main (String[] args)
{
// EXAMPLE 2:
//
// this function takes a string containing a blank and
// returns the first word in the string
// (all the characters up to the first blank)
//
// function firstWord ( str : string ) : string
//
// for i: 1 .. length ( str )
//
// if str ( i ) = " " then
//
// result str ( 1 .. i - 1 )
//
// end if
//
// end for
//
// end firstWord
//
// MAIN PROGRAM IN TURING:
// put firstWord ("Henry Hudson") % the output is Henry Hudson
int j = 6;
System.out.println ("This is the return value of the 'firstWord' function: "
+ firstWord ("Henry Hudson", j));
System.out.println ("This is the end of the 'firstWord' function program!");
} // main method
// this function returns all the characters up to the first blank space
public static String firstWord (String str, int j)
{
System.out.println ("j = " + j); // length of first word passed in
System.out.println (str);
String strA = str.substring (0, j);
System.out.println (strA);
// no for loop or if statement is necessary
// to use Java String objects and substrings here
return (strA);
} // firstWord method
} // FunctionTypeMethod2 class
No comments:
Post a Comment