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:
SimpleReader in = new SimpleReader1L(); SimpleWriter out = new SimpleWriter1L();Write program fragments (i.e., you do not need to write complete programs) that read a line of input as a
Stringand print:- Only the uppercase letters in the
String - Every second letter of the
String - The
Stringwith all vowels replaced by an underscore - The number of vowels in the
String - The positions of all vowels in the
String
- Only the uppercase letters in the
-
(This is modified from Review Exercise R6.3 at the end of Chapter 6 of Java for Everyone.) Consider the following array:
int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 };What are the contents of the array
aafter the following loops complete?-
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
intcalleda. -
Write Java code for a loop that sets
booleanvariable isOrdered totrueif the elements of a given array ofintcalledaare in non-decreasing order, otherwise it sets isOrdered tofalse. -
Consider the following XML document (adapted from here):
<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>Answer the following questions.
-
Draw the complete
XMLTreecorresponding to this document. -
Assume you have an
XMLTreevariable 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
nametag. -
Redraw the complete
XMLTreecorresponding to the new document.
-
Additional Questions
-
Implement the following static method that, given an
XMLTree, menu, in the format of thebreakfast_menuabove and aSimpleWriter, out, outputs a list of all the menu items, one per line, with their name and price. Note that you cannot assume the givenXMLTreewill 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.