Sunday, May 24, 2015

CodingBat Java > Recursion-1 > strCount

Given a string and a non-empty substring sub, compute recursively the number of times that sub appears in the string, without the sub strings overlapping. 

strCount("catcowcat", "cat") → 2

strCount("catcowcat", "cow") → 1
strCount("catcowcat", "dog") → 0

Expected
Run
strCount("catcowcat", "cat") → 2
2
OK
strCount("catcowcat", "cow") → 1
1
OK
strCount("catcowcat", "dog") → 0
0
OK
strCount("cacatcowcat", "cat") → 2
2
OK
strCount("xyx", "x") → 2
2
OK
strCount("iiiijj", "i") → 4
4
OK
strCount("iiiijj", "ii") → 2
2
OK
strCount("iiiijj", "iii") → 1
1
OK
strCount("iiiijj", "j") → 2
2
OK
strCount("iiiijj", "jj") → 1
1
OK
strCount("aaabababab", "ab") → 4
4
OK
strCount("aaabababab", "aa") → 1
1
OK
strCount("aaabababab", "a") → 6
6
OK
strCount("aaabababab", "b") → 4
4
OK
other tests
OK




          public int strCount(String str, String sub) {

              int index = str.indexOf(sub);

              if (index < 0)
                     return 0;

              return 1 + strCount(str.substring(index + sub.length()), sub);
       }

Sunday, May 3, 2015

Strategy Design Pattern | Java Example



Definition

  • defines a family of algorithms,
  • encapsulates each algorithm, and
  • makes the algorithms interchangeable within that family

When to Use?

  • To choose an appropriate algorithm/behavior at run time when there are multiple algorithms/behaviors executing the same task/operation.


UML



Real World Scenario


As depicted in the UML diagram below, we have an application that plays audio files. And we have many options to play the audio files: VLC Media Player, Window Media Player, KMPlayer. Now, the client or the main program will choose which media player to play the audio files at run time.


Strategy Design Pattern
Figure: Strategy Design Pattern

Code Sample


MainProgram.java
import java.io.File;

public class MainProgram {

       public static void main(String[] args) {

             File mediaFile = new File("audio.mp3");

             MediaPlayer player = new MediaPlayer(new VLCMediaPlayer());

             // MediaPlayer player = new MediaPlayer(new WindowMediaPlayer());
             // MediaPlayer player = new MediaPlayer(new KMPlayer());

             player.play(mediaFile);
       }
}



MediaPlayer.java
import java.io.File;

public class MediaPlayer {

       private IPlayer player;

       public MediaPlayer(IPlayer player) {

             this.player = player;
       }

       public void play(File mediaFile) {

             player.play(mediaFile);
       }
}


IPlayer.java

import java.io.File;

public interface IPlayer {

       public void play(File mediaFile);

}

VLCMediaPlayer.java

import java.io.File;

public class VLCMediaPlayer implements IPlayer {

       @Override
       public void play(File file) {

             System.out.println("Playing via. VLC Media Player");
       }
}

WindowMediaPlayer.java

import java.io.File;

public class WindowMediaPlayer implements IPlayer {

       @Override
       public void play(File file) {

             System.out.println("Playing via. Window Media Player");
       }
}


KMPlayer.java

import java.io.File;

public class KMPlayer implements IPlayer {

       @Override
       public void play(File file) {

             System.out.println("Playing via. KMPLayer");
       }
}