Homework: Review II
Here are some review questions to give you extra practice with
what you have learned so far and help you prepare for the first
midterm exam.
- (This is modified from Programming Exercise P4.3 at
the end of Chapter 4 of Java for Everyone..) Assume you have the following
variable declarations: Write program fragments
(i.e., you do not need to write complete programs) that read a
line of input as a String and print:
- Only the uppercase letters in the String
- Every second letter of the String
- The String with all vowels replaced by an
underscore
- The number of vowels in the String
- The positions of all vowels in the String
- (This is modified from Review Exercise R6.3 at the
end of Chapter 6 of Java for Everyone.) Consider the following array: What
are the contents of the array a after the following loops
complete?
- (This is modified from Review Exercise R6.5 at the
end of Chapter 6 of Java for Everyone.) Write Java code for a loop that
simultaneously computes both the maximum and minimum of an array
of ints called a.
- Write Java code for a loop that sets boolean
variable isOrdered to true if the elements
of a given array of ints called a are in
non-decreasing order, otherwise it sets isOrdered to false.
- Consider the following XML document (adapted from here): Answer the following questions.
- Draw the complete XMLTree corresponding to
this document.
- Assume you have an XMLTree variable called menu
initialized with the value of the XML document above. What is
the value returned by each of the following calls?
- Modify the document so that the price is an attribute
of the name tag.
- Redraw the complete XMLTree corresponding to
the new document.
Additional Questions
- Implement the following static method that, given an XMLTree,
menu, in the format of the breakfast_menu
above and a SimpleWriter, out, outputs a list of
all the menu items, one per line, with their name and price. Note
that you cannot assume the given XMLTree will have the
same number of food entries as the example above; only that it
will have the same format.
- Modify the method implementation so that it also prints the
number of calories for each item.