Java Reference
In-Depth Information
The String class provides methods for joining, searching, and modifying strings - the
modifications being achieved by creating a new String object.
A StringBuffer object can store a string of characters that can be modified.
You can get the number of characters stored in a StringBuffer object by calling its
length() method, and you can find out the current maximum number of characters it can
store by using its capacity() method.
You can change both the length and the capacity for a StringBuffer object.
The StringBuffer class contains a variety of methods for modifying StringBuffer objects.
You can create a String object from a StringBuffer object by using the toString()
method of the StringBuffer object.
Exercises
1.
Create an array of String variables and initialize the array with the names of the months
from January to December. Create an array containing 12 random decimal values between 0.0
and 100.0. Display the names of each month along with the corresponding decimal value.
Calculate and display the average of the 12 decimal values.
2.
Write a program to create a rectangular array containing a multiplication table from 1x1 up to
12x12. Output the table as 13 columns with the numeric values right aligned in columns. (The
first line of output will be the column headings, the first column with no heading, then the
numbers 1 to 12 for the remaining columns. The first item in each of the succeeding lines is
the row heading, which ranges from 1 to 12.)
3.
Write a program that sets up a String variable containing a paragraph of text of your choice.
Extract the words from the text and sort them into alphabetical order. Display the sorted list
of words. You could use a simple sorting method called the bubble sort. To sort an array into
ascending order the process is as follows:
Starting with the first element in the array compare successive elements (0 and 1, 1 and 2, 2
and 3, and so on).
If the first element of any pair is greater than the second, interchange the two elements.
Repeat the process for the whole array until no interchanges are necessary. The array
elements will now be in ascending order.
4.
Define an array of ten String elements each containing an arbitrary string of the form
" month/day/year ", for example," 10/29/99 " or " 12/5/01 ". Analyze each element in the
array and output the date represented in the form 29th October 1999.
5.
Write a program that will reverse the sequence of letters in each word of your chosen
paragraph from Exercise 3. For instance, " To be or not to be. " would become
" oT eb ro ton ot eb. "
Search WWH ::




Custom Search