+ 16
Music Player
You are building a Music Player app. You need to implement the MusicPlayer class, which should hold the track names as Strings in an array. The array is already defined in the given code. The player should support the following functions: add: add the given argument track to the tracks array. show: output all track names in the player on separate lines. play: start playing the first track by outputting "Playing name" where name is the first track name. You can add a new item to an array using +=, for example: tracks += track https://code.sololearn.com/ck9NHmjfhHog/?ref=app
17 ответов
+ 12
Try this, the code is up and running
class MusicPlayer {
private var songs: Array<String> = arrayOf()
//your code goes here
fun add(track: String): Array<String>{
songs = songs + track
return songs
}
fun show(){
for(track in songs.indices){
println(songs[track])
}
}
fun play(){
println("Playing "+songs[0])
}
}
fun main(args: Array<String>) {
val m = MusicPlayer()
while(true) {
var input = readLine()!!
if(input == "stop") {
break
}
m.add(input)
}
m.show()
m.play()
}
+ 10
class Track:
def __init__(self, title, next):
self.title = title
self.next = next
class Player:
def __init__(self):
self.head = None
def add(self, title):
if not self.head:
self.head = Track(title, None)
return
curr = self.head
while curr.next:
curr = curr.next
curr.next = Track(title, None)
p = Player()
d = []
while True:
x = input()
if x == 'end':
break
p.add(x)
d.append(x)
for i in d:
print (i)
+ 6
class MusicPlayer {
private var songs: Array<String> = arrayOf()
//your code goes here
fun add(track: String): Array<String>{
songs = songs + track
return songs
}
fun show(){
for(track in songs.indices){
println(songs[track])
}
}
fun play(){
println("Playing "+songs[0])
}
}
fun main(args: Array<String>) {
val m = MusicPlayer()
while(true) {
var input = readLine()!!
if(input == "stop") {
break
}
m.add(input)
}
m.show()
m.play()
}
+ 4
class MusicPlayer {
private var songs: Array<String> = arrayOf()
//your code goes here
fun add(song:String):Array<String> {
songs += song
return songs
}
fun show() {
for (song in songs) {
println(song)
}
}
fun play() {
println ("Playing " + songs[0])
}
}
fun main(args: Array<String>) {
val m = MusicPlayer()
while(true) {
var input = readLine()!!.toString()
if(input == "stop") {
break
}
m.add(input)
}
m.show()
m.play()
}
+ 1
Dave Enyi Thank You
+ 1
🙏
+ 1
class Track:
def __init__(self, title, next):
self.title = title
self.next = next
class Player:
def __init__(self):
self.head = None
def add(self, title):
if not self.head:
self.head = Track(title, None)
return
curr = self.head
while curr.next:
curr = curr.next
curr.next = Track(title, None)
p = Player()
d = []
while True:
x = input()
if x == 'end':
break
p.add(x)
d.append(x)
for i in d:
print (i)
0
Thank you i have learnt something on you.
0
Copy this code correct answer and like thank you
0
Alessio Pustorino !! The :Array part after the "fun add" was what I had missed. Thank you.
Do you happen to know why it is needed?
0
class MusicPlayer {
private var songs: Array<String> = arrayOf()
//your code goes here
fun add(input: String){
songs += input
}
fun show(){
songs.forEach{
println(it)
}
}
fun play(){
println("Playing "+songs[0])
}
}
fun main(args: Array<String>) {
val m = MusicPlayer()
while(true) {
var input = readLine()!!
if(input == "stop") {
break
}
m.add(input)
}
m.show()
m.play()
}
0
class MusicPlayer {
private var songs: Array<String> = arrayOf()
//your code goes here
fun add(track: String): Array<String>{
songs = songs + track
return songs
}
fun show(){
for(track in songs.indices){
println(songs[track])
}
}
fun play(){
println("Playing "+songs[0])
}
}
fun main(args: Array<String>) {
val m = MusicPlayer()
while(true) {
var input = readLine()!!
if(input == "stop") {
break
}
m.add(input)
}
m.show()
m.play()
}
0
class Track:
def __init__(self, title, next):
self.title = title
self.next = next
class Player:
def __init__(self):
self.head = None
def add(self, title):
if not self.head:
self.head = Track(title, None)
return
curr = self.head
while curr.next:
curr = curr.next
curr.next = Track(title, None)
p = Player()
d = []
while True:
x = input()
if x == 'end':
break
p.add(x)
d.append(x)
for i in d:
print (i)
0
All are wrong answer, Try mine
class MusicPlayer {
private var tracks: Array<String> = arrayOf()
fun add(track: String) {
//your code goes here
tracks += track
}
fun show() {
//your code goes here
for (track in tracks){
println(track)
}
}
fun play() {
//your code goes here
println("Playing ${tracks[0]}")
}
}
fun main(args: Array<String>) {
val m = MusicPlayer()
while(true) {
var input = readLine()!!
if(input == "stop") {
break
}
m.add(input)
}
m.show()
m.play()
}
0
class Track:
def __init__(self, title, next):
self.title = title
self.next = next
class Player:
def __init__(self):
self.head = None
def add(self, title):
if not self.head:
self.head = Track(title, None)
return
curr = self.head
while curr.next:
curr = curr.next
curr.next = Track(title, None)
p =Player()
while True:
x =input()
if x == 'end':
break
p.add(x)
n = p.head
while n!= None:
print(n.title)
n = n.next
# it may be work
0
class MusicPlayer:
def __init__(self):
# Initialize an empty array to store track names
self.tracks = []
# Function to add a new track
MusicPlayerc(https://sites.google.com/site/legitticketsites/viagogo-es-seguro)
def add(self, track):
self.tracks += [track] # Add the track to the array
# Function to show all tracks
def show(self):
for track in self.tracks:
print(track) # Output each track on a separate line
# Function to play the first track
def play(self):
if self.tracks: # Check if there are any tracks
print(f"Playing {self.tracks[0]}") # Output the first track
else:
print("No tracks to play") # Handle empty track list
0
This is a solid approach to creating a Music Player app! Implementing the MusicPlayer class with functions to add, show, and play tracks will make it user-friendly and functional. For more insights into building comprehensive music apps, check out this guide: https://www.cleveroad.com/blog/how-to-make-a-music-app-like-pandora--step-by-step-guide-with-detailed-estimation/.