Java: We are going to create an array of playlist that hold songs. 1. Create a c
ID: 3716402 • Letter: J
Question
Java: We are going to create an array of playlist that hold songs.
1. Create a class named Song.
2. Create 3 instance variables, title, artist and length;
3. Create a constructor which initializes the instance variables. It will have 3 arguments.
4. Create 3 methods.
a. Create a getTitle method which should return a title.
b. Create a getArtist method which should return an artist.
c. Create a getLength method which should return a length.
5. Create a call named SongTester.
6. Import Scanner;
7. Create a Scanner object.
8. Create an array of type Song. It should hold 5 songs and be called playlist.
9. In a loop:
a. Ask the user to enter the name of the song and assign it to name.
b. Ask the user to enter the artist of the song and assign it to artist.
c. Ask the user to enter the length of the song and assign it to length.
d. Create a Song object using the user input.
e. Add the object to playlist.
10. Create a variable named playListLength.
11. Create a second loop a. Get the length of each song in the playlist array b. Add the length to playListLength.
12. Print the length of the playlist using words to describe the output.
Explanation / Answer
public class Song {
String title;
String artist;
int length;
/**
* @param title
* @param artist
* @param length
*/
public Song(String title, String artist, int length) {
super();
this.title = title;
this.artist = artist;
this.length = length;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @return the artist
*/
public String getArtist() {
return artist;
}
/**
* @return the length
*/
public int getLength() {
return length;
}
}
//-----------------------------------------------
import java.util.Scanner;
public class SongTester {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner console = new Scanner(System.in);
Song[] playlist = new Song[5];
for(int i=0;i<5;i++){
System.out.print("Enter title of the song: ");
String title = console.next();
System.out.print("Enter artist of the song: ");
String artist = console.next();
System.out.print("Enter length of the song: ");
int length = console.nextInt();
playlist[i] = new Song(title, artist, length);
}
int playListLength = 0;
for(Song s:playlist){
playListLength+=s.getLength();
}
System.out.println("Length of the play list is: " + playListLength);
}
}
//------------------------------------------------
output
Enter title of the song: S1
Enter artist of the song: A1
Enter length of the song: 10
Enter title of the song: S2
Enter artist of the song: A2
Enter length of the song: 12
Enter title of the song: S3
Enter artist of the song: A3
Enter length of the song: 3
Enter title of the song: S4
Enter artist of the song: A4
Enter length of the song: 14
Enter title of the song: S5
Enter artist of the song: A5
Enter length of the song: 4
Length of the play list is: 43
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.