summaryrefslogtreecommitdiff
path: root/projects/project1_frequencyBag
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-02-06 11:41:36 -0500
committerAlexander Pickering <alexandermpickering@gmail.com>2017-02-06 11:41:36 -0500
commit89cdf3efb49335e7c07a68a5a64657eeec2288a6 (patch)
treecdc0fd8165e65b1637fa54cac11c932acefc8a89 /projects/project1_frequencyBag
downloadcoe0445-master.tar.gz
coe0445-master.tar.bz2
coe0445-master.zip
Inital commitHEADmaster
Diffstat (limited to 'projects/project1_frequencyBag')
-rw-r--r--projects/project1_frequencyBag/CharacterFrequency.java34
-rw-r--r--projects/project1_frequencyBag/FNode.java0
-rw-r--r--projects/project1_frequencyBag/FrequencyBag.java168
-rw-r--r--projects/project1_frequencyBag/FrequencyBagTester.java531
-rw-r--r--projects/project1_frequencyBag/FrequencyFrame.java72
-rw-r--r--projects/project1_frequencyBag/FrequencyGraphComponent.java71
-rw-r--r--projects/project1_frequencyBag/RandomDistribution.java77
-rw-r--r--projects/project1_frequencyBag/letter1.txt16
-rw-r--r--projects/project1_frequencyBag/project1_frequencyBag.pdfbin0 -> 165759 bytes
9 files changed, 969 insertions, 0 deletions
diff --git a/projects/project1_frequencyBag/CharacterFrequency.java b/projects/project1_frequencyBag/CharacterFrequency.java
new file mode 100644
index 0000000..4bf4a78
--- /dev/null
+++ b/projects/project1_frequencyBag/CharacterFrequency.java
@@ -0,0 +1,34 @@
+import java.util.*;
+import java.io.*;
+
+public class CharacterFrequency
+{
+ public static void main(String[] args)
+ {
+ File f = new File("letter1.txt");
+ Scanner s;
+ try
+ {
+ s = new Scanner(f);
+ }
+ catch (Exception e)
+ {
+ System.out.println("Big error!");
+ return;
+ }
+ FrequencyBag<Character> fb = new FrequencyBag<Character>();
+ while(s.hasNextLine())
+ {
+ String s1 = s.nextLine();
+ for(char c : s1.toCharArray())
+ {
+ fb.add(new Character((char)(c<='Z'&&c>='A'?c+0x20:c)));
+ }
+ }
+ System.out.println("Character: Frequency\n===================");
+ for(int i=0x61;i<=0x7A;i++)
+ {
+ System.out.println((char)i+": "+fb.getFrequencyOf(new Character((char)i)));
+ }
+ }
+} \ No newline at end of file
diff --git a/projects/project1_frequencyBag/FNode.java b/projects/project1_frequencyBag/FNode.java
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/projects/project1_frequencyBag/FNode.java
diff --git a/projects/project1_frequencyBag/FrequencyBag.java b/projects/project1_frequencyBag/FrequencyBag.java
new file mode 100644
index 0000000..3545ff9
--- /dev/null
+++ b/projects/project1_frequencyBag/FrequencyBag.java
@@ -0,0 +1,168 @@
+
+public class FrequencyBag<T>
+{
+ // TO DO: Instance Variables
+ private FNode head;
+ private boolean hasChanged = true;
+ private int max;
+ //Tracks the number of times add has been called
+ private int total;
+ /**
+ * Constructor
+ * Constructs an empty frequency bag.
+ */
+ public FrequencyBag()
+ {
+ head = null;
+ }
+
+ /**
+ * Adds new entry into this frequency bag.
+ * @param aData the data to be added into this frequency bag.
+ */
+ public void add(T aData)
+ {
+ //Add to the start to make add an O(1) operation
+ if(!addhelper(head,aData))
+ {
+ hasChanged = true;
+ FNode n = new FNode(aData,head);
+ head = n;
+ }
+ total++;
+ }
+ public boolean addhelper(FNode head, T data)
+ {
+ if(head == null)
+ {
+ //We haven't found the item in the list, return false
+ return false;
+ }
+ if(head.getObject().equals(data))
+ {
+ head.add();
+ return true;
+ }
+ return addhelper(head.getNext(),data);
+ }
+
+ /**
+ * Gets the number of occurrences of aData in this frequency bag.
+ * @param aData the data to be checked for its number of occurrences.
+ * @return the number of occurrences of aData in this frequency bag.
+ */
+ public int getFrequencyOf(T aData)
+ {
+ return getFrequencyOfHelper(head,aData);
+ }
+
+ public int getFrequencyOfHelper(FNode<T> node,T data)
+ {
+ //If we've hit a null node, the object isn't in the list
+ if(node == null)
+ {
+ return 0;
+ }
+ if(node.getObject().equals(data))
+ {
+ return node.getFrequency();
+ }
+ else
+ {
+ return getFrequencyOfHelper(node.getNext(),data);
+ }
+ }
+ /**
+ * Gets the maximum number of occurrences in this frequency bag.
+ * @return the maximum number of occurrences of an entry in this
+ * frequency bag.
+ */
+ public int getMaxFrequency()
+ {
+ if(hasChanged)
+ {
+ hasChanged = false;
+ max = getMaxFrequencyHelper(head);
+ return max;
+ }
+ else
+ {
+ return max;
+ }
+ }
+ private int getMaxFrequencyHelper(FNode<T> node)
+ {
+ if(node == null) //Were at the end
+ return 0;
+ int nmax = getMaxFrequencyHelper(node.getNext());
+ return node.getFrequency() > nmax?node.getFrequency():nmax;
+ }
+
+ /**
+ * Gets the probability of aData
+ * @param aData the specific data to get its probability.
+ * @return the probability of aData
+ */
+ public double getProbabilityOf(T aData)
+ {
+ return getFrequencyOf(aData)/(total*1.0);
+ }
+
+ /**
+ * Empty this bag.
+ */
+ public void clear()
+ {
+ head = null;
+ hasChanged = true;
+ total = 0;
+ }
+
+ /**
+ * Gets the number of entries in this bag.
+ * @return the number of entries in this bag.
+ */
+ public int size()
+ {
+ return total;
+ }
+ public class FNode<T>
+ {
+ private T object = null;
+ private int frequency = 1;
+ private FNode next = null;
+
+ public FNode()
+ {
+ }
+ public FNode(T obj,FNode n)
+ {
+ object = obj;
+ next = n;
+ }
+ public void setObject(T obj)
+ {
+ object = obj;
+ }
+ public T getObject()
+ {
+ return object;
+ }
+ public void add()
+ {
+ frequency++;
+ }
+ public int getFrequency()
+ {
+ return frequency;
+ }
+ public FNode getNext()
+ {
+ return next;
+ }
+ public void setNext(FNode n)
+ {
+ next = n;
+ }
+ }
+}
diff --git a/projects/project1_frequencyBag/FrequencyBagTester.java b/projects/project1_frequencyBag/FrequencyBagTester.java
new file mode 100644
index 0000000..db1ae99
--- /dev/null
+++ b/projects/project1_frequencyBag/FrequencyBagTester.java
@@ -0,0 +1,531 @@
+import java.util.Random;
+import java.util.Scanner;
+
+public class FrequencyBagTester
+{
+ public static void main(String[] args)
+ {
+ int numberOfData = 20;
+ int maxValueEx = 5;
+ boolean wrongFrequency = false;
+ int failed = 0;
+ Scanner in = new Scanner(System.in);
+ //CHANGE THIS BACK----------------------------------CHANGE THIS BACK-------
+ int option = 1;
+ //CHANGE THIS BACK----------------------------------CHANGE THIS BACK-------
+
+ while(option == 0)
+ {
+ System.out.println("Please select a test option");
+ System.out.println(" 1. Stop the test as soon as an error is detected");
+ System.out.println(" 2. Do not stop the test when an error is detected");
+ System.out.print("Enter an option (1 or 2): ");
+ option = in.nextInt();
+
+ if(option != 1 && option != 2)
+ {
+ System.out.println(option + " is not a valid choice.\n");
+ option = 0;
+ }
+ }
+
+ in.close();
+
+ FrequencyBag<Integer> fb = new FrequencyBag<Integer>();
+
+ // getFrequencyOf() empty bag
+
+ System.out.print("Checking the method getFrequencyOf() of an empty frequency bag: ");
+ for(int i = 0; i < maxValueEx; i++)
+ {
+ if(fb.getFrequencyOf(i) != 0)
+ {
+ if(!wrongFrequency)
+ {
+ System.out.println("FAIL");
+ failed++;
+ }
+ System.out.println("The method getFrequencyOf(" + i + ") of an empty bag should return 0.");
+ System.out.println("But your method getFrequencyOf(" + i + ") returns " + fb.getFrequencyOf(i) + ".");
+ wrongFrequency = true;
+ }
+ }
+
+ if(!wrongFrequency)
+ {
+ System.out.println("PASS");
+ }
+ else if(option == 1)
+ {
+ return;
+ }
+
+
+ // size() of empty bag.
+
+ System.out.print("Checking the method size() of an empty bag: ");
+ if(fb.size() != 0)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("The size of an empty bag should be 0.");
+ System.out.println("But your method size() returns " + fb.size() + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // getMaxFrequency() of empty bag
+
+ System.out.print("Checking the method getMaxFrequency() of an empty frequency bag: ");
+ if(fb.getMaxFrequency() != 0)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("The method getMaxFrequency() of an empty bag should return 0.");
+ System.out.println("But your method getMaxFrequency() returns " + fb.getMaxFrequency() + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS\n");
+ }
+
+ // Adding data into empty bag
+
+ int dataSet[] = new int[5];
+
+ for(int i = 0; i < maxValueEx; i++)
+ {
+ dataSet[i] = 0;
+ }
+
+ Random rand = new Random();
+
+ System.out.println("Add the following data into your frequency bag (in that order):");
+
+ for(int i = 0; i < numberOfData; i++)
+ {
+ int temp = rand.nextInt(maxValueEx);
+
+ System.out.print(temp + " ");
+ fb.add(temp);
+ dataSet[temp]++;
+ }
+ System.out.println();
+
+ // getFrequencyOf()
+
+ System.out.println("Checking the method getFrequencyOf() of each data in this frequency bag: ");
+
+ for(int i = 0; i < maxValueEx; i++)
+ {
+ System.out.print("Frequency of " + i + ": ");
+ if(fb.getFrequencyOf(i) != dataSet[i])
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("The frequency of " + i + " is " + dataSet[i] + ".");
+ System.out.println("But the method getFrequencyOf(" + i + ") returns " + fb.getFrequencyOf(i) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+ }
+
+ // getFrequencyOf()
+
+ System.out.print("Checking the method getFrequencyOf() of a data that is not in this frequency bag: ");
+
+ if(fb.getFrequencyOf(-1) != 0)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("The frequency of -1 is 0.");
+ System.out.println("But the method getFrequencyOf(-1) returns " + fb.getFrequencyOf(-1) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // getProbabilityOf()
+
+ System.out.println("Checking the method getProbabilityOf() of each data in this frequency bag: ");
+
+ for(int i = 0; i < maxValueEx; i++)
+ {
+ System.out.print("Probability of " + i + ": ");
+ if(Math.abs(fb.getProbabilityOf(i) - ((double) dataSet[i]/numberOfData)) > 0.000001)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("The probability of " + i + " is " + ((double) dataSet[i]/numberOfData) + ".");
+ System.out.println("But the method getProbabilityOf(" + i + ") returns " + fb.getProbabilityOf(i) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+ }
+
+ // getProbabilityOf()
+
+ System.out.print("Checking the method getProbabilityOf() of a data that is not in this frequency bag: ");
+
+ if(fb.getProbabilityOf(-1) != 0.0)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("The probability of -1 is 0.");
+ System.out.println("But the method getProbabilityOf(-1) returns " + fb.getProbabilityOf(-1) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // size()
+
+ System.out.print("Checking the method size() of this non-empty frequency bag: ");
+ if(fb.size() != numberOfData)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("After adding " + numberOfData + " entries, the method size() should return " + numberOfData + ".");
+ System.out.println("But your method size() returns " + fb.size() + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // getMaxFrequency()
+
+ System.out.print("Checking the method getMaxFrequency() of this non-empty frequency bag: ");
+
+ int tempMax = 0;
+
+ for(int i = 0; i < maxValueEx; i++)
+ {
+ if(tempMax < dataSet[i])
+ {
+ tempMax = dataSet[i];
+ }
+ }
+
+ if(fb.getMaxFrequency() != tempMax)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("The method getMaxFrequency() of this non-empty frequency bag should return " + tempMax + ".");
+ System.out.println("But your method getMaxFrequency() of this non-empty frequency bag returns " + fb.getMaxFrequency() + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS\n");
+ }
+
+ // clear();
+
+ fb.clear();
+
+ System.out.println("Checking the method clear()");
+ System.out.print("Checking the method size() after clear(): ");
+ if(fb.size() != 0)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("After the bag is cleared, the method size() should return 0.");
+ System.out.println("But your method size() returns " + fb.size() + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // getFrequencyOf() after clear()
+
+ System.out.print("Checking the method getFrequencyOf(3) after clear(): ");
+ if(fb.getFrequencyOf(3) != 0)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("After the bag is cleared, the method getFrequencyOf(3) should return 0.");
+ System.out.println("But your method getFrequencyOf(3) returns " + fb.getFrequencyOf(3) + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // getMaxFrequency() after clear()
+
+ System.out.print("Checking the method getMaxFrequency() after clear(): ");
+ if(fb.getMaxFrequency() != 0)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("After the bag is cleared, the method getMaxFrequency() should return 0.");
+ System.out.println("But your method getMaxFrequency() returns " + fb.getMaxFrequency() + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Generate large bag
+
+ numberOfData = 1000000;
+ maxValueEx = 1000;
+
+ dataSet = new int[maxValueEx];
+
+ for(int i = 0; i < maxValueEx; i++)
+ {
+ dataSet[i] = 0;
+ }
+
+ System.out.println("\nSo far so good.");
+ System.out.println("Let's try adding " + numberOfData + " data into your frequency bag.");
+
+ for(int i = 0; i < numberOfData; i++)
+ {
+ int temp = rand.nextInt(maxValueEx);
+
+ fb.add(temp);
+ dataSet[temp]++;
+ }
+
+ // size() of large bag.
+
+ System.out.print("Checking the method size() of this non-empty frequency bag: ");
+ if(fb.size() != numberOfData)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("After adding " + numberOfData + " entries, the method size() should return " + numberOfData + ".");
+ System.out.println("But your method size() returns " + fb.size() + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // getFrequencyOf() of large bag
+
+ System.out.print("Checking the method getFrequencyOf() of each data in this frequency bag: ");
+
+ wrongFrequency = false;
+
+ for(int i = 0; i < maxValueEx; i++)
+ {
+ if(fb.getFrequencyOf(i) != dataSet[i])
+ {
+ wrongFrequency = true;
+ }
+ }
+
+ if(wrongFrequency)
+ {
+ failed++;
+ System.out.println("FAIL");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // getProbabilityOf() of large bag
+
+ System.out.print("Checking the method getProbabilityOf() of each data in this frequency bag: ");
+
+ boolean wrongProbability = false;
+
+ for(int i = 0; i < maxValueEx; i++)
+ {
+ if(Math.abs(fb.getProbabilityOf(i) - ((double) dataSet[i]/numberOfData)) > 0.000001)
+ {
+ wrongProbability = true;
+ }
+ }
+
+ if(wrongProbability)
+ {
+ failed++;
+ System.out.println("FAIL");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // getMaxFrequency() of large bag
+
+ System.out.print("Checking the method getMaxFrequency() of this non-empty frequency bag: ");
+
+ tempMax = 0;
+
+ for(int i = 0; i < maxValueEx; i++)
+ {
+ if(tempMax < dataSet[i])
+ {
+ tempMax = dataSet[i];
+ }
+ }
+
+ if(fb.getMaxFrequency() != tempMax)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("The method getMaxFrequency() of this non-empty frequency bag should return " + tempMax + ".");
+ System.out.println("But your method getMaxFrequency() of this non-empty frequency bag returns " + fb.getMaxFrequency() + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ // Frequency Bag of String
+
+ System.out.println("\nSo far so good.");
+ System.out.println("Let's construct a frequency bag of String.");
+
+ FrequencyBag<String> sfb = new FrequencyBag<String>();
+
+ String s = "hello how are you i am find thank you and you i am fine thank you";
+ String[] str = s.split(" ");
+
+ for(int i = 0; i < str.length; i++)
+ {
+ sfb.add(str[i]);
+ }
+
+ System.out.println("Adding the following strings into an empty frequency bag:");
+ System.out.println(s + "\n");
+
+ System.out.print("Checking the method size() of this non-empty frequency bag: ");
+ if(sfb.size() != 16)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("After adding 16 entries, the method size() should return 16.");
+ System.out.println("But your method size() returns " + sfb.size() + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ System.out.print("Checking the method getFrequencyOf(\"am\"): ");
+ if(sfb.getFrequencyOf("am") != 2)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("There are two strings \"am\" added into this bag.");
+ System.out.println("But your method getFrequencyOf(\"am\") returns " + sfb.getFrequencyOf("am") + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ System.out.print("Checking the method getMaxFrequency(): ");
+ if(sfb.getMaxFrequency() != 4)
+ {
+ failed++;
+ System.out.println("FAIL");
+ System.out.println("The maximum frequency should be 4 (the string \"you\").");
+ System.out.println("But your method getMaxFrequency() returns " + sfb.getMaxFrequency() + ".\n");
+ if(option == 1)
+ {
+ return;
+ }
+ }
+ else
+ {
+ System.out.println("PASS");
+ }
+
+ System.out.println();
+
+ if(failed != 0)
+ {
+ System.out.println("Threre are " + failed + " in this test. Fix your program.");
+ }
+ else
+ {
+ System.out.println("Congratulation!!! Your FrequencyBag works perfectly (I hope).");
+ System.out.println("Run the program FrequencyFrame and compare its result.");
+ }
+ }
+}
diff --git a/projects/project1_frequencyBag/FrequencyFrame.java b/projects/project1_frequencyBag/FrequencyFrame.java
new file mode 100644
index 0000000..bc26e1a
--- /dev/null
+++ b/projects/project1_frequencyBag/FrequencyFrame.java
@@ -0,0 +1,72 @@
+import java.awt.BorderLayout;
+import java.awt.GridLayout;
+
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.border.TitledBorder;
+
+
+public class FrequencyFrame
+{
+ public static void main(String[] args) throws InterruptedException
+ {
+ JFrame frame = new JFrame();
+
+ FrequencyBag<Integer> normal = new FrequencyBag<Integer>();
+ FrequencyBag<Integer> laplace = new FrequencyBag<Integer>();
+ FrequencyBag<Integer> uniform = new FrequencyBag<Integer>();
+ FrequencyBag<Integer> clock = new FrequencyBag<Integer>();
+
+ FrequencyGraphComponent normalFC = new FrequencyGraphComponent(normal, -500, 500, 1);
+ FrequencyGraphComponent laplaceFC = new FrequencyGraphComponent(laplace, -500, 500, 1);
+ FrequencyGraphComponent uniformFC = new FrequencyGraphComponent(uniform, -500, 500, 1);
+ FrequencyGraphComponent clockFC = new FrequencyGraphComponent(clock, 0, 1000, 1);
+
+ JPanel normalPanel = new JPanel();
+ normalPanel.setLayout(new BorderLayout());
+ normalPanel.setBorder(new TitledBorder("Normal Distribution"));
+ normalPanel.add(normalFC);
+
+ JPanel laplacePanel = new JPanel();
+ laplacePanel.setLayout(new BorderLayout());
+ laplacePanel.setBorder(new TitledBorder("Laplace Distribution"));
+ laplacePanel.add(laplaceFC);
+
+ JPanel uniformPanel = new JPanel();
+ uniformPanel.setLayout(new BorderLayout());
+ uniformPanel.setBorder(new TitledBorder("Uniform Distribution"));
+ uniformPanel.add(uniformFC);
+
+ JPanel clockPanel = new JPanel();
+ clockPanel.setLayout(new BorderLayout());
+ clockPanel.setBorder(new TitledBorder("Clock Distributioin"));
+ clockPanel.add(clockFC);
+
+ frame.setSize(800,800);
+ frame.setTitle("Probability Distributions");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setLayout(new GridLayout(2,2));
+ frame.add(normalPanel);
+ frame.add(laplacePanel);
+ frame.add(uniformPanel);
+ frame.add(clockPanel);
+ frame.setVisible(true);
+
+ for(int i = 0; i < 20000; i++)
+ {
+ for(int j = 0; j < 10; j++)
+ {
+ normal.add(RandomDistribution.normalDistributionInt(-500, 500));
+ laplace.add(RandomDistribution.laplaceDistributionInt(-500, 500));
+ uniform.add(RandomDistribution.uniformDistributionInt(-500, 500));
+ clock.add(RandomDistribution.clockDistribution(0, 1000, 50, 50));
+ }
+ normalFC.repaint();
+ laplaceFC.repaint();
+ uniformFC.repaint();
+ clockFC.repaint();
+ Thread.sleep(1);
+ }
+ }
+
+}
diff --git a/projects/project1_frequencyBag/FrequencyGraphComponent.java b/projects/project1_frequencyBag/FrequencyGraphComponent.java
new file mode 100644
index 0000000..3d48c74
--- /dev/null
+++ b/projects/project1_frequencyBag/FrequencyGraphComponent.java
@@ -0,0 +1,71 @@
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.geom.Line2D;
+
+import javax.swing.JComponent;
+
+@SuppressWarnings("serial")
+public class FrequencyGraphComponent extends JComponent
+{
+ private FrequencyBag<Integer> fb;
+ private int min;
+ private int max;
+ private int delta;
+ private int width;
+ private int height;
+ private int leftMargin = 10;
+ private int rightMargin = 10;
+ private int topMargin = 10;
+ private int bottomMargin = 10;
+
+ public FrequencyGraphComponent(FrequencyBag<Integer> afb, int aMin, int aMax, int aDelta)
+ {
+ fb = afb;
+ min = aMin;
+ max = aMax;
+ delta = aDelta;
+ }
+
+ public void paintComponent(Graphics g)
+ {
+ Graphics2D g2 = (Graphics2D) g;
+
+ g2.setColor(Color.BLACK);
+
+ width = this.getWidth();
+ height = this.getHeight();
+
+ int maxFrequency = fb.getMaxFrequency();
+ double deltaWidth = (double) (width - (leftMargin + rightMargin)) / ((max - min) / delta);
+ double deltaHeight = (double) (height - (topMargin + bottomMargin)) / maxFrequency;
+
+ Line2D.Double line = new Line2D.Double(0,0,0,0);
+
+ for(int i = min; i < max; i = i + delta)
+ {
+ double sx = ((i - min) * deltaWidth) + leftMargin;
+ double ex = (((i + delta) - min) * deltaWidth) + leftMargin;
+ double sy = (height - bottomMargin) - (fb.getFrequencyOf(i) * deltaHeight);
+ double ey = (height - bottomMargin) - (fb.getFrequencyOf(i + delta) * deltaHeight);
+ line.setLine(sx,sy,ex,ey);
+ g2.draw(line);
+ }
+
+ g2.setColor(Color.GREEN);
+ double accProp = 0.0;
+
+ for(int i = min; i < max; i = i + delta)
+ {
+ accProp = accProp + fb.getProbabilityOf(i);
+ double nextProp = fb.getProbabilityOf(i + delta);
+
+ double sx = ((i - min) * deltaWidth) + leftMargin;
+ double ex = (((i + delta) - min) * deltaWidth) + leftMargin;
+ double sy = (height - bottomMargin) * (1 - accProp);
+ double ey = (height - bottomMargin) * (1 - (accProp + nextProp));
+ line.setLine(sx,sy,ex,ey);
+ g2.draw(line);
+ }
+ }
+}
diff --git a/projects/project1_frequencyBag/RandomDistribution.java b/projects/project1_frequencyBag/RandomDistribution.java
new file mode 100644
index 0000000..4e8bc0f
--- /dev/null
+++ b/projects/project1_frequencyBag/RandomDistribution.java
@@ -0,0 +1,77 @@
+import java.util.Random;
+
+
+public class RandomDistribution
+{
+ public static int normalDistributionInt(int min, int max)
+ {
+ int result;
+ do
+ {
+ result = (int) Math.round(boxMuller() * 100);
+ }
+ while(result < min || result > max);
+
+ return result;
+ }
+
+ private static double boxMuller()
+ {
+ Random rand = new Random();
+ double u = rand.nextDouble();
+ double v = rand.nextDouble();
+ return Math.sqrt(-2 * (Math.log(u)/Math.log(Math.E))) * Math.cos(2 * Math.PI * v);
+ }
+
+ public static int laplaceDistributionInt(int min, int max)
+ {
+ int result;
+ do
+ {
+ result = (int) Math.round(laplaceDistribution() * 100);
+ }
+ while(result < min || result > max);
+
+ return result;
+ }
+
+ private static double laplaceDistribution()
+ {
+ Random rand = new Random();
+ double u = rand.nextDouble() - 0.5;
+
+ return 0 - (Math.signum(u) * Math.log(1 - (2 * Math.abs(u))));
+ }
+
+ public static int uniformDistributionInt(int min, int max)
+ {
+ Random rand = new Random();
+
+ return rand.nextInt((max - min) + 1) + min;
+ }
+
+ public static int clockDistribution(int min, int max, int hWidth, int lWidth)
+ {
+ int result;
+
+ do
+ {
+ result = uniformDistributionInt(min, max);
+ int modResult = result % (hWidth + lWidth);
+ if(modResult < hWidth)
+ {
+ break;
+ }
+ else
+ {
+ if(uniformDistributionInt(0,1) == 1)
+ {
+ break;
+ }
+ }
+ }
+ while(true);
+
+ return result;
+ }
+}
diff --git a/projects/project1_frequencyBag/letter1.txt b/projects/project1_frequencyBag/letter1.txt
new file mode 100644
index 0000000..f9d942a
--- /dev/null
+++ b/projects/project1_frequencyBag/letter1.txt
@@ -0,0 +1,16 @@
+To whom it may concern,
+
+As the Director of the Department of Computer Sciences of North Pole University of Technology, which is one of the most prestigious engineering universities in north pole, I deem it a pleasure to recommend John Smith, one of the outstanding students in our department for admission and assistantship you are your graduate program.
+
+Since his enrollment into our department with remarkable entrance scores, John has embodied the fine character of strong inquisition and industriousness in learning, which has gained him significant academic success during his college years. I am aware of his distinctive academic performance as reflected in his transcripts. He maintains a very high grade point average that places him in the top five percent out of one hundred and fifty through the past three years, winning scholarships of the department almost every year. In the College English Speech Contest organized by the Teaching Committee of North Pole Province, he excelled most of other contestants, winning the first prize in the Region wide preliminary contest, and the third prize in the final of the provincial level, being the sole participant representing our university.
+
+I have also discovered him to be perseverant and enthusiastic in studies and extracurricular activities. During the Military Life Experiencing period, a compulsory training program for all North Pole college freshmen upon their immediate entrance, he was granted the title of Excellent Trainee thanks to his painstaking training efforts, though the training was extraordinarily rigorous and demanding. In the past college years, he has enthusiastically taken part in many students societies, such as Computer Society, Road to American English and Literature Garden, which has both seen his versatility and organizing ability by serving in quite a few positions.
+
+Speaking of his organizing ability, I have to mention two well known events under his active involvement and organization. One is the College Computer Festival of North Pole Province and the other is the Science and Culture Week of our school. In the first one, he was one of the chief representatives from our department to undertake this provincial level activity. His enthusiastic involvements in liaison, coordination and the contestants performance evaluation had won him the honorable title of Active Organizing Participant specially granted by our department for this important occasion. In the second one of Science and Culture Week, organized in our school in nineteen ninety nine, he was a chief organizer. And it was so successful that it attracted more than one thousand students to participate, who were all satisfied with this event for the opportunity of a better understanding of the past and future of science in north pole.
+
+As the Director, I am deeply impressed and moved by his capability of organizing and his sense of involvement, which have been explicitly demonstrated through all his participation in these activities
+
+A bright young man, with blazing intelligence, energy, and determination, John deserves my first rate recommendation. Thus, I would unqualifiedly recommend him for admission into the Doctor of Philosophy program at your university and I will appreciate your sincere assistance to his admission.
+
+
+Sincerely yours,
diff --git a/projects/project1_frequencyBag/project1_frequencyBag.pdf b/projects/project1_frequencyBag/project1_frequencyBag.pdf
new file mode 100644
index 0000000..d7af0aa
--- /dev/null
+++ b/projects/project1_frequencyBag/project1_frequencyBag.pdf
Binary files differ