1. Write 4 overloading static methods to construct a String that compose a diamond: public static String diamond (); public static String diamond (int n); public static String diamond (int n, char flash); public static String diamond (int n, char flash, char blank); System.out.print( diamond() ) will output as left below:
System.out.print( diamond( 5 ) ) will output as right above. System.out.print( diamond( 7, ’$’ ) ) will output as left below:
System.out.print( diamond( 6, ’@’, ’.’ ) ) will output as right above.
2
2. Write a static methods that remove all the tabs (\t) in a String and substitute a tab with needed blanks. public static String noTabs (String s, int tabPosition); For example, noTabs( ”\tabcde\t1234\txyz\tABCDEFGHIJKLMN\tOK\n”, 8) will return the String: ”........abcde...1234....xyz.....ABCDEFGHIJKLMN..OK\n”. For clarity, using a . to present a blank. And noTabs( ”\tabcde\t1234\txyz\tABCDEFGHIJKLMN\tOK\n”, 4 ) will return the String: ”....abcde...1234....xyz.ABCDEFGHIJKLMN..OK\n”.
3. (jhtp10, Exercise 5.29) “The Twelve Days of Christmas” Song. Write an application that uses repetition and switch statements to print the song “The Twelve Days of Christmas.” One switch statement should be used to print the day (“first,” “second,” and so on). A separate switch statement should be used to print the remainder of each verse. Visit the website en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_(song) for the lyrics of the song. (12DaysOfChristmas.txt attached)
Typical switch statament is something like:
switch (integrelExpression) { // integerals, String, enum type case c1: // a constant .. break; // might be falling down case c2: .. break; .. case cn: .. break; default: .. break; }
3
12DaysOfChristmas.txt
The Twelve Days of Christmas (12 Days of Christmas) – lyrics
On the first day of Christmas, my true love sent to me A partridge in a pear tree.
On the second day of Christmas, my true love sent to me Two turtle doves, And a partridge in a pear tree.
On the third day of Christmas, my true love sent to me Three French hens, Two turtle doves, And a partridge in a pear tree.
On the fourth day of Christmas, my true love sent to me Four calling birds, Three French hens, Two turtle doves, And a partridge in a pear tree.
On the fifth day of Christmas, my true love sent to me Five golden rings, Four calling birds, Three French hens, Two turtle doves, And a partridge in a pear tree.
On the sixth day of Christmas, my true love sent to me Six geese a-laying, Five golden rings, Four calling birds, Three French hens, Two turtle doves, And a partridge in a pear tree.
On the seventh day of Christmas, my true love sent to me Seven swans a-swimming, Six geese a-laying, Five golden rings, Four calling birds, Three French hens, Two turtle doves, And a partridge in a pear tree.
On the eighth day of Christmas, my true love sent to me Eight maids a-milking, Seven swans a-swimming, Six geese a-laying,
4
Five golden rings, Four calling birds, Three French hens, Two turtle doves, And a partridge in a pear tree.
On the ninth day of Christmas, my true love sent to me Nine ladies dancing, Eight maids a-milking, Seven swans a-swimming, Six geese a-laying, Five golden rings, Four calling birds, Three French hens, Two turtle doves, And a partridge in a pear tree.
On the tenth day of Christmas, my true love sent to me Ten lords a-leaping, Nine ladies dancing, Eight maids a-milking, Seven swans a-swimming, Six geese a-laying, Five golden rings, Four calling birds, Three French hens, Two turtle doves, And a partridge in a pear tree.
On the eleventh day of Christmas, my true love sent to me Eleven pipers piping, Ten lords a-leaping, Nine ladies dancing, Eight maids a-milking, Seven swans a-swimming, Six geese a-laying, Five golden rings, Four calling birds, Three French hens, Two turtle doves, And a partridge in a pear tree.
On the twelfth day of Christmas, my true love sent to me Twelve drummers drumming, Eleven pipers piping, Ten lords a-leaping, Nine ladies dancing, Eight maids a-milking, Seven swans a-swimming, Six geese a-laying, Five golden rings, Four calling birds, Three French hens, Two turtle doves, And a partridge in a pear tree!
The Twelve Days of Christmas (12 Days of Christmas) – lyrics