Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Lyrics { public static void main ( String [ ] args ) { singDuckSong ( ) ; } private static void singDuckSong (

public class Lyrics {
public static void main(String[] args){
singDuckSong();
}
private static void singDuckSong(){
for (int i =5; i >=1; i--){
singVerse(i);
printQuacks(i);
}
singFinalVerse();
printQuacks(0);
}
private static void singVerse(int numDucks){
printDucksHeading(numDucks);
printOverTheHillAndFarAway();
printMotherDuckSays();
printDucksReturned(numDucks);
}
private static void singFinalVerse(){
System.out.println("Sad mother duck went out one day,");
printOverTheHillAndFarAway();
printMotherDuckSays();
System.out.println("And all of the five little ducks came back.");
System.out.println(); // Empty line
}
private static void printDucksHeading(int numDucks){
String numberWord = convertToWords(numDucks);
String ducksWord =(numDucks ==1)? "duck" : "ducks";
System.out.println(numberWord +" little "+ ducksWord +" went out one day,");
}
private static void printOverTheHillAndFarAway(){
System.out.println("over the hill and far away.");
}
private static void printMotherDuckSays(){
System.out.println("Mother duck said, \"Quack, quack, quack, quack.\"");
}
private static void printDucksReturned(int numDucks){
if (numDucks ==1){
System.out.println("But none of the five little ducks came back.");
} else {
System.out.println("But only "+ convertToWords(numDucks -1).toLowerCase()+" little ducks came back.");
}
}
private static void printQuacks(int numDucks){
System.out.println("Quack, quack, quack, quack, quack, quack.");
}
private static String convertToWords(int number){
switch (number){
case 1:
return "Five";
case 2:
return "Four";
case 3:
return "Three";
case 4:
return "Two";
case 5:
return "One";
default:
return "none";
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David Kroenke

11th Edition

0132302675, 9780132302678

More Books

Students also viewed these Databases questions

Question

Did you print a proof to view color and image consistency?

Answered: 1 week ago