class Lect120 extends Object120 { /** Given how many slices of (say) mushroom pizza are * currently available, create a nice tempting message * to advertise (shout out, or post, or put on a * LED sign...) * * @param topping The type of pizza (e.g. "pepperoni") * @param piecesReady The number of currently ready-to-serve * slices with the given topping. Must be non-negative. * @return A complete sentence describing the inventory. * For example: * */ static String slicesReadyMsg( String flavor, int numSlices ) { String verb; String plurality; // what to place after the noun, to indicate plural or not. if (numSlices == 1) { verb = "is"; if (equals(flavor, "cactus")) { plurality = "us"; } else { plurality = ""; } } else { verb = "are"; if (!equals(flavor, "cactus")) { plurality = "s"; } else { plurality = "i"; } } if (!equals(flavor, "cactus")) { return "There " + verb + " " + numSlices + " piping hot piece" + plurality + " of " + flavor + " pizza, ready to eat!"; } else { return "Woot! " + numSlices + " cact" + plurality + " " + verb + " up!"; } } static void testSlicesReadyMsg() { System.out.println( "Actual: " + slicesReadyMsg( "mushroom", 3 )); System.out.println( "Desire: " + "There are 3 piping hot pieces of mushroom pizza, ready to eat!" ); System.out.println( "Actual: " + slicesReadyMsg( "mushroom", 0 )); System.out.println( "Desire: " + "There are 0 piping hot pieces of mushroom pizza, ready to eat!" ); System.out.println( "Actual: " + slicesReadyMsg( "hawaiian", 0 )); System.out.println( "Desire: " + "There are 0 piping hot pieces of hawaiian pizza, ready to eat!" ); System.out.println( "Actual: " + slicesReadyMsg( "cactus", 17 )); System.out.println( "Desire: " + "Woot! 17 cacti are up!" ); System.out.println( "Actual: " + slicesReadyMsg( "cactus", 1 )); System.out.println( "Desire: " + "Woot! 1 cactus is up!" ); System.out.println( "Actual: " + slicesReadyMsg( "cactus", 0 )); System.out.println( "Desire: " + "Woot! 0 cacti are up!" ); System.out.println( "Actual: " + slicesReadyMsg( "mushroom", 1 )); System.out.println( "Desire: " + "There is 1 piping hot piece of mushroom pizza, ready to eat!" ); } static char lastChar( String wrd ) { return charAt(wrd, length(wrd)-1); } static String callOut( String name ) { return "Hey, that means you, " + name + "."; } // you write `wwngbt` static String wwngbt( String nameToDiss ) { return "We will NEVER get back together. " + callOut(nameToDiss); } // you write `wwngbt` static String wwngbt_obscured( String nameToDiss ) { //return "We will NEVER get back together. " + callOut(toString(lastChar(nameToDiss))); return wwngbt( toString(lastChar(nameToDiss) )); } static void testWwngbt() { System.out.println( "Actual: " + wwngbt("Barland") ); System.out.println( "Desired: " + "We will NEVER get back together. Hey, that means you, Barland." ); System.out.println( "Actual: " + wwngbt("Insane Clown Posse") ); System.out.println( "Desired: " + "We will NEVER get back together. Hey, that means you, Insane Clown Posse." ); System.out.println( "Actual: " + wwngbt("Taylor Swift") ); System.out.println( "Desired: " + "We will NEVER get back together. Hey, that means you, Taylor Swift." ); } static void testWwngbt_obscured() { System.out.println( "Actual: " + wwngbt_obscured("Barland") ); System.out.println( "Desired: " + "We will NEVER get back together. Hey, that means you, d." ); System.out.println( "Actual: " + wwngbt_obscured("Insane Clown Posse") ); System.out.println( "Desired: " + "We will NEVER get back together. Hey, that means you, e." ); System.out.println( "Actual: " + wwngbt_obscured("Taylor Swift") ); System.out.println( "Desired: " + "We will NEVER get back together. Hey, that means you, t." ); } static void testLastChar() { System.out.println( "Actual: " + lastChar("hello") ); System.out.println( "Desire: " + 'o' ); System.out.println( "Actual: " + lastChar("z") ); System.out.println( "Desire: " + 'z' ); System.out.println( "Actual: " + lastChar("huh?") ); System.out.println( "Desire: " + '?' ); } static void testCallOut() { System.out.println( "Actual: " + callOut( "Caleb" ) ); System.out.println( "Desired: " + "Hey, that means you, Caleb." ); System.out.println( "Actual: " + callOut( "Taylor Swift" ) ); System.out.println( "Desired: " + "Hey, that means you, Taylor Swift." ); } static void testAll() { System.out.println( "==== Testing all ====" ); testLastChar(); testCallOut(); testWwngbt(); testWwngbt_obscured(); testMyMax(); testSlicesReadyMsg(); } static double myMax( double x, double y) { if (x >= y) { return x; } else { return y; } } /** * Given a *nonnegative* numCustomers, how much to charge (in dollars)? */ static double buffetPrice( int numCustomers ) { if (numCustomers != 0) { return 8 + 4.5*(numCustomers-1); } else { return 0; } } static void testMyMax() { System.out.println( "Actual: " + myMax( toDouble(17), 17) ); System.out.println( "Desired: " + 17 ); System.out.println( "Actual: " + myMax( Math.pow(Math.PI, Math.E), 64 )); System.out.println( "Desired: " + 64 ); System.out.println( "Actual: " + myMax(3,7) ); System.out.println( "Desired: " + 7 ); System.out.println( "Actual: " + myMax(3.5, 3.6) ); System.out.println( "Desired: " + 3.6 ); System.out.println( "Actual: " + myMax(-8, 2) ); System.out.println( "Desired: " + 2 ); System.out.println( "Actual: " + myMax(-8, -2) ); System.out.println( "Desired: " + -2 ); System.out.println( "Actual: " + myMax(0, -2) ); System.out.println( "Desired: " + 0 ); } static void whileDemo() { String chapterTitle = "An Unexpected Party that is very very long and keeps on going and going"; // Create the string "An Unexpected Party ..................." which is exactly 40 chars long // for use in a table-of-contents ("toc") String tocSoFar = chapterTitle + " "; while (length(tocSoFar) < 40) { tocSoFar = tocSoFar + "."; System.out.println( "tocSoFar is " + length(tocSoFar) + "chars: " + tocSoFar ); } System.out.println(tocSoFar); } /** Return `msg` padded with copies of `padStr` so that the * result is at least `minLength` characters long. * @return `msg` padded with copies of `padStr` so that the result is at least `minLength` characters long. */ static String padRight( String msg, String padStr, int minLength ) { String paddedSoFar = msg; while (length(paddedSoFar) < minLength) { paddedSoFar = paddedSoFar + padStr; } return paddedSoFar; } /** Return `txt` but with each 'e' replaced with '3'. * @return `txt` but with each 'e' replaced with '3'. */ static String leet( String txt ) { String leetTxtSoFar = ""; int nextIndexToCheck = 0; while (nextIndexToCheck < length(txt)) { char nextChar =charAt(txt,nextIndexToCheck); if (nextChar == 'e') { leetTxtSoFar = leetTxtSoFar + '3'; } else { leetTxtSoFar = leetTxtSoFar + nextChar; } nextIndexToCheck = nextIndexToCheck + 1; System.out.println( "debug: At loop-bottom: nextIndexToCheck is " + nextIndexToCheck + " and leetTxtSoFar is " + leetTxtSoFar ); } return leetTxtSoFar; } }