Examples of smaller Java programs that do various interesting things.

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * A class to provide a simple list of integers.
  3. * List resizes automatically. Used to illustrate
  4. * various design and implementation details of
  5. * a class in Java.
  6. *
  7. * Version 1 only contains the instance variables and
  8. * the constructors
  9. * @author scottm
  10. *
  11. */
  12. public class IntListVer1 {
  13. // class constant for default size
  14. private static final int DEFAULT_CAP = 10;
  15. //instance variables
  16. private int[] iValues;
  17. private int iSize;
  18. /**
  19. * Default constructor. Creates an empty list.
  20. */
  21. public IntListVer1(){
  22. //redirect to single int constructor
  23. this(DEFAULT_CAP);
  24. //other statments could go here.
  25. }
  26. /**
  27. * Constructor to allow user of class to specify
  28. * initial capacity in case they intend to add a lot
  29. * of elements to new list. Creates an empty list.
  30. * @param initialCap > 0
  31. */
  32. public IntListVer1(int initialCap) {
  33. assert initialCap > 0 : "Violation of precondition. IntListVer1(int initialCap):"
  34. + "initialCap must be greater than 0. Value of initialCap: " + initialCap;
  35. iValues = new int[initialCap];
  36. iSize = 0;
  37. }
  38. }