Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1.  
  2. public class FrequencyBag<T>
  3. {
  4.     // TO DO: Instance Variables
  5.     private Node firstNode;
  6.         private int numberOfEntries;
  7.     /**
  8.      * Constructor
  9.      * Constructs an empty frequency bag.
  10.      */
  11.     public FrequencyBag()
  12.     {
  13.         firstNode = null;
  14.                 numberOfEntries = 0;
  15.                
  16.     }
  17.    
  18.     /**
  19.      * Adds new entry into this frequency bag.
  20.      * @param aData the data to be added into this frequency bag.
  21.      */
  22.     public void add(T aData)
  23.     {
  24.         if(numberOfEntries != 0){
  25.                     Node newNode = new Node(aData);
  26.                     newNode.next = firstNode;
  27.                     firstNode = newNode;
  28.                     numberOfEntries++;
  29.                 }
  30.                 else{
  31.                     Node newNode = new Node(aData);
  32.                     firstNode = newNode;
  33.                     numberOfEntries++;
  34.                 }
  35.     }
  36.    
  37.     /**
  38.      * Gets the number of occurrences of aData in this frequency bag.
  39.      * @param aData the data to be checked for its number of occurrences.
  40.      * @return the number of occurrences of aData in this frequency bag.
  41.      */
  42.     public int getFrequencyOf(T aData)
  43.     {
  44.             int counter  = 0;
  45.             Node currentNode = firstNode;
  46.             for(int i = 1; i <=numberOfEntries; i++) {
  47.                 if(currentNode.data.equals(aData))
  48.                 {
  49.                     counter ++;
  50.                    
  51.                 }
  52.                 currentNode = currentNode.next;
  53.                    
  54.                 }
  55.             return counter;
  56.             }
  57.        
  58.    
  59.  
  60.     /**
  61.      * Gets the maximum number of occurrences in this frequency bag.
  62.      * @return the maximum number of occurrences of an entry in this
  63.      * frequency bag.
  64.      */
  65.     public int getMaxFrequency()
  66.     {
  67.         // TO DO
  68.             Node currentNode = firstNode;
  69.             int currentFrequency = 0;
  70.             int maxFrequency = currentFrequency;
  71.  
  72.             for(int i = 1; i <= numberOfEntries; i++)
  73.         {
  74.             currentFrequency = getFrequencyOf(currentNode.data);
  75.             if(currentFrequency > maxFrequency)
  76.             {
  77.                 maxFrequency = currentFrequency;
  78.             }
  79.             currentNode = currentNode.next;
  80.         }
  81.         return maxFrequency;
  82.     }
  83.    
  84.     /**
  85.      * Gets the probability of aData
  86.      * @param aData the specific data to get its probability.
  87.      * @return the probability of aData
  88.      */
  89.     public double getProbabilityOf(T aData)
  90.     {
  91.         double num = getFrequencyOf(aData);
  92.                 double probability = num / numberOfEntries;
  93.                 return probability;
  94.     }
  95.  
  96.     /**
  97.      * Empty this bag.
  98.      */
  99.     public void clear()
  100.         {
  101.             Node tempNode = null;
  102.            
  103.             for(int i = 1; i <= numberOfEntries; i++){
  104.             tempNode = firstNode.next;
  105.             firstNode = null;
  106.             firstNode = tempNode;
  107.      
  108.             }
  109.             numberOfEntries = 0;
  110.                
  111.     }
  112.    
  113.     /**
  114.      * Gets the number of entries in this bag.
  115.      * @return the number of entries in this bag.
  116.      */
  117.     public int size()
  118.     {
  119.         return numberOfEntries;
  120.     }
  121.        
  122.    private class Node {
  123.        
  124.        private T data;
  125.        private Node next;
  126.        
  127.        private Node(T aData){
  128.            data = aData;
  129.            next = null;
  130.        }
  131.    
  132.    }    
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement