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.

  1. (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:
    1. Only the uppercase letters in the String
    2. Every second letter of the String
    3. The String with all vowels replaced by an underscore
    4. The number of vowels in the String
    5. The positions of all vowels in the String
  2. (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?
  3. (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.
  4. 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.
  5. Consider the following XML document (adapted from here): Answer the following questions.
    1. Draw the complete XMLTree corresponding to this document.
    2. 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?
    3. Modify the document so that the price is an attribute of the name tag.
    4. Redraw the complete XMLTree corresponding to the new document.

Additional Questions

  1. 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.
  2. Modify the method implementation so that it also prints the number of calories for each item.