/* * * * * CS 112 * * Lab Practicum * * A class to output the twolve days of christmas * * Christine Papadaks * * Fall 2025 * * * */ public class TwelveDays { private static final String[] days = { "first", "second", "third", "Fourth", "fifth" , "sixth", "seventh", "eighth", "nineth", "tenth", "eleventh", "twelvthe" }; private static final String[] gifts = { "Partridge in a Pear Tree!", "Turtle Doves", "French Hens", "Colly Birds", "Gold Rings", "Geese-A-Laying" , "Swans A Swimming", "Maids a Milking", "Ladies Dancing", "Lords a Leaping", "Pipers Piping", "Drummers Drumming" }; private static final String message = "On the day of Christmas, my true love sent to me..."; public static int totalNumTimes = 0; int numTimes; // no-arg construtor used to setup default values public TwelveDays() { numTimes = 1; // set the default to 1 but could also be 0 } // Custome constructor, note the constructor chaining public TwelveDays( int n ) { // Should check for n > 0 and throw an exception or call the no arg constrcutor this(); if (n > 0) numTimes = n; // else can throw an exception } // Good to show them how they can decompose the logic into logical method blocks public void sing() { // You want to update static variable in this method in this case, and not in the constuctors totalNumTimes += numTimes; for (int i = 0; i < numTimes; i++ ) outputTwelveDays(); } // Good to show them use of private method public void outputTwelveDays() { for ( int d = 1; d <= 12; d++ ) { System.out.println( message.substring(0,7) + days[d-1] + message.substring( 6 ) ); for ( int g = d; g >= 1; g-- ) { if ( g == 1 ) if ( d > 1 ) System.out.print( "and a " ); else System.out.print( "A " ); else System.out.print( g + " " ); System.out.println( gifts[g-1] ); } System.out.println( "\n" ); } } // Note the use of statuc in this method public static int getTotal() { return( totalNumTimes ); } // Simple test driver public static void main( String[] arg ) { TwelveDays tdays = new TwelveDays(2); tdays.sing(); tdays = new TwelveDays(3); tdays.sing(); System.out.println( "\nTotal times sung is: " + TwelveDays.getTotal() ); } }