Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. package HW5;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Scanner;
  6.  
  7. /**
  8. A table for lookups and reverse lookups
  9. */
  10. public class LookupTable
  11. {
  12. private ArrayList<Item> byName;
  13. private ArrayList<Item> byNumber;
  14.  
  15. /**
  16. Constructs a LookupTable object.
  17. */
  18. public LookupTable()
  19. {
  20. byName = new ArrayList<Item>();
  21. byNumber = new ArrayList<Item>();
  22. }
  23.  
  24. /**
  25. Reads name and number pairs from the Scanner
  26. and adds them to the byName and byNumber array lists.
  27. @param in the scanner for reading the input
  28. */
  29. public void read(Scanner in)
  30. {
  31. while (in.hasNextLine())
  32. {
  33. // Read a line containing the name
  34. String name = new String(in.nextLine());
  35.  
  36. // Read a line containing the number
  37. String number = new String(in.nextLine());
  38.  
  39. // Store the name and number in the byName array list
  40. Item Name = new Item(name, number);
  41. byName.add(Name);
  42.  
  43. // Store the number and name in the byNumber array list
  44. Item Number = new Item(number, name);
  45. byNumber.add(Number);
  46. }
  47.  
  48. // Sort the byName Items so we can binary search
  49. Collections.sort(byName);
  50. // Sort the byNumber Items so we can binary search
  51. Collections.sort(byNumber);
  52. }
  53.  
  54. /**
  55. Looks up an item in the table.
  56. @param k the key to find
  57. @return the value with the given key, or null if no
  58. such item was found.
  59. */
  60. public String lookup(String k)
  61. {
  62. // Use the Collections.binarySearch() method to find the
  63. // position of the matching name in the byName array list.
  64. // Return null if position is less than 0 (not found).
  65. // Otherwise, return the number for the found name.
  66.  
  67. }
  68.  
  69. /**
  70. Looks up an item in the table.
  71. @param v the value to find
  72. @return the key with the given value, or null if no
  73. such item was found.
  74. */
  75. public String reverseLookup(String v)
  76. {
  77. // Use the Collections.binarySearch() method to find the
  78. // position of the matching number in the byNumber array list.
  79. // Return null if position is less than 0 (not found).
  80. // Otherwise, return the name for the found number.
  81.  
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement