Question

In: Computer Science

JAVA In the last lesson, we wrote a class with methods to print out the song...

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.

  1. 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.

  2. 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

}
}

Solutions

Expert Solution

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);
}
}

Related Solutions

Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song titles by classifying them according to genre (e.g., Pop, Rock, etc.). The class uses a HashMap to map a genre with a set of songs that belong to such a genre. The set of songs will be represented using a HashSet. Your driver output should sufficiently prove that your code properly implements the code below. public class SongsDatabase { private Map<String, Set<String>> genreMap =...
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
Create a generic method to print objects in java. Include a tester class.
Create a generic method to print objects in java. Include a tester class.
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for the following: 1. Name the class SalonServices 2. Add private data fields: a. salonServiceDescription – This field is a String type b. price - This field is a double type 3. Create two methods that will set the field values. a. The first method setSalonServiceDescription() accepts a String parameter defined as service and assigns it to the salonServiceDescription. The method is not static b....
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int m_numElements; // constructor // Do not make any changes to this method! public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! boolean isEmpty() { if (m_last == null) return true; else return false; } // return the size of the list (# of Song nodes) // Do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT