9.4. Overriding Methods

 
[Page 297]

Programming Exercises

Section 8.2 The String Class

8.1* ( Revising Listing 8.1 CheckPalindrome.java ) Rewrite Listing 8.1 by creating a new string that is a reversal of the string and compare the two to determine whether the string is a palindrome . Write your own reverse method using the following header:
   public static   String reverse(String s) 

8.2* ( Revising Listing 8.1 CheckPalindrome.java ) Rewrite Listing 8.1 to ignore cases.
8.3** ( Checking substrings ) You can check whether a string is a substring of another string by using the indexOf method in the String class. Write your own method for this function. Write a program that prompts the user to enter two strings, and check whether the first string is a substring of the second.
8.4* ( Occurrences of a specified character ) Write a method that finds the number of occurrences of a specified character in the string using the following header:
   public static int   count(String str,   char   a) 

For example, count("Welcome", 'e') returns 2 .

Section 8.3 The Character Class

8.5** ( Occurrences of each digit in a string ) Write a method that counts the occurrences of each digit in a string using the following header:
   public static int   [] count(String s) 

The method counts how many times a digit appears in the string. The return value is an array of ten elements, each of which holds the count for a digit. For example, after executing int[] counts = count("12203AB3") , counts[0] is 1 , counts[1] is 1 , counts[2] is 2 , counts[3] is 2 .

Write a main method to display the count for "SSN is 343 32 4545 and ID is 434 34 4323" .

8.6* ( Counting the letters in a string ) Write a method that counts the number of letters in the string using the following header:
   public static int   countLetters(String s) 

Write a main method to invoke countLetters("Java in 2008") and display its return value.

8.7* ( Hex to decimal ) Write a method that parses a hex number as a string into a decimal integer. The method header is as follows :
   public static int   parseHex(String hexString) 

For example, hexString A5 is 165 (10 x 16 + 5 = 165) and FAA is 4129 (15 x 16 2 + 10 x 16 + 10 = 4129). So parseHex("A5") returns 165 , and parseHex("FAA") returns 4129 . Use hex strings ABC and 10A to test the method. Note that Integer.parseInt("FAA", 16) parses a hex string to a decimal value. Do not use this method in this exercise.


[Page 298]
8.8* ( Binary to decimal ) Write a method that parses a binary number as a string into a decimal integer. The method header is as follows:
  public static int  parseBinary(String binaryString) 

For example, binaryString 10001 is 17 (1 x 2 4 + 0 x 2 3 + 0 x 2 2 + 0 x 2 + 1 = 17) So, parseBinary("10001") returns 17 . Use binary string 11111111 to test the method. Note that Integer.parseInt("10001", 2) parses a binary string to a decimal value. Do not use this method in this exercise.

Section 8.4 The StringBuilder / StringBuffer Class

8.9** ( Decimal to hex ) Write a method that parses a decimal number into a hex number as a string. The method header is as follows:
   public static   String convertDecimalToHex(   int   value) 

See §1.5, "Number Systems," for converting a decimal into a hex. Use decimal 298 and 9123 to test the method.

8.10** ( Decimal to binary ) Write a method that parses a decimal number into a binary number as a string. The method header is as follows:
   public static   String convertDecimalToBinary(   int   value) 

See §1.5, "Number Systems," for converting a decimal into a binary. Use decimal 298 and 9123 to test the method.

8.11** ( Sorting characters in a string ) Write a method that returns a sorted string using the following header:
   public static   String sort(String s) 

For example, sort("acb") returns abc .

8.12** ( Anagrams ) Write a method that checks whether two words are anagrams. Two words are anagrams if they contain the same letters in any order. For example, "silent" and "listen" are anagrams. The header of the method is as follows:
   public static boolean   isAnagram(String s1, String s2) 

Write a main method to invoke isAnagram("silent", "listen") , isAnagram("garden", "ranged") , and isAnagram("split", "lisp") .

Section 8.5 Command-Line Arguments

8.13* ( Passing a string to check palindromes ) Rewrite Example 8.1, "Checking Palindromes," by passing the string as a command-line argument.
8.14* ( Summing integers ) Write two programs. The first program passes an unspecified number of integers as separate strings to the main method and displays their total. The second program passes an unspecified number of integers in one string to the main method and displays their total. Name the two programs Exercise8_14a and Exercise8_14b , as shown in Figure 8.18.
[Page 299]
Figure 8.18. The program adds all the numbers passed from the command line.


8.15* ( Finding the number of uppercase letters in a string ) Write a program that passes a string to the main method and displays the number of uppercase letters in a string.

Section 8.6 Regular Expressions

8.16* ( Monetary units ) Rewrite Listing 2.7, ComputeChange.java, to receive the input as a string, and extract the dollars and cents using the split method in the String class.
8.17* ( Extracting words ) Write a program that extracts words from a string using the spaces and punctuation marks as delimiters. Enter the string from an input dialog box.
8.18* ( Summing values ) Write a program that reads a string from an input dialog box. The string consists of double values separated by spaces. Display the sum of the values.

Sections 8.7 “8.8

8.19** ( Reformatting Java source code ) Write a program that converts the Java source code from the next-line brace style to the end-of-line brace style. For example, the following Java source in (a) uses the next -line brace style. Your program converts it to the end-of-line brace style in (b).

Your program can be invoked from the command line with the Java source code file as the argument. It converts the Java source code to a new format. For example, the following command converts the Java source code file Test.java to the end-of-line brace style.

 java Exercise8_19 Test.java 

8.20* ( Counting characters, words, and lines in a file ) Write a program that will count the number of characters (excluding control characters '\r' and '\n' ), words, and lines, in a file. Words are separated by spaces, tabs, carriage -returns, or line-feed characters. The filename should be passed as a command-line argument, as shown in Figure 8.19.
[Page 300]
Figure 8.19. The program displays the number of characters, words, and lines in a given file.


8.21* ( Processing scores in a text file ) Suppose that a text file named Exercise8_21.txt contains an unspecified number of scores. Write a program that reads the scores from the file and displays their total and average. Scores are separated by blanks.
8.22* ( Writing/Reading data ) Write a program to create a file named Exercise8_22.txt if it does not exist. Write one hundred integers created randomly into the file using text I/O. Integers are separated by spaces in the file. Read the data back from the file and display the sorted data.
8.23** ( Replacing text) Listing 8.8, ReplaceText.java, gives a program that replaces text in a source file and saves the change into a new file. Revise the program to save the change into the original file. For example, invoking
 java Exercise8_23 file oldString newString 

replaces oldString in the source file with newString .

Hint

Read each line from the file, replace all the occurrences of oldString with newString , and append it into a string builder. Finally write the string builder to the original file.


8.24** ( Removing text ) Write a program that removes all the occurrences of a specified string from a text file. For example, invoking
 java Exercise8_24 John filename 

removes string John from the specified file.

Hint

Read each line from the file, remove all the occurrences of the text in the line, and append the line into a string builder. Finally write the string builder to the source file.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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