Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment must use the runnable interface to implements threads that repre

ID: 3866263 • Letter: T

Question

This assignment must use the runnable interface to implements threads that represent singers. The singer’s singing will be simulated by displaying the each line of the songs lyrics.

The project must have the following components:

package threadRunnableInterfacePackage

SingersClass.java

SongLyricsClass.java

ThreadRunnableInterfaceMainClass.java

In the SingersClass.java:

Implement the run() method that represents each singer. In this method design a loop that the singer uses to sing the song.

Create a class for the song lyrics.

SongLyricsClass.java: Have a Sting array that has the song’s lyrics. Have an accessor synchronized get method to get each line of the song array by requesting the line no. Have an accessor get method to get the total number of lines in the song.

You must include a couple of stanzas of your favorite song lyrics that you choose.

ThreadRunnableInterfaceMainClass.java: Use spate 6 singers to test the program. In a loop, have each singer start singing. Display a message when the loop is done. Display a message when the main program is done. For a reference solution to help you with the program development, see the jpeg files.

This assignment must use the runnable interface to implements threads that represent singers. The singer's singing will be simulated by displaying the each line of the songs lyrics. The project must have the following components: package threadRunnablelnterfacePackage SingersClass.java SongLyricsClass.java ThreadRunnableInterfaceMainClass.java In the Sin gersClass.java: Implement the run) method that represents each singer In this method design a loop that the singer uses to sing the song. Create a class for the song lyrics. SongLyricsClass.java Have a Sting array that has the song's lyrics. Have an accessor synchronized get method to get each line of the song array by requesting the line no. Have an accessor get method to get the total number of lines in the song. You must include a couple of stanzas of your favorite song lyrics that you choose ThreadRunnableInterfaceMainClass.java: Use spate 6 singers to test the program. In a loop, have each singer start singing Display a message when the loop is done. Display a message when the main p rogram is done For a reference solution to help you with the program development, see the jpeg files.

Explanation / Answer

//SingerClass.java

package threadRunnableInterfacePackage;

/***

* singer class have run method

*

*/

public class SingerClass implements Runnable {

//run method

@Override

public void run() {

SongLyricsClass obj = new SongLyricsClass();

for(int i=0;i<obj.getNoOfLine();i++){

System.out.println(obj.getLyrics(i));

}

//after exiting the loop printing the message

System.out.printf("%s Done ",Thread.currentThread().getName());

}

}

//SongLyricsClass.java

package threadRunnableInterfacePackage;

public class SongLyricsClass {

//declared the synchronized method

public String lyrics[][]=new String[2][1];

//synchronized method

//return the lyrics by line no

public synchronized String getLyrics(int lineNo){

//song lyrics

lyrics[0][0] = "When I was young, they told me, they said";

lyrics[1][0]="Make your bed, you lie in that bed";

return lyrics[lineNo][0];

}

//get line method

public int getNoOfLine(){

return lyrics.length;

}

}

//ThreadRunnableInterfaceMainClass.java

package threadRunnableInterfacePackage;

public class ThreadRunnableInterfaceMainClass {

public static void main(String[] args) {

//created 6 method of singer class

SingerClass singer1 = new SingerClass();

SingerClass singer2 = new SingerClass();

SingerClass singer3 = new SingerClass();

SingerClass singer4 = new SingerClass();

SingerClass singer5 = new SingerClass();

SingerClass singer6 = new SingerClass();

//created six threads and passed the parameter

//of above singer class

//and starting the thread

Thread t1 = new Thread(singer1,"Singer 1");

t1.start();

Thread t2 = new Thread(singer2,"Singer 2");

t2.start();

Thread t3 = new Thread(singer3,"Singer 3");

t3.start();

Thread t4 = new Thread(singer4,"Singer 4");

t4.start();

Thread t5 = new Thread(singer5,"Singer 5");

t5.start();

Thread t6 = new Thread(singer6,"Singer 6");

t6.start();

//waiting to complete above threads

try{

Thread.sleep(3000);

}

catch(InterruptedException e){

System.out.println(e.getMessage());

}

//printing the message after

System.out.println("Program Done ");

}

}