In: Computer Science
JAVA
In the last lesson, we wrote a class with methods to print out the song The Ants Go Marching. Notice that this is a class where there are no instance variables and we don’t really need to generate multiple objects. With students or pets, it makes sense to have multiple objects. With the Song, we can just make the methods static and have just 1 copy of them.
Copy in your class from the last lesson into this active code window. Change the method(s) that print out the verses of the Song to be static. In the main method, change how you call the static methods by using just the classname instead of creating an object.
Add a static variable to the class that keeps track of the number of verses. Increment this variable in the method verse and print it out at the beginning of the verse.
public class Song
{
// Add a static verse counter variable
// Change the method(s) to be static
public static void main(String args[])
{
// Call the static method(s) to print out the Song
}
}
I have added the static count and verse methods in the Song
Class
PLEASE FIND THE FOLLOWING CODE SCREENSHOT, OUTPUT, AND CODE.
ANY CLARIFICATIONS/MODIFICATIONS REQUIRED LEAVE A COMMENT
1.CODE SCREENSHOT:
2.OUTPUT:
3.CODE:
public class Song
{
// Add a static verse counter variable
public static int verseCount=0;
// Change the method(s) to be static
public static void verse(String s[],int i){
verseCount++;
System.out.println("Verse :"+verseCount);
System.out.println(s[i]);
}
public static void main(String args[])
{
String lyric[]={"The ants go marching one by one, hurrah, hurrah\nThe ants go marching one by one, hurrah, hurrah\nThe ants go marching one by one\nThe little one stops to suck his thumb\nAnd they all go marching down to the ground\nTo get out of the rain, boom! Boom! Boom!\n\n",
"The ants go marching two by two, hurrah, hurrah\nThe ants go marching two by two, hurrah, hurrah \nThe ants go marching two by two\nThe little one stops to tie his shoe\nAnd they all go marching down to the ground\nTo get out of the rain, boom! Boom! Boom!\n\n",
"The ants go marching three by three, hurrah, hurrah\nThe ants go marching three by three, hurrah, hurrah\nThe ants go marching three by three\nThe little one stops to climb a tree\nAnd they all go marching down to the ground\nTo get out of the rain, boom! Boom! Boom!\n\n",
"The ants go marching four by four, hurrah, hurrah\nThe ants go marching four by four, hurrah, hurrah\nThe ants go marching four by four\nThe little one stops to shut the door\nAnd they all go marching down to the ground\nTo get out of the rain, boom!\n\n",
"The ants go marching five by five, hurrah, hurrah\nThe ants go marching five by five, hurrah, hurrah\nThe ants go marching five by five\nThe little one stops to take a dive\nAnd they all go marching down to the ground\nTo get out of the rain, boom! Boom! Boom!\n\n",
"The ants go marching six by six, hurrah, hurrah\nThe ants go marching six by six, hurrah, hurrah\nThe ants go marching six by six \nThe little one stops to pick up sticks\nAnd they all go marching down to the ground\nTo get out of the rain, boom! Boom! Boom!\n\n",
"The ants go marching seven by seven, hurrah, hurrah\nThe ants go marching seven by seven, hurrah, hurrah\nThe ants go marching seven by seven\nThe little one stops to pray to heaven\nAnd they all go marching down to the ground\nTo get out of the rain, boom!\n\n",
"The ants go marching eight by eight, hurrah, hurrah\nThe ants go marching eight by eight, hurrah, hurrah\nThe ants go marching eight by eight\nThe little one stops to shut the gate\nAnd they all go marching down to the ground\nTo get out of the rain, boom! Boom! Boom!\n\n",
"The ants go marching nine by nine, hurrah, hurrah\nThe ants go marching nine by nine, hurrah, hurrah\nThe ants go marching nine by nine\nThe little one stops to get out the line\nAnd they all go marching down to the ground\nTo get out of the rain, boom! Boom! Boom!\n\n",
"The ants go marching ten by ten, hurrah, hurrah\nThe ants go marching ten by ten, hurrah, hurrah\nThe ants go marching ten by ten \nThe little one stops to do it again\nAnd they all go marching down to the ground\nTo get out of the rain, boom! Boom! Boom! Boom!"
};
for(int i=0;i<lyric.length;i++)
verse(lyric,i);
}
}