{{announcement.body}}
{{announcement.title}}

Road to Great Code, Challenge #1 — Shuffle Playlist

DZone 's Guide to

Road to Great Code, Challenge #1 — Shuffle Playlist

Hone your craft with this Java coding challenge!

· Java Zone ·
Free Resource

Hone your craft with this Java coding challenge!

No matter what your profession is, it’s important to hone your craft. Uncle Bob (Rob Martin) in his book titled The Clean Coder suggests that us programmers should regularly practice writing code and solve common problems. Once we get good at solving them, he goes as far as recommending that we program to the rhythm of the music. Not to mention that we shouldn’t only stick to one programming language that we’ve mastered. We should constantly expose ourselves to different languages to get a better understanding of the programming universe.

You may also like: Clean Code Principles

By doing this consistently, you'll be able to instantly make a decision about which programming language is the most suitable to solve a problem at hand.

Road to Great Code will, therefore, be a series of programming challenges to help you work on your craft by tackling a different programming challenge once a week. 

The Problem

It’s October 2001 and Apple is releasing the first iPod, which will revolutionize the portable media player market. Your job is to implement the song shuffle feature. Using your favorite programming language (Java), you need to implement this feature. 

Naturally, you could use the Collections.shuffle method; however, that wouldn’t help us improve all that much, so let's implement the shuffle method from scratch. 

Here’s the starting point that contains a library of sorted songs. 

Java




x
26


 
1
  public static void main(String[] args) {
2
        List<Song> library = new ArrayList<>() {
3
            {
4
                add(new Song("Basket Case"));
5
                add(new Song("Blood On The Leaves"));
6
                add(new Song("Creep"));
7
                add(new Song("Cut Your Hair"));
8
                add(new Song("Don't Look Back in Anger"));
9
                add(new Song("Fields Of Gold"));
10
                add(new Song("Juicy"));
11
                add(new Song("Mysterious Ways"));
12
                add(new Song("Rocket Man"));
13
                add(new Song("Smells Like Teen Spirit"));
14
                add(new Song("Video Games"));
15
                add(new Song("Wannabe"));
16
            }
17
        };
18
 
          
19
        myShuffle(library);
20
        
21
        library.forEach(System.out::println);
22
    }
23
 
          
24
    private static void myShuffle(List<Song> songs) {
25
        // type solution here
26
    }


The Task

Your job is to implement the myShuffle method and then print out the shuffled library. 

A Solution

This is an example solution that I came up with. Compare it with your own and let me know your observations. 

Java




xxxxxxxxxx
1
16


 
1
private static void myShuffle(List<Song> songs) {
2
        Random random = new Random();
3
 
          
4
        for (int index = 0; index < songs.size(); index++) {
5
            int secondIndex = random.nextInt(songs.size());
6
            swap(index, secondIndex, songs);
7
        }
8
    }
9
 
          
10
    private static void swap(int firstIndex, int secondIndex, List<Song> songs) {
11
        System.out.println(String.format("Replacing %s with %s", firstIndex, secondIndex));
12
        Song firstSong = songs.get(firstIndex);
13
        Song secondSong = songs.get(secondIndex);
14
        songs.set(secondIndex, firstSong);
15
        songs.set(firstIndex, secondSong);
16
    }



Best of luck, and we can't wait to see what you come up with!

Further Reading

Clean Code Principles

7 Tips to Write Better Java Code You Should Know

Clean Architecture Is Screaming
Topics:
challenge, code, craft, exercise, great code, java, road to great code, uncle bob

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}