2.5 HOMEWORK


2.5 HOMEWORK

  1. The following code fragment is legal in C++, but not so in C. Why? What would it take to make the code legal in C? Also, what's the value of the variable y?

         double x[10] = {5.1, 7.3};     x[0] = 3.12;     *x = 4.13;     double y;     y = x[0]; 
  2. Write programs in C++ and Java for reading just one integer, just one string, and just one double from the console.

  3. Write programs in C++ and Java for finding the maximum of an array of ints. Your C++ program should use the object cout and the operator ‘<<' for displaying the result. Your Java program should use the System.out.println method for doing the same.

  4. Write programs in C++ and Java that read a text file containing 100 or fewer words and store all the words read into an array of strings.

    Suggestions for the C++ solution:

    1. You can declare an array of string pointers by

          const int N = 100;    string* wordList[ N ] 

      Each word read from the text file can be stored in the form of a string and a pointer to the string deposited in the array wordList.

    2. Create an input stream for reading words from the file by something like

          ifstream readFromFile( sourceFile ); 

      Now you can read one word at a time by

          string buffer;    readFromFile >> buffer 

      A pointer to this word can then be deposited in the array by

          wordList[ i ] = new string( buffer ); 

    Suggestions for the Java solution:

    1. Be prepared to browse the on-line Java documentation at java.sun.com as you go through the rest of the suggestions here.

    2. The author recommends that you read all the characters in the text file into one single string and then break the string into individual words using a StringTokenizer class.

    3. You can use the while loop shown below to read the file one character at a time into one long string called allChars:

          String allChars = " ";    int ch;    Reader in = new FileReader( fileName );    while ( -1 != ( ch = in.read() ) )       allChars += (char) ch; 

      Check out the on-line documentation on the class FileReader in the java.io package. As the documentation says, it is a convenient class for reading character files. Its read() method reads one character at a time into an int. The value read can be compared against-1 to test for the end-of-file condition. For reasons that will be explained in Chapter 6, you must cast the value to char before appending it to allChars.

    4. Now use the StringTokenizer class to break allChars into the individual words. The on-line documentation presents a simple example illustrating how to use this class. We have used same logic in the following code fragment. By invoking hasMoreTokens() and nextToken() alternately on the StringTokenizer object, you can extract all the words from the allChars string.

          String[] wordList = new String[100];    StringTokenizer st = new StringTokenizer( allChars );    int i = 0;    while ( st.hasMoreTokens() )       wordList[i++] = st.nextToken(); 
    5. As was the case with the Java code in this chapter, the I/O functions above throw exceptions. So you must invoke them inside try-catch blocks. For the same pattern as used in the chapter examples.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net