+ 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
15 ответов
+ 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