|
Question 5.1: Study the following code and then compile and run it to verify that it prints out the lyrics to a popular beer-drinking song:
class BeerSong begin beginMain System.out.println("Five bottles of beer on the wall."); System.out.println("Five bottles of beer on the wall."); System.out.println("If one bottle of beer should accidentally fall,"); System.out.println("there'd be four bottles of beer on the wall."); System.out.println(); System.out.println("Four bottles of beer on the wall."); System.out.println("Four bottles of beer on the wall."); System.out.println("If one bottle of beer should accidentally fall,"); System.out.println("there'd be three bottles of beer on the wall."); System.out.println(); System.out.println("Three bottles of beer on the wall."); System.out.println("Three bottles of beer on the wall."); System.out.println("If one bottle of beer should accidentally fall,"); System.out.println("there'd be two bottles of beer on the wall."); System.out.println(); System.out.println("Two bottles of beer on the wall."); System.out.println("Two bottles of beer on the wall."); System.out.println("If one bottle of beer should accidentally fall,"); System.out.println("There'd be one bottle of beer on the wall."); System.out.println(); System.out.println("One bottle of beer on the wall."); System.out.println("One bottle of beer on the wall."); System.out.println("If one bottle of beer should accidentally fall,"); System.out.println("there'd be no bottles of beer on the wall."); System.out.println(); endMain end
Question 5.2: The following is the first attempt to make the code smaller but to keep the same output: If you compile and run the following code you will notice that it counts up from one rather than down from n. Change the for loop so that it runs down rather than up. For information about how to write the for loop, please consult Tutorial 2.
class BeerSong begin function song(int n) begin for (var int i=1; i<=n; i=i+1) begin System.out.println(i + " bottles of beer on the wall"); System.out.println(i + " bottles of beer on the wall"); System.out.println("If one bottle of beer should accidentally fall,"); System.out.println("there'd be " + (i-1) + " bottles of beer on the wall"); System.out.println(); end end beginMain song(5); endMain end
Question 5.3: Finish the number2string function below and add a new function call to this function in the song function so that it print textual numbers rather than digits.
function String number2string(int n) begin assert n>=0 : n; assert n<=10: n; if (n == 0) then return "no"; if (n == 1) then return "one"; if (n == 2) then return "two"; /* rest of code goes here */ if (n == 9) then return "nine"; if (n == 10) then return "ten"; assert false; end
Question 5.4: Add a new function String capitalize(int n) that capitalizes the first word in a String and call this function from the song function so that the first words in each sentence are capitalized. You should find the function Character.toUpperCase and the methods String and String helpful for writing this function. See the String class of the java.lang package in the following link:
for more details.
Question 5.5: Add new function call String plural(int n) that returns the string "s" if n is not equal to 1 and the empty string "" otherwise. Then call this function from the song function so that the phrase "bottle" is pluralized when it should be.
Question 5.6: Write a function called number2string2 that can handle values up to but not including 100. Note that you will need multiple if statements to achive this. Note that if n is a number then the following expressions are useful:
Also make it print out "one hundred or more" in the case that n>=100
Question 5.7: Change the song function so that the following function call: song(5,"rum"); in the main function results in the following printout:
Five bottles of rum on the wall.
...
there'd be no bottles of rum on the wall.
Question 5.8: Once all the code is working, add the following line to the main function: song(100,"gin"); so that it prints out the following:
One hundred bottles of gin on the wall.
...
there'd be zero bottles of gin on the wall.
Question 5.9 Write a new function number2string3 that works like number2string2 and number2string except that it handles numbers up to 999. Internally number2string3 should call number2string2. You might find the following function useful:
function String textand(String a, String b) begin if (a.equals("") or b.equals("")) then return a + b; else return a + " and " + b; end
Question 5.10 † Tricky Write a new function number2string4 that works like number2string3 execpt that it handles numbers up to nine hundred and ninety-nine million nine hundred and ninety-nine thousand nine hundred and ninety-nine, i.e. 999,999,999. The function number2string4 should internally call number2string3 like so:
Note that the variables above will have values from 0 to 999 inclusive.
Back to J.T.W |
This page has the following hit count:
|