Tuesday, September 24, 2013

Reading Flat files seperated by | symbol

Often I have seen that there are files having lines seperated by a de-limiter |.
I thought of making a class that could easily iterate over each of the line one by one..

This class PipedString represents a string which has elements seperated by a | symbol.
This is an iterable class

Create a class PipedString as follows


Now create a Flat File Reader using this PipedString


How to use this FlatFileReader class



Saturday, September 14, 2013

A generic problem solver from State to Goal

I thought of making a generic problem solver, which could solve any problem which has an initial state and a final state and a set of possible moves.

Though my program is not perfect, but still it is able to give some learning and if you guys have suggestions to make it better, please feel free to write in comments.

I have tried to use Template Design pattern here.. Where an algorithm is implemented in the abstract class with some parts left out to be implemented by the user.

Create a State interface



Now implement this abstract class and create a custom class that defines the state of your problem.
Please make sure that you override all the methods given in the interface very correctly.
Do make member variables that can signify your state.
For example, let me take an example of 15-squred number puzzle

Implement the abstract class and create SlidingState class


Create a SolveProblem abstract class as follows

SolveProblem Abstract class


Now extend the SolveProblem class and override the abstract method to give your implementation as follows
And use the methods to solve your problem:

Extend SolveProblem class and use

Tuesday, September 10, 2013

Real time examples of Design patterns being used by Java

I am very keen to learn what all design patterns are being used by java code.

So here I am going to list them one by one.. I'll keep on updating this article as and when I learn more about design patterns...

To begin with.. Lets learn something about strategy design pattern.

Strategy Design Pattern Examples

Sunday, September 8, 2013

The water jug problem

We have three water jugs, and each can hold 3oz., 5oz., and 8oz. of water, respectively.
Without the possibility of water spilling when poured from one jug to another, and given that the jugs have no calibration, how do we divide the 8oz. of water equally among two jugs?


We will define a class named State holding the capacity of A and B jars.
It should be noted that only 2 jars are sufficient to define a state, as water held in third jar can be calculated by subtracting the sum of two from the total.

Define class State like this...

Depth First Search Algorithm

Breadth First Search algorithm...