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:SimpleReader in = new SimpleReader1L(); SimpleWriter out = new SimpleWriter1L();
- 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?int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 };
-
int i = 1; while (i < 10) { a[i] = a[i - 1]; i++; }
-
int i = 9; while (i > 0) { a[i] = a[i - 1]; i--; }
-
int i = 0; while (i < 9) { a[i] = a[i + 1]; i++; }
-
int i = 8; while (i >= 0) { a[i] = a[i + 1]; i--; }
-
int i = 1; while (i < 10) { a[i] = a[i] + a[i - 1]; i++; }
-
int i = 1; while (i < 10) { a[i] = 0; i = i + 2; }
-
int i = 0; while (i < 5) { a[i + 5] = a[i]; i++; }
-
int i = 1; while (i < 5) { a[i] = a[9 - i]; i++; }
-
- (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.<breakfast_menu> <food calories="650"> <name>Belgian Waffles</name> <price>$5.95</price> </food> <food calories="630"> <name>Blueberry Pancakes</name> <price>$4.95</price> </food> <food calories="600"> <name>French Toast</name> <price>$4.50</price> </food> <food calories="950"> <name>Homestyle Breakfast</name> <price>$6.95</price> </food> </breakfast_menu>
- 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?
menu.isTag()
menu.label()
menu.numberOfChildren()
menu.hasAttribute("calories")
menu.child(1)
menu.child(1).isTag()
menu.child(1).label()
menu.child(1).numberOfChildren()
menu.child(1).hasAttribute("calories")
menu.child(1).attributeValue("calories")
menu.child(1).child(0).isTag()
menu.child(1).child(0).child(0).isTag()
menu.child(1).child(0).child(0).label()
- 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.
/** * Output the name and price for each food item in the given breakfast_menu * {@code XMLTree}. * * @param menu * the {@code XMLTree} corresponding to a breakfast_menu XML document * @param out * the output stream * @requires <pre> * [menu is in the format of a breakfast_menu XML document] and out.is_open * </pre> * @ensures <pre> * [output the name and price of all food items in the given {@code XMLTree}] * </pre> */ public static void printMenu(XMLTree menu, SimpleWriter out) {...}
- Modify the method implementation so that it also prints the number of calories for each item.