Room for improvement? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Room for improvement?

Doing the following program for a class. Any redundancy that could be improved upon? Thank anyone who answers in advance. public class SixDays { public static void main(String[] args) { dayOne(); pearTree(); dayTwo(); turtleDoves(); pearTree(); dayThree(); frenchHens(); turtleDoves(); pearTree(); dayFour(); callingBirds(); frenchHens(); turtleDoves(); pearTree(); dayFive(); goldenRings(); callingBirds(); frenchHens(); turtleDoves(); pearTree(); daySix(); geeseLaying(); goldenRings(); callingBirds(); frenchHens(); turtleDoves(); pearTree(); } public static void dayOne() { System.out.println("On the 1st day of Christmas, my true love sent to me"); } public static void dayTwo() { System.out.println("On the 2nd day of Christmas, my true love sent to me"); } public static void dayThree() { System.out.println("On the 3rd day of Christmas, my true love sent to me"); } public static void dayFour() { System.out.println("On the 4th day of Christmas, my true love sent to me"); } public static void dayFive() { System.out.println("On the 5th day of Christmas, my true love sent to me"); } public static void daySix() { System.out.println("On the 6th day of Christmas, my true love sent to me"); } public static void pearTree() { System.out.println("a partridge in a pear tree"); } public static void turtleDoves() { System.out.println("two turtle doves, and"); } public static void frenchHens() { System.out.println("three French hens,"); } public static void callingBirds() { System.out.println("four calling birds,"); } public static void goldenRings() {

25th Oct 2017, 7:57 PM
Jesse Erazo
Jesse Erazo - avatar
1 Answer
+ 1
You should put this into a loop, and have a switch statement on the inside, without any breaks. The input would be what day you are currently on, and from top to bottom of the switch statement, you would have the last method (geeseLaying(), if six) to the first method (pearTree()). This way, you call on specifically the one that needs to be run in regards to the day, but it then falls through the rest of the methods below it, as there is no break statement. As to the day methods, you could also use a switch statement, but implement breaks, as to keep it from doing, say, dayOne() on Day One, and dayTwo() and dayOne() on Day Two.
25th Oct 2017, 8:27 PM
Quantallax