Java Reference
In-Depth Information
E6. Write a function that, given a name like "Gries, Paul Christian" puts it in this
form: "Paul Christian Gries".
E7. Write a function that produces the English word for a digit 0 , 1 , ..., or 9 .
E8. Write a function that, given an integer in the range 20..99 , produces the
English word for it, e.g. for 30 , produce "thirty" and for 42 , produce "forty
two" . Use the function of the previous exercise.
E9. Write a function that, given a String s that contains a time like "1:15PM"
or "11:00AM" , produces a String that has the same time but in 24-hour format,
e.g. "13:15" or "11:00" . You don't need loops.
E10. Write a function that changes a date exemplified by the string "November
3, 2003" into this form: "3 November 2003" .
The following exercises require loops and should be done using StringBuffer s.
The result of each function is a String , but the value to be returned should be
first calculated as a StringBuffer and then converted to a String .
E11. Write a function that, given an integer n ( ≥0 ), produces a String that con-
tains the integers in the range 0..n , separated by commas. For example, for n =
2 , produce a String that contains "0, 1, 2" . Do the calculation using a
StringBuffer .
E12. Write a function that, given an integer n ( ≥0 ) and a String s , produces a
String that contains n copies of s , one after the other. Do the calculation using
a StringBuffer .
E13 . Write a function that removes all blanks from a String .
E14. Write a function that duplicates each character of a String . For example,
for the "abc" , produce the "aabbcc" . Build the result in a StringBuffer .
5.3
Class Vector
An instance of class Vector (in package java.util ) contains a list of objects. It
has several simple ways to refer to the objects and change them. For example, an
instance could contain the email addresses of your friends, information about
your compact disks, or information about students at Cornell University.
An instance of Vector is shown in Fig. 5.3. It contains three objects, num-
bered 0 , 1 , and 2 , and it has room for two more (at the moment). Its size —the
number of objects it contains— is 3, and its capacity —the number of objects it
(currently) can contain— is 5. The instance contains many methods, which we
do not show in the object. We do show functions size and capacity , which give
the size and capacity of the Vector .
Two objects in this Vector are strings, and one is an Integer . Objects in a
Vector can have any class, but they cannot be primitive values.
Lesson
page 5-5
Search WWH ::




Custom Search