Cracking the coding interview, 4 edition 150 programming interview questions and solutions

Page 147

Solutions to Chapter 7 | Object Oriented Design 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

private CDPlayer cdPlayer; private User user; private Set<CD> cdCollection; private TrackSelector ts; public JukeBox(CDPlayer cdPlayer, User user, Set<CD> cdCollection, TrackSelector ts) { ... } public Song getCurrentTrack() { return ts.getCurrentSong(); } public void processOneUser(User u) { this.user = u; } } public class Playlist { private Song track; private Queue<Song> queue; public Playlist(Song track, Queue<Song> queue) { ... } public Song getNextTrackToPlay(){ return queue.peek(); } public void queueUpTrack(Song s){ queue.add(s); } } public class Song { private String songName; } public class TrackSelector { private Song currentSong; public TrackSelector(Song s) { currentSong=s; } public void setTrack(Song s) { currentSong = s; } public Song getCurrentSong() { return currentSong; } } public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public long getID() { return ID; } public void setID(long iD) { ID = iD; } private long ID; public User(String name, long iD) { ... } public User getUser() { return this; } public static User addUser(String name, long iD) { ... } }

155

Cracking the Coding Interview | Concepts and Algorithms


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.