Title: Sequences Solution
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Sample Solution to the 2 Letter Sequences Problem

Solution using a List data structure(Note you can use an ArrayList or a Linked List here)


import java.util.*;

class Data
{
    String twoChars;
    int count = 1;
    Data (String str)
    {
        twoChars = str;
    }
    public String toString()
    {
        return twoChars + " : " + count;
    }
}


class Sequences
{
    List list = new ArrayList();

    public String toString()
    {
        String lineSeparator =
            System.getProperty("line.separator");
        StringBuffer sb = new StringBuffer();
        int len = list.size();
        Data d=null;

       Iterator iter = list.iterator();
       while (iter.hasNext())
       {
            d =(Data)iter.next();
            sb.append(d + lineSeparator);
        }
        return sb.toString();
    }

    Data check(String tryStr)
    {
       int len = list.size();
       Data d=null;

       Iterator iter = list.iterator();
       while (iter.hasNext())
       {
            d =(Data)iter.next();
            if (d.twoChars.equals(tryStr))
                return d;
       }
       return null;
    }

    void fill(int iterations)
    {
        for (int i =0; i < iterations; i++)
        {
            String tryStr = generate();
            Data d = check(tryStr);
            if (d == null)
            {
                d = new Data(tryStr);
                list.add(d);
            }
            else
                d.count+=1;

        }
    }
    String generate()
    {
        char c1 = (char)('a' + (int)(26*Math.random()));
        char c2 = (char)('a' + (int)(26*Math.random()));
        StringBuffer sb= new StringBuffer();
        sb.append(c1);
        sb.append(c2);
        return sb.toString();
    }

    public static void main(String[] args)
    {
        Sequences seq = new Sequences();

        if (args.length == 0)
        {
            System.out.println("Need number of iterations");
            return;
        }

        try
        {
            long time = System.currentTimeMillis();
            int iterations = Integer.parseInt(args[0]);
            seq.fill(iterations);
            System.out.println(seq);
            System.out.println("Delta time = "+
               (System.currentTimeMillis() -time) );
        }
        catch (Exception e)
        {
            System.out.println("Bad Number"+ e);
        }
    }


}


Solution using a HashMap data structure:


import java.util.*;

class Data
{
    String twoChars;
    int count = 1;
    Data (String str)
    {
        twoChars = str;
    }
    public String toString()
    {
        return twoChars + " : " + count;
    }
}


class SequencesHashMap
{
    HashMap list = new HashMap();

    public String toString()
    {
        String lineSeparator =
            System.getProperty("line.separator");
        StringBuffer sb = new StringBuffer();
        int len = list.size();
        Data d=null;

       Set keys = list.keySet();
       Iterator iter = keys.iterator();
       while (iter.hasNext())
       {
            String key =(String)iter.next();
            d = (Data)list.get(key);
            sb.append(d + lineSeparator);
        }
        return sb.toString();
    }

    Data check(String tryStr)
    {
       return (Data)list.get(tryStr);
    }

    void fill(int iterations)
    {
        for (int i =0; i < iterations; i++)
        {
            String tryStr = generate();
            Data d = check(tryStr);
            if (d == null)
            {
                d = new Data(tryStr);
                list.put(tryStr,d);
            }
            else
                d.count+=1;

        }
    }
    String generate()
    {
        char c1 = (char)('a' + (int)(26*Math.random()));
        char c2 = (char)('a' + (int)(26*Math.random()));
        StringBuffer sb= new StringBuffer();
        sb.append(c1);
        sb.append(c2);
        return sb.toString();
    }

    public static void main(String[] args)
    {
        SequencesHashMap seq = new SequencesHashMap();

        if (args.length == 0)
        {
            System.out.println("Need number of iterations");
            return;
        }

        try
        {
            long time = System.currentTimeMillis();
            int iterations = Integer.parseInt(args[0]);
            seq.fill(iterations);
            System.out.println(seq);
            System.out.println("Delta time = "+
               (System.currentTimeMillis() -time) );
        }
        catch (Exception e)
        {
            System.out.println("Bad Number"+ e);
        }
    }


}


November 5, 2003 1:06 PM