4.6 HOMEWORK


4.6 HOMEWORK

  1. How many String objects will be created by the following Java statements:

         String s1 = "hoity";     String s2 = s1;     s1 = s2 + "toity"; 
  2. How many String objects are created by the following Java statements:[25]

         String s1 = "hoity";     String s2 = "hoity";       String s3 = "hoity";     String s4 = "hoity";     char[] charArr = [ 'h', 'o', 'i', 't', 'y' ];     String s5 = new String( charArr );     String s6 = new String( s1 ); 
  3. The goal of this homework is to write a Java program to determine whether a string is a palindrome. (A palindrome is a word, phrase, verse, or sentence that reads the same backward or forward.) The string may contain other characters that are not alphanumeric. These characters are to be removed first before determining whether a string is a palindrome. More specifically, your solution should be structured along the following lines:

    1. Provide implementation for a function removeAllMarks( String ) that takes the original string as its argument and returns a new string after the original string has been stripped of the punctuation marks.

      [Suggestion: A convenient way to do this is by scanning the argument string with the charAt ( int i ) method presented in Section 4.4.2 and checking whether the character at position i is alphanumeric or not. This test can be carried out by invoking Character.isLetterOrDigit( char ) on the character. In order to create a clean version of the string, all characters that pass the test can be appended to a StringBuffer object with the append ( char ) method defined for the StringBuffer class. Finally, the StringBuffer object can be converted into a String object by using the toString() method of the StringBuffer class.]

    2. Provide implementation for reverseString(String str) which returns a new string obtained by reversing the argument string.

      [Suggestion: You can convert the argument string into a StringBuffer object via the latter's constructor and then invoke the reverse () method on the StringBuffer object.]

    3. Provide implementation for isPalindrome( string ) that will return true or false as to whether the argument string is a palindrome. Compare the cleaned up version of the original string with the cleaned up and reversed version using the compareToIgnoreCase( String ) method of the String class.

    You may define and implement other functions to complete this homework. Print out the original string and whether or not it is a palindrome.

  4. Both the StringFind.cc program of Section 4.3.5 and the StringFind.java program of Section 4.4.5 suffer from one major shortcoming that is illustrated by the following example. If we ask the program to replace "ice" by "cream" in the following string,

         ice and icecream are two different things 

    both programs would result in the following string

         cream and creamcream are two different things 

    This happens because the programs blindly replace the substrings without making certain that the substrings are words. Modify those programs so that this does not happen.

  5. Extend the StringFind.cc program of Section 4.3.5 so that it prompts a user for the entry of a line of text, a word all of whose occurrences must be replaced in the text, and a replacement word. The program should then display on the terminal the modified line of text.

  6. Write a C++ program that reads a text file to accomplish the following

    • As it reads each word into the program, it should drop any punctuations and other non-alphanumeric marks sticking to the words in the text file. In particular, it should detect and drop the following marks

           , ; : # . * ! " ) ( > < ] [ \ _ - ? 

      if they are attached to either the beginning or the end of a word. Make sure that if there are multiple occurrences of a mark at either the beginning or the end of a words, your program deletes all of them. That is, the word "!!!great!!!" should get cleaned up to just "great".

    • The program should store each cleaned up word in a vector of strings.

    • Finally, the program should sort the cleaned up words by invoking the generic algorithm sort and write the words in sorted order into an output text file.

    Use the code shown in Chapter 2 for reading from a text file and writing into a text file. Test your program on the following input

         !!!Hello!!!     Where's you??????     You have not been seen in a >>>>long<<<< time.     Will you *****ever***** sur-     face again??????? I can't wait for e-v-e-r!     I have a question: Should "dirty-words" be     stored as two words: ‘dirty' and 'words',     or as one word: dirtywords.     Yours     ** ### !!!! 
  7. Extend the above program by giving special consideration to hyphenations and apostrophes. For hyphenated words, if the hyphenation is at the end of a line, assume that the last word in the line got broken up into two parts. Join the two parts. If the hyphenation is in middle of a line, do not delete it. When you detect an apostrophe, also drop the letter that follows the apostrophe. Hint: Use the peek () function to detect the end of a line.

  8. If you just feed an array of strings to the java.util.Arrays.sort method, it uses the natural order for strings for sorting. This order, which basically amounts to comparing ASCII encodings of the characters, places all the words that begin with uppercase letters before the words that begin with lowercase letters. If you are not happy with this sort result, it is possible to invoke java.util.Arrays.sort with a second argument that is a Comparator object which can tell the sort function to produce its output in the same way you see the words in an English dictionary. The Comparator object must invoke the locale-specific string comparison methods. The locale-specific string comparison methods are defined for the Collator class. The main goal of this homework is to see how the Locale, Collator, and Comparator classes work together to produce a sort that corresponds to English language dictionary order.

    Write a Java program that sorts the words of English in a way that you'd find in an English language dictionary. For the purpose of testing your program, initialize an array of strings within the program with different words.

[25]Based on an example posted at the Java Virtual Machine Forum of the Java Developer Connection.




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