Question
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
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 dont 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 public static variable called numVerses 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.
code from last lesson:
public class Song { public void verse(String num, String line) { System.out.println("The ants go marching " + num + " by " + num + ", hurrah, hurrah"); System.out.println("The ants go marching " + num + " by " + num + ", hurrah, hurrah"); System.out.println("The ants go marching " + num + " by " + num); System.out.println("The little one stops to " + line); System.out.println("To get out of the rain, boom! Boom! Boom!"); }
// The chorus method public void chorus() { System.out.println("And they all go marching down to the ground"); }
public static void main(String args[]) { // Create a Song object and call its method(s) to print out // the verses of The Ants Go Marching // There should be atleast 1 method called verse that takes 2 arguments. Song mySong = new Song(); mySong.verse("one", "suck his thumb"); mySong.chorus(); mySong.verse("two", "tie his shoe"); mySong.chorus(); mySong.verse("three", "climb a tree"); mySong.chorus(); mySong.verse("four", "shut the door"); mySong.chorus(); mySong.verse("five", "take a dive"); mySong.chorus(); mySong.verse("six", "pick up sticks"); mySong.chorus(); mySong.verse("seven", "pray to heaven"); mySong.chorus(); mySong.verse("eight", "shut the gate"); mySong.chorus(); mySong.verse("nine", "get out the line"); mySong.chorus(); mySong.verse("ten", "do it again"); mySong.chorus();
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started