Can anyone help me with this? here is a file called SimpleSong.java the followin
ID: 3605768 • Letter: C
Question
Can anyone help me with this?
here is a file called SimpleSong.java
the following is the code for SimpleSong.java
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
/**The Song class has three private members: songName, player, and singer
*/
public class SimpleSong {
//FIELD VARIABLES -- note they are all private in order to protect
the data
private String songName;
private Artist artist;
private Player player;
//CONSTRUCTORS
/**Default constructor (Java automatically provides this if no
constructor is typed out)
*/
public SimpleSong(){
super(); //call the constructor of the superclass; Java
automatically does this even if it is not typed out
}
/**Constructor from fields
*/
public SimpleSong(String songName, Artist artist, String file){
this.songName = songName;
this.artist = artist;
this.setPlayer(file); //the constructor can call the
setter methods
}
//METHODS
/**Getters and Setters
*/
public String getSongName() {
return this.songName;
}
public void setSongName(String name) {
songName = name; //if the parameter does not have the
same name as the field variable, the "this" keyword is not needed
}
public Artist getArtist() {
return artist;
}
public void setArtist(Artist artist) {
this.artist = artist; //in this case the "this" keyword
is needed
}
public void setPlayer (String fileName){ //another benefit of a
setter method is that it can offer functionality such as this:
try{ // create a player object from the given
fileName
FileInputStream fis = new
FileInputStream(fileName);
BufferedInputStream bis = new
BufferedInputStream(fis);
this.player = new Player(bis);
}catch(Exception e){
System.err.println("Error accessing the file
"+fileName);
}
}
/**A method to play the song
*/
public void playSong(){
//TODO: Add a print statement that prints the title and
artist name and genre of the song being played
// Hint: Use the "this" keyword and getter methods
new Thread() { //run in new thread to play in background
public void run() {
try {
player.play();
}catch (Exception e) {
System.out.println(e);
}
}
}.start();
}
/**A method to stop playing the song
*/
public void stopSong(){
if (this.player != null){
this.player.close();
}
}
}
-----------------------------
my activity is as followed:
i cant upload my jar file and songs....
but,can you still help me these activities?
import java.util.Scanner;
/**This is the driver class from which you will create and manipulate
* instances of the Song and Singer classes
*/
public class Demo {
public static void main(String args[]){
//Activity 1: Create the Artist class
//Activity 2
//TODO: Create artists for your songs
//TODO: Create a blank SimpleSong, call the setters to
initialize it
//TODO: Create a new song using the constructor from
fields
//Activity 3
//TODO: Prompt the user what song they want to play
//TODO: Play the appropriate song
//TODO: Continuously prompt the user to enter "s" when
they want to stop the song until they enter "s"
//TODO: Call the stopSong method to stop the song
}
}
Explanation / Answer
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
/**The Song class has three private members: songName, player, and singer
*/
public class SimpleSong {
//FIELD VARIABLES -- note they are all private in order to protect
the data
private String songName;
private Artist artist;
private Player player;
//CONSTRUCTORS
/**Default constructor (Java automatically provides this if no
constructor is typed out)
*/
public SimpleSong(){
super(); //call the constructor of the superclass; Java
automatically does this even if it is not typed out
}
/**Constructor from fields
*/
public SimpleSong(String songName, Artist artist, String file){
this.songName = songName;
this.artist = artist;
this.setPlayer(file); //the constructor can call the
setter methods
}
//METHODS
/**Getters and Setters
*/
public String getSongName() {
return this.songName;
}
public void setSongName(String name) {
songName = name; //if the parameter does not have the
same name as the field variable, the "this" keyword is not needed
}
public Artist getArtist() {
return artist;
}
public void setArtist(Artist artist) {
this.artist = artist; //in this case the "this" keyword
is needed
}
public void setPlayer (String fileName){ //another benefit of a
setter method is that it can offer functionality such as this:
try{ // create a player object from the given
fileName
FileInputStream fis = new
FileInputStream(fileName);
BufferedInputStream bis = new
BufferedInputStream(fis);
this.player = new Player(bis);
}catch(Exception e){
System.err.println("Error accessing the file
"+fileName);
}
}
/**A method to play the song
*/
public void playSong(){
//TODO: Add a print statement that prints the title and
artist name and genre of the song being played
// Hint: Use the "this" keyword and getter methods
new Thread() { //run in new thread to play in background
public void run() {
try {
player.play();
}catch (Exception e) {
System.out.println(e);
}
}
}.start();
}
/**A method to stop playing the song
*/
public void stopSong(){
if (this.player != null){
this.player.close();
}
}
}
-----------------------------
my activity is as followed:
i cant upload my jar file and songs....
but,can you still help me these activities?
import java.util.Scanner;
/**This is the driver class from which you will create and manipulate
* instances of the Song and Singer classes
*/
public class Demo {
public static void main(String args[]){
//Activity 1: Create the Artist class
//Activity 2
//TODO: Create artists for your songs
Artist artist1 = new Artist("yanny", "pop" , "29");
SimpleSong song1 = new SimpleSong("Beautiful world", artist1, "C:/programfiles/a1.txt");
System.out.print(song1.getArtist().toString());
//TODO: Create a blank SimpleSong, call the setters to
initialize it
SimpleSong song = new SimpleSong();
song.SetSongname("Beautiful world");
song.SetArtist(artist1);
song.SetPlayer("C:/programfiles/a1.txt");
//TODO: Create a new song using the constructor from
fields
Artist artist2 = new Artist("nelly", "blues" , "40");
SimpleSong song2 = new SimpleSong(" humming bird", artist2, "C:/programfiles/a2.txt");
//Activity 3
System.out.println("Please enter the song to be played");
Scanner in = new Scanner(System.in);
String userChoice = in.nextLine();
if(userChoice == "hummingbird") {
song2.playSong().start();
for(;;){
System.out.println(" Press s to stop the song");
Scanner checkStop = new Scanner(System.in);
String keypress = checkStop.nextLine();
if(keypress == "s"){
song2.stopSong();
}else{
continue;
}
}
}
else {
System.out.println("The song you entered is not in the list")
}
}
}
public class Artist {
private String artistname;
private String artistgenre;
private String artistAge;
Artist(String artistname, String artistgenre,String artistAge){
this.artistname = artistname;
this.artistgenre = artistgenre;
this.artistAge = artistAge;
}
public String getName(){
return artistname;
}
public String getGenre(){
return artistgenre;
}
public String getAge(){
return artistAge;
}
public String toString(){
return artistname +" : " + artistgenre + " : " + artistage + ":";
}
}