must be in java: Problem 2: We are going to create an array of playlist that hol
ID: 3718968 • Letter: M
Question
must be in java:
Problem 2: We are going to create an array of playlist that hold songs.
Create a class named Song.
Create 3 instance variables, title, artist and length;
Create a constructor which initializes the instance variables. It will have 3 arguments.
Create 3 methods.
Create a getTitle method which should return a title.
Create a getArtist method which should return an artist.
Create a getLength method which should return a length.
Create a call named SongTester.
Import Scanner;
Create a Scanner object.
Create an array of type Song. It should hold 5 songs and be called playlist.
In a loop:
Ask the user to enter the name of the song and assign it to name.
Ask the user to enter the artist of the song and assign it to artist.
Ask the user to enter the length of the song and assign it to length.
Create a Song object using the user input.
Add the object to playlist.
Create a variable named playListLength.
Create a second loop
Get the length of each song in the playlist array
Add the length to playListLength.
Print the length of the playlist using words to describe the output.
Explanation / Answer
Code:-
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Song { //main class named Song
String title;
String artist;
int length;
public Song(String title,String artist,int length){ //song method
this.title = title;
this.artist = artist;
this.length = length;
}
public String getTitle() { //getTitle method to return title
return title;
}
public void setTitle(String title) { //setTitle method to return title
this.title = title;
}
public String getArtist() { //getArtist method to return title
return artist;
}
public void setArtist(String artist) { //setArtist method to return title
this.artist = artist;
}
public int getLength() { //getLength method
return length;
}
public void setLength(int length) { //setLength method
this.length = length;
}
public static void main(String[] args) { //this is the main method
List<Song> song = new ArrayList<Song>();
Scanner sc = new Scanner(System.in); //scanner to take input
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();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.