Lab 6

Due Friday, 4/6/2012, 5:00 p.m.

NOTE: 4/6 is a holiday, so help will NOT be available that day. We recommend that you get the lab done by Thursday, so that you can avail yourself of help from TA and professors.

NOTE #2: There were some bugs in initial OrderedStringList.java code, bugs fixed and changes uploaded on 3/30 @ 1:30 pm. If you have downloaded an earlier version, please redownload the revised version.

For your 6th lab, you will use inheritance to change the behavior of a data structure, and you will create some of the data structures that will be used for the next project.

    1. Ordered String List
    2. Consider the following OrderedStringList class, which implements an ordered string list (available to download here: OrderedStringList.java)
    3. import java.util.NoSuchElementException;
    4. public class OrderedStringList
    5. {
    6. String data[];
    7. int length;
    8. protected static final int DEFAULT_LENGTH = 10;
    9. public OrderedStringList()
    10. {
    11. data = new String[DEFAULT_LENGTH];
    12. length = 0;
    13. }
    14. public OrderedStringList(int initialAllocation)
    15. {
    16. data = new String[initialAllocation];
    17. length = 0;
    18. }
    19. public String minString()
    20. {
    21. if (length > 0)
    22. {
    23. return data[0];
    24. }
    25. else
    26. {
    27. throw new NoSuchElementException();
    28. }
    29. }
    30. public String maxString()
    31. {
    32. if (length > 0)
    33. {
    34. return data[length - 1];
    35. }
    36. else
    37. {
    38. throw new NoSuchElementException();
    39. }
    40. }
    41. public void add(String element)
    42. {
    43. if (data.length == length)
    44. {
    45. growData();
    46. }
    47. int insertIndex = length;
    48. while (insertIndex > 0 && element.compareTo(data[insertIndex-1]) < 0)
    49. {
    50. data[insertIndex] = data[insertIndex - 1];
    51. insertIndex--;
    52. }
    53. data[insertIndex] = element;
    54. length++;
    55. }
    56. protected void growData()
    57. {
    58. String newData[] = new String[data.length * 2];
    59. for (int i = 0; i < data.length; i++)
    60. {
    61. newData[i] = data[i];
    62. }
    63. data = newData;
    64. }
    65. public String toString()
    66. {
    67. String contents;
    68. if (length == 0)
    69. {
    70. contents = "";
    71. }
    72. else
    73. {
    74. contents = data[0];
    75. }
    76. for (int i = 1; i < length; i++)
    77. {
    78. contents += ", " + data[i];
    79. }
    80. return "[" + contents + "]";
    81. }
    82. String get(int index)
    83. {
    84. if (index < 0 && index >= length)
    85. {
    86. throw new IndexOutOfBoundsException();
    87. }
    88. return data[index];
    89. }
    90. void delete(int index)
    91. {
    92. length = length - 1;
    93. for(int i = index; i < length; i++)
    94. {
    95. data[i] = data[i+1];
    96. }
    97. }
    98. }
    99. This class implements an ordered string list. Strings can be added to the list (which will be added not to the end of the list, but at the appropriate place for the list to remain sorted), the largest and smallest element can be retrieved from the list, the string at a specific index can be deleted, and so on. For the first part of your lab you will write a class ReversedOrderedStringList, which will extend OrderedStringList. Your reverse ordered string list will be the same as the standard ordered string list, except that elements will stored in inverse sorted order.
    100. So, if we did:
    101. ReversedOrderedStringList list = new ReversedOrderedStringList();
    102. list.add("cat");
    103. list.add("ant");
    104. list.add("zoo");
    105. then list.get(0) would return "zoo", list.get(1) would return "cat", and list.get(2) would return "ant"
    106. likewise, list.minString() would return "ant", list.maxString() would return "zoo", and list.toString() would return
    107. "[zoo, cat, ant]"
    108. Your ReverseOrderedStringList must extend the given OrderedStringList, which must not be modified. All of the public methods defined in OrderedStringList must work properly for your ReverersedOrderedStringList. For full credit, reuse as much of the code in OrderedStringList as possible.
    109. For the second part of this lab, you will code the data structures for a Clue-like game (the next project will involve coding the rest of the game logic). There are 3 types of clue cards: Weapon cards, Location cards, and Person cards. You will define an abstract Card class, with three subclasses, PersonCard, WeaponCard, and LocationCard. In addition, you will create a Hand class, which holds a list of cards. Many (but not all!) of the required functionality can be tested using the HandTester class, included below. Your code needs to define all of the methods exactly as defined in the following documentation. Use the exact same names (case matters!), the same modifiers (public, abstract, etc.) See your professor or TA if you have any questions!
    110. Required classes:
      1. All documentation can be found in one place (with frames) here

Submission

Please submit your work in the following SVN directories:

For ReveresedOrderedStringList:

https://www.cs.usfca.edu/svn/<username>/cs112/lab6

For card game:

https://www.cs.usfca.edu/svn/<username>/cs112/project3