Examples of smaller Java programs that do various interesting things.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * A class to provide a simple list.
  3. * List resizes automatically. Used to illustrate
  4. * various design and implementation details of
  5. * a class in Java.
  6. *
  7. * @author scottm
  8. *
  9. */
  10. public class GenericList{
  11. // class constant for default size
  12. private static final int DEFAULT_CAP = 10;
  13. //instance variables
  14. // iValues store the elements of the list and
  15. // may have extra capacity
  16. private Object[] iValues;
  17. private int iSize;
  18. /**
  19. * Default add method. Add x to the end of this IntList.
  20. * Size of the list goes up by 1.
  21. * @param x The value to add to the end of this list.
  22. */
  23. public void add(Object x){
  24. insert(iSize, x);
  25. }
  26. public Object get(int pos){
  27. return iValues[pos];
  28. }
  29. /**
  30. * Insert obj at position pos.
  31. * post: get(pos) = x, size() = old size() + 1
  32. * @param pos 0 <= pos <= size()
  33. * @param obj The element to add.
  34. */
  35. public void insert(int pos, Object obj){
  36. ensureCapcity();
  37. for(int i = iSize; i > pos; i--){
  38. iValues[i] = iValues[i - 1];
  39. }
  40. iValues[pos] = obj;
  41. iSize++;
  42. }
  43. public Object remove(int pos){
  44. Object removedValue = iValues[pos];
  45. for(int i = pos; i < iSize - 1; i++)
  46. iValues[i] = iValues[i + 1];
  47. iValues[iSize - 1] = null;
  48. iSize--;
  49. return removedValue;
  50. }
  51. private void ensureCapcity(){
  52. // is there extra capacity available?
  53. // if not, resize
  54. if(iSize == iValues.length)
  55. resize();
  56. }
  57. public int size(){
  58. return iSize;
  59. }
  60. // resize internal storage container by a factor of 2
  61. private void resize() {
  62. Object[] temp = new Object[iValues.length * 2];
  63. System.arraycopy(iValues, 0, temp, 0, iValues.length);
  64. iValues = temp;
  65. }
  66. /**
  67. * Return a String version of this list. Size and
  68. * elements included.
  69. */
  70. public String toString(){
  71. // we could make this more effecient by using a StringBuffer.
  72. // See alternative version
  73. String result = "size: " + iSize + ", elements: [";
  74. for(int i = 0; i < iSize - 1; i++)
  75. result += iValues[i].toString() + ", ";
  76. if(iSize > 0 )
  77. result += iValues[iSize - 1];
  78. result += "]";
  79. return result;
  80. }
  81. // Would not really have this and toString available
  82. // both included just for testing
  83. public String toStringUsingStringBuffer(){
  84. StringBuffer result = new StringBuffer();
  85. result.append( "size: " );
  86. result.append( iSize );
  87. result.append(", elements: [");
  88. for(int i = 0; i < iSize - 1; i++){
  89. result.append(iValues[i]);
  90. result.append(", ");
  91. }
  92. if( iSize > 0 )
  93. result.append(iValues[iSize - 1]);
  94. result.append("]");
  95. return result.toString();
  96. }
  97. /**
  98. * Default constructor. Creates an empty list.
  99. */
  100. public GenericList(){
  101. //redirect to single int constructor
  102. this(DEFAULT_CAP);
  103. //other statments could go here.
  104. }
  105. /**
  106. * Constructor to allow user of class to specify
  107. * initial capacity in case they intend to add a lot
  108. * of elements to new list. Creates an empty list.
  109. * @param initialCap > 0
  110. */
  111. public GenericList(int initialCap) {
  112. assert initialCap > 0 : "Violation of precondition. IntListVer1(int initialCap):"
  113. + "initialCap must be greater than 0. Value of initialCap: " + initialCap;
  114. iValues = new Object[initialCap];
  115. iSize = 0;
  116. }
  117. /**
  118. * Return true if this IntList is equal to other.<br>
  119. * pre: none
  120. * @param other The object to comapre to this
  121. * @return true if other is a non null, IntList object
  122. * that is the same size as this IntList and has the
  123. * same elements in the same order, false otherwise.
  124. */
  125. public boolean equals(Object other){
  126. boolean result;
  127. if(other == null)
  128. // we know this is not null so can't be equal
  129. result = false;
  130. else if(this == other)
  131. // quick check if this and other refer to same IntList object
  132. result = true;
  133. else if( this.getClass() != other.getClass() )
  134. // other is not an IntList they can't be equal
  135. result = false;
  136. else{
  137. // other is not null and refers to an IntList
  138. GenericList otherList = (GenericList)other;
  139. result = this.size() == otherList.size();
  140. int i = 0;
  141. while(i < iSize && result){
  142. result = this.iValues[i].equals( otherList.iValues[i] );
  143. i++;
  144. }
  145. }
  146. return result;
  147. }
  148. }