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

Java: We are going to create an array of playlist that hold songs. 1. Create a c

ID: 3716274 • 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

package com.test;


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Song {
String title;
String artist;
int length;
public Song(String title,String artist,int length){
this.title = title;
this.artist = artist;
this.length = length;
}

public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public static void main(String[] args) {
List<Song> song = new ArrayList<Song>();
Scanner sc = new Scanner(System.in);
int playListLength=0;


System.out.println("Please Enter Song Title");
String title = sc.next();
System.out.println("Please Enter the Artist");
String artist = sc.next();
System.out.println("Please Enter the Length");
int length = sc.nextInt();
Song song2 = new Song(title, artist, length);
song.add(song2);
  
for(Song s : song){
playListLength=playListLength+s.getLength();
}
String numberToWord = convert(playListLength);
System.out.println("Playlist Length : "+numberToWord);


}
private static final String[] specialNames = {
"",
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion"
};
  
private static final String[] tensNames = {
"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};
  
private static final String[] numNames = {
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
};
  
private static String convertLessThanOneThousand(int number) {
String current;
  
if (number % 100 < 20){
current = numNames[number % 100];
number /= 100;
}
else {
current = numNames[number % 10];
number /= 10;
  
current = tensNames[number % 10] + current;
number /= 10;
}
if (number == 0) return current;
return numNames[number] + " hundred" + current;
}
  
public static String convert(int number) {
if (number == 0) { return "zero"; }
  
String prefix = "";
  
if (number < 0) {
number = -number;
prefix = "negative";
}
  
String current = "";
int place = 0;
  
do {
int n = number % 1000;
if (n != 0){
String s = convertLessThanOneThousand(n);
current = s + specialNames[place] + current;
}
place++;
number /= 1000;
} while (number > 0);
  
  
return (prefix + current).trim();
}



}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote