Jelajahi Sumber

commit Project4 with updated comments

Lara Luu 10 tahun lalu
induk
melakukan
c01ac06b87
3 mengubah file dengan 147 tambahan dan 14 penghapusan
  1. 135 10
      Project4/src/HashTable.java
  2. 5 1
      Project4/src/ListPrinter.java
  3. 7 3
      Project4/src/Student.java

+ 135 - 10
Project4/src/HashTable.java

@@ -1,13 +1,41 @@
+/**
+ * Hashtable implementation
+ * Contains Iterator
+ * 
+ * Project 2
+ * 
+ * @author Thomas Flucke tflucke
+ * @author Lara Luu ljluu
+ * 
+ * @since 2015/10/21
+ * 
+ * @see Comparable
+ */
+
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
 public class HashTable
 {
+	/**
+	 * A private class used by Hashtable
+	 * Creates HashEntries to be used in the hashtable consisting of an element (type Object) and a boolean active
+	 * 
+	 * Project 4
+	 * 
+	 * @author Thomas Flucke tflucke
+	 * @author Lara Luu ljluu
+	 * 
+	 * @since 2015/10/21
+	 */
 	private class HashEntry
 	{
-		public Object elm;
-		public boolean active;
+		public Object elm; // element stored in the hashEntry
+		public boolean active; // boolean stating whether or not the entry can be used (used for "deleting" an entry)
 		
+		/**
+		 * Constructor creating a new hash entry of Object type
+		 */
 		public HashEntry(Object elm)
 		{
 			this.elm = elm;
@@ -15,15 +43,25 @@ public class HashTable
 		}
 	}
 	
-	private HashEntry[] table;
-	private int size;
+	private HashEntry[] table; // Creates the table array in which the hash entries will be held
+	private int size; // parameter for the number of elements inside the hashtable
 	
+	/**
+	 * Constructor creating a new hash table
+	 * Length of the array: next prime number after doubling the capacity
+	 */
 	public HashTable(int tableCapacity)
 	{
 		table = new HashEntry[nextPrime(2*tableCapacity)];
 		size = 0;
 	}
 	
+
+	/**
+	 * Method returning whether or not the parameter (num) is prime
+	 * @param num
+	 * @return true if the input is prime, false otherwise
+	 */
 	private static boolean isPrime(int num)
 	{
 		if (num % 2 == 0)
@@ -39,7 +77,11 @@ public class HashTable
 		}
 		return true;
 	}
-	
+	/**
+	 * Method for finding the next prime number specified in the parameter (num)
+	 * @param num
+	 * @return the next prime integer
+	 */
 	private static int nextPrime(int num)
 	{
 		if (num % 2 == 0)
@@ -53,21 +95,44 @@ public class HashTable
 		return num;
 	}
 	
+	/**
+	 * Method returning whether or not a hashEntry is active
+	 * @param entry
+	 * @return true if the hashEntry is active, false if it is not
+	 */
 	private static boolean isActive(HashEntry entry)
 	{
 		return entry != null && entry.active;
 	}
 	
+	/**
+	 * A private class used by Hashtable
+	 * Visits each number in the order of its hash
+	 * 
+	 * Project 4
+	 * 
+	 * @author Thomas Flucke tflucke
+	 * @author Lara Luu ljluu
+	 * 
+	 * @since 2015/10/21
+	 */
 	private class Iter implements Iterator<Object>
 	{
-		private int cursor;
+		private int cursor; // cursor: points to current location on the hashTable
 		
+		/**
+		 * Constructor creating an iterator
+		 * cursor starts at -1
+		 */
 		public Iter()
 		{
 			cursor = -1;
 			findNextElm();
 		}
 
+		/**
+		 * function moving the cursor onto the next element
+		 */
 		private void findNextElm()
 		{
 			cursor++;
@@ -77,28 +142,42 @@ public class HashTable
 			}
 		}
 		
+		/**
+		 * Returning a boolean stating whether or not there is a next node
+		 */
 		@Override
 		public boolean hasNext() {
 			return cursor < table.length;
 		}
 
+		/**
+		 * Returns the next element in the in-level traversal
+		 */
 		@Override
 		public Object next() {
 			if (!hasNext())
 			{
 				throw new NoSuchElementException();
 			}
-			Object tmp = table[cursor];
+			Object tmp = table[cursor]; //temp variable holding the value of the cursor
 			findNextElm();
 			return tmp;
 		}
 
+		/**
+		 * throws an UnsupportedOperationException; is not meant to be used in the class
+		 */
 		@Override
 		public void remove() {
 			throw new UnsupportedOperationException();
 		}
 	}
 	
+	/**
+	 * Method inserting an element of type Object in the hashTable
+	 * resizes the hashtable if it the size of the hashTable is greater than half of the hashTable
+	 * @param item
+	 */
 	public void insert(Object item)
 	{
 		int index = findNextIndex(item);
@@ -117,11 +196,21 @@ public class HashTable
 		}
 	}
 	
+	/**
+	 * Returns the unique identifier for the object
+	 * @param obj
+	 * @return the index of the array where the element will be stored (hash)
+	 */
 	private int hash(Object obj)
 	{
 		return Math.abs(obj.hashCode()) % table.length;
 	}
 	
+	/**
+	 * finds the next available index for the object to be stored if the first spot is already taken
+	 * @param obj
+	 * @return the next index where the object will be stored
+	 */
 	private int findNextIndex(Object obj)
 	{
 		int hash = hash(obj);
@@ -133,9 +222,13 @@ public class HashTable
 		return hash+i*i;
 	}
 	
+	/**
+	 * Creates a new hashtable twice the size of the old hashTable
+	 * Moves all the old elements into the new hashTable
+	 */
 	private void rehash()
 	{
-		HashEntry[] tmp = table;
+		HashEntry[] tmp = table; //creates temporary table twice the size of the old one
 		table = new HashEntry[nextPrime(table.length*2)];
 		for (HashEntry he : tmp)
 		{
@@ -146,6 +239,11 @@ public class HashTable
 		}
 	}
 	
+	/**
+	 * Method "deleting" the specified element in the hashTable
+	 * To "delete": makes the element inactive by changing the active instance variable to "false"
+	 * @param item
+	 */
 	public void delete(Object item)
 	{
 		int index = findNextIndex(item);
@@ -155,6 +253,12 @@ public class HashTable
 		}
 	}
 	
+	/**
+	 * Method to find the specified item in the hashTable
+	 * 
+	 * @param item
+	 * @return the index at which the item is located, returns null if the item is not located in the hashTable
+	 */
 	public Object find(Object item)
 	{
 		int index = findNextIndex(item);
@@ -165,9 +269,13 @@ public class HashTable
 		return null;
 	}
 	
+	/**
+	 * Counts the number of entries in the hashtable
+	 * @return an integer for the amount of items in a hashTable
+	 */
 	public int elementCount()
 	{
-		int count = 0;
+		int count = 0; // integer counting the amount of items in an object
 		for (HashEntry he : table)
 		{
 			if (isActive(he))
@@ -178,6 +286,10 @@ public class HashTable
 		return count;
 	}
 	
+	/**
+	 * Method seeing whether or not the hashTable is empty
+	 * @return true if the hashTable is empty, false if it is not
+	 */
 	public boolean isEmpty()
 	{
 		for (HashEntry he : table)
@@ -190,11 +302,20 @@ public class HashTable
 		return true;
 	}
 	
+	/**
+	 * Empties the hashTable of all elements
+	 */
 	public void makeEmpty()
 	{
 		table = new HashEntry[table.length];
 	}
 	
+	/**
+	 * Prints out the hashtable in the following format:
+	 * [0]: xxx, active
+ 	 *[1]: empty
+   	 *[2]: yyy, inactive
+	 */
 	public void printTable()
 	{
 		for (int i = 0; i < table.length; i++)
@@ -213,7 +334,11 @@ public class HashTable
 			System.out.println();
 		}
 	}
-	
+
+	/**
+	 * Constructor creating a new iterator
+	 * @return a new Iterator
+	 */
 	@SuppressWarnings("rawtypes")
 	public Iterator iterator()
 	{

+ 5 - 1
Project4/src/ListPrinter.java

@@ -141,7 +141,11 @@ public class ListPrinter
 		}
 		in.close();
 	}
-	
+	/**
+	 * Helper method to determine whether or not the line inputted is a valid student
+	 * @param line
+	 * @return a student if valid or null if it is an invalid line
+	 */
 	private static Student readStudent(String line)
 	{
 		//Defines the format for what a (non-negative) long value looks like

+ 7 - 3
Project4/src/Student.java

@@ -1,12 +1,12 @@
 /**
  * Stores student id and last name in a sortable way.
  * 
- * Project 2
+ * Project 4
  * 
  * @author Thomas Flucke tflucke
  * @author Lara Luu ljluu
  * 
- * @since 2015/10/22
+ * @since 2015/11/12
  */
 
 public class Student
@@ -37,7 +37,11 @@ public class Student
 	{
 		return other instanceof Student && id == ((Student) other).id;
 	}
-	
+	/**
+	 * Creates a hashcode representation for the student object.
+	 * The hashcode will be taken from the student's ID number.
+	 * @return long type student id
+	 */
 	@Override
 	public int hashCode()
 	{