Selaa lähdekoodia

Fixed random bugs that came up in testing.

Tom Flucke 10 vuotta sitten
vanhempi
commit
b004ff86e8
3 muutettua tiedostoa jossa 29 lisäystä ja 15 poistoa
  1. 9 7
      Project4/src/HashTable.java
  2. 19 7
      Project4/src/ListPrinter.java
  3. 1 1
      Project4/src/Student.java

+ 9 - 7
Project4/src/HashTable.java

@@ -7,7 +7,7 @@
  * @author Thomas Flucke tflucke
  * @author Lara Luu ljluu
  * 
- * @since 2015/10/21
+ * @since 2015/11/11
  * 
  * @see Comparable
  */
@@ -136,7 +136,7 @@ public class HashTable
 		private void findNextElm()
 		{
 			cursor++;
-			while (cursor < table.length && isActive(table[cursor]))
+			while (cursor < table.length && !isActive(table[cursor]))
 			{
 				cursor++;
 			}
@@ -159,7 +159,7 @@ public class HashTable
 			{
 				throw new NoSuchElementException();
 			}
-			Object tmp = table[cursor]; //temp variable holding the value of the cursor
+			Object tmp = table[cursor].elm; //temp variable holding the value of the cursor
 			findNextElm();
 			return tmp;
 		}
@@ -215,11 +215,13 @@ public class HashTable
 	{
 		int hash = hash(obj);
 		int i = 0;
-		while (table[hash + i*i] != null && !table[hash + i*i].elm.equals(obj))
+		int index = hash;
+		while (table[index] != null && !table[index].elm.equals(obj))
 		{
 			i++;
+			index = (hash + i*i) % table.length;
 		}
-		return hash+i*i;
+		return index;
 	}
 	
 	/**
@@ -264,7 +266,7 @@ public class HashTable
 		int index = findNextIndex(item);
 		if (isActive(table[index]))
 		{
-			return table[index];
+			return table[index].elm;
 		}
 		return null;
 	}
@@ -320,7 +322,7 @@ public class HashTable
 	{
 		for (int i = 0; i < table.length; i++)
 		{
-			System.out.print("["+i+"] ");
+			System.out.print("["+i+"]: ");
 			if (table[i] == null)
 			{
 				System.out.print("empty");

+ 19 - 7
Project4/src/ListPrinter.java

@@ -2,12 +2,12 @@
  * Reads student information from a file, ignoring invalid lines, and prints them in ascending order by id.
  * Inputs must be one-per-line, formatted as such: "[id] [last name]"
  * 
- * Project 2
+ * Project 4
  * 
  * @author Thomas Flucke tflucke
  * @author Lara Luu ljluu
  * 
- * @since 2015/10/22
+ * @since 2015/11/11
  * 
  * @see Student
  */
@@ -98,6 +98,7 @@ public class ListPrinter
 					{
 						System.out.println("Invalid Input");
 					}
+					in.nextLine();
 					break;
 					
 				case 'f':
@@ -106,16 +107,27 @@ public class ListPrinter
 					if(in.hasNextLong() && (findId = in.nextLong()) > 0)
 					{
 						Student dummyStudent = new Student(findId, "lastName");
-						table.find(dummyStudent);
-						
 						Student findStudent = (Student)table.find(dummyStudent);
-						System.out.println(findStudent != null? String.format("Student with id %d has been found", findId):"Student not found");
-						
+						System.out.println(findStudent != null? String.format("Student %s has been found", findStudent.toString()):"Student not found");
 					}
 					else
 					{
 						System.out.println("Invalid Input");
 					}
+					in.nextLine();
+					break;
+					
+				case 'n':
+					System.out.println(table.elementCount() + " elements in the table");
+					break;
+					
+				case 'e':
+					System.out.println(table.isEmpty()? "Table is empty":"Table is not empty");
+					break;
+					
+				case 'k':
+					table.makeEmpty();
+					System.out.println("Table is now empty");
 					break;
 					
 				case 'p':
@@ -152,7 +164,7 @@ public class ListPrinter
 		final String longFormat = "^\\+?\\d+$";
 		
 		//Breaks the input line into whitespace separated parts
-		String[] inputLineParts = line.split("\\s");
+		String[] inputLineParts = line.trim().split("\\s");
 		if(inputLineParts.length == 2 && inputLineParts[0].matches(longFormat))
 		{
 			return new Student(Long.parseLong(inputLineParts[0]), inputLineParts[1]);

+ 1 - 1
Project4/src/Student.java

@@ -6,7 +6,7 @@
  * @author Thomas Flucke tflucke
  * @author Lara Luu ljluu
  * 
- * @since 2015/11/12
+ * @since 2015/11/11
  */
 
 public class Student