patternjavaMajor
Print the Twelve Days of Christmas without loops or conditionals
Viewed 0 times
withouttheconditionalschristmasloopsprinttwelvedays
Problem
For my AP Computer Science class, I am required to write a program that prints the Twelve Days of Christmas without loops or conditionals using static methods.
```
public class TwelveDaysOfChristmas {
public static final String[] lines = new String[] {
"A partridge in a pear tree",
"Two turtle doves and",
"Three French Hens,",
"Four calling birds",
"Five golden rings.",
"Six geese a-laying,",
"Seven swans a-swimming,",
"Eight maids a-milking,",
"Nine ladies dancing,",
"Ten lords a-leaping,",
"Eleven Pipers piping,",
"Twelve drummers drumming,"
};
public static final String[] days = new String[] {
"first",
"second",
"third",
"fourth",
"fifth",
"sixth",
"seventh",
"eighth",
"ninth",
"tenth",
"eleventh",
"twelfth"
};
public static final String verseText =
"On the %s day of Christmas,\n" +
"My true love sent to me\n" +
"%s";
public static void main(String[] args) {
//Print the song
System.out.println(song());
}
public static String song() {
//The song consists of all verses up to verse 12, but the index starts from 0
//Print verses 0 to 11
return versesBelow(11);
}
public static String versesBelow(int verse) {
//Evil hack that allows me to check whether the input is zero without using a conditional ;)
try {
//Divide by verse. If the verse is zero, then an ArithmeticException is thrown.
//I am not using a conditional here!!!
int i = 1 / verse;
}
catch(ArithmeticException ex) {
//Stop the recursion
return verse(verse);
}
//else return this verse and all the verses below this one.
return versesBelow(verse - 1) + verse(verse);
}
public static String vers
```
public class TwelveDaysOfChristmas {
public static final String[] lines = new String[] {
"A partridge in a pear tree",
"Two turtle doves and",
"Three French Hens,",
"Four calling birds",
"Five golden rings.",
"Six geese a-laying,",
"Seven swans a-swimming,",
"Eight maids a-milking,",
"Nine ladies dancing,",
"Ten lords a-leaping,",
"Eleven Pipers piping,",
"Twelve drummers drumming,"
};
public static final String[] days = new String[] {
"first",
"second",
"third",
"fourth",
"fifth",
"sixth",
"seventh",
"eighth",
"ninth",
"tenth",
"eleventh",
"twelfth"
};
public static final String verseText =
"On the %s day of Christmas,\n" +
"My true love sent to me\n" +
"%s";
public static void main(String[] args) {
//Print the song
System.out.println(song());
}
public static String song() {
//The song consists of all verses up to verse 12, but the index starts from 0
//Print verses 0 to 11
return versesBelow(11);
}
public static String versesBelow(int verse) {
//Evil hack that allows me to check whether the input is zero without using a conditional ;)
try {
//Divide by verse. If the verse is zero, then an ArithmeticException is thrown.
//I am not using a conditional here!!!
int i = 1 / verse;
}
catch(ArithmeticException ex) {
//Stop the recursion
return verse(verse);
}
//else return this verse and all the verses below this one.
return versesBelow(verse - 1) + verse(verse);
}
public static String vers
Solution
Recursion is something that is often "unwrapped" to become loops, but the same is true, in the other direction. Almost all loops can be implemented as recursion.
Using your "trick" of throwing an exception to act as a conditional, it would be simple to turn all your code... this code:
in to just:
you can see it running here: http://ideone.com/SAte6Z
Written properly, it would be:
Using your "trick" of throwing an exception to act as a conditional, it would be simple to turn all your code... this code:
public static final String verseText =
"On the %s day of Christmas,\n" +
"My true love sent to me\n" +
"%s";
public static void main(String[] args) {
//Print the song
System.out.println(song());
}
public static String song() {
//The song consists of all verses up to verse 12, but the index starts from 0
//Print verses 0 to 11
return versesBelow(11);
}
public static String versesBelow(int verse) {
//Evil hack that allows me to check whether the input is zero without using a conditional ;)
try {
//Divide by verse. If the verse is zero, then an ArithmeticException is thrown.
//I am not using a conditional here!!!
int i = 1 / verse;
}
catch(ArithmeticException ex) {
//Stop the recursion
return verse(verse);
}
//else return this verse and all the verses below this one.
return versesBelow(verse - 1) + verse(verse);
}
public static String verse(int verse) {
//Format the string with the correct day and verse and add a newline for a blank space after
return String.format(verseText, days[verse], linesBelow(verse)) + '\n';
}
public static String linesBelow(int line) {
//Same evil hack
try {
int i = 1 / line;
}
catch(ArithmeticException ex) {
//Stop the recursion if line is zero
return line(line);
}
//Else return this line and all the lines below this line
return line(line) + linesBelow(line - 1);
}
public static String line(int line) {
//Return this line and a newline
return lines[line] + '\n';
}in to just:
public static void main(String[] args) {
try {
recurseDown(0);
} catch (RuntimeException e) {
// nothing.
}
}
private static void recurseDown(int i) {
System.out.println("\nOn the " + days[i] + " day of Christmas\nMy true love sent to me");
try {
recurseUp(i);
} catch (RuntimeException e) {
// nothing.
}
recurseDown(i + 1);
}
private static void recurseUp(int i) {
System.out.println(lines[i]);
recurseUp(i - 1);
}you can see it running here: http://ideone.com/SAte6Z
Written properly, it would be:
public static void main(String[] args) {
recurseDown(0);
}
private static void recurseDown(int i) {
if (i == days.length) {
return;
}
System.out.println("\nOn the " + days[i] + " day of Christmas\nMy true love sent to me");
recurseUp(i);
recurseDown(i + 1);
}
private static void recurseUp(int i) {
if (i < 0) {
return;
}
System.out.println(lines[i]);
recurseUp(i - 1);
}Code Snippets
public static final String verseText =
"On the %s day of Christmas,\n" +
"My true love sent to me\n" +
"%s";
public static void main(String[] args) {
//Print the song
System.out.println(song());
}
public static String song() {
//The song consists of all verses up to verse 12, but the index starts from 0
//Print verses 0 to 11
return versesBelow(11);
}
public static String versesBelow(int verse) {
//Evil hack that allows me to check whether the input is zero without using a conditional ;)
try {
//Divide by verse. If the verse is zero, then an ArithmeticException is thrown.
//I am not using a conditional here!!!
int i = 1 / verse;
}
catch(ArithmeticException ex) {
//Stop the recursion
return verse(verse);
}
//else return this verse and all the verses below this one.
return versesBelow(verse - 1) + verse(verse);
}
public static String verse(int verse) {
//Format the string with the correct day and verse and add a newline for a blank space after
return String.format(verseText, days[verse], linesBelow(verse)) + '\n';
}
public static String linesBelow(int line) {
//Same evil hack
try {
int i = 1 / line;
}
catch(ArithmeticException ex) {
//Stop the recursion if line is zero
return line(line);
}
//Else return this line and all the lines below this line
return line(line) + linesBelow(line - 1);
}
public static String line(int line) {
//Return this line and a newline
return lines[line] + '\n';
}public static void main(String[] args) {
try {
recurseDown(0);
} catch (RuntimeException e) {
// nothing.
}
}
private static void recurseDown(int i) {
System.out.println("\nOn the " + days[i] + " day of Christmas\nMy true love sent to me");
try {
recurseUp(i);
} catch (RuntimeException e) {
// nothing.
}
recurseDown(i + 1);
}
private static void recurseUp(int i) {
System.out.println(lines[i]);
recurseUp(i - 1);
}public static void main(String[] args) {
recurseDown(0);
}
private static void recurseDown(int i) {
if (i == days.length) {
return;
}
System.out.println("\nOn the " + days[i] + " day of Christmas\nMy true love sent to me");
recurseUp(i);
recurseDown(i + 1);
}
private static void recurseUp(int i) {
if (i < 0) {
return;
}
System.out.println(lines[i]);
recurseUp(i - 1);
}Context
StackExchange Code Review Q#113328, answer score: 22
Revisions (0)
No revisions yet.