More Examples, Ele. Data Structure Related

This page contains elementary data structure related stuff (from Gaddis book, very good book for Java + Data Structure)
Note: from cmd prompt
javac *.java //compile and creates class file
java * //run the created class file

Array and the ArrayList Class (Gaddis Ch 8)

1. (text file) - array decleration, user entry from keyboard, basic for loop; copy reference memery address (only)

2. (text file) - another way to declare array

3. (text file) - "length" field, user entry from keyboard, enhanced for loop

4. (text file) - pass individual elements as arguments to a method

5. (text file) - pass an array as argument to method (pass by reference)

6. (text file) - a example, a class, using array to keep track to sales data, calculate total, average, highest and lowest amount, no main().
6. (text file) - uses, test and display, main() is here.

7. (text file) - how a reference to an array can be returned from a method

8. (text file) - array of strings, accessing two arrays

9. (text file) - 2D array, input from user, total sum

10. (text file) - 2D array, argument to methods, length

11. (text file) - pass arguments through command line

12. (text file) - ArrayList class, add, remove, insert

13. (text file) - <String> ArrayList to hold strings only

Classes and Objects
(Gaddis Ch 9)

14. (text file) - static (field for all instances) vs normal field (instance field)
14. (text file) - use, test display, main() is here

15. (text file) - static method, no instance needed to execute the method
15. (text file) - use, test and display, main() is here

16. (text file) - Rectangle class used in Ch 7, need to to demonastrate pass "reference to objects" as arguments to methods, return object from method, also toString() method - a default way to display stuff, also equals() method - a way to compare objects
16. (text file) - demonastrate pass "reference to objects" as arguments to methods, main() is here.

17. Aggregation (text file) (text file) (text file), fileds contain instance of eg17_Instructor class and eg17_TextBook class. (text file), main() is here
To show the concept of "aggregation".
The fields of "eg17_Course" class use instance of "eg17_TextBook" class and instance of "eg17_Instructor" class. "eg17_CourseDemo" contains main(), calls "eg17_Course" class.

18. Enumerated Types - a set of predefined values (text file)

Text Processing and More about Wrapper Classes
(Gaddis Ch 10)

19. (text file) - Character class, wrapper class for char data type, static methods of Character class

20. (text file) - Character class,static methods, isLetter, isDigit

21. (text file) - Character class,static methods, toLowerCase, toUpperCase

22. (text file) - a test program for lots of String class methods

23. (text file) - use startsWith method to search a array of strings using a partial string

24. (text file) - display the number of letters, digits, and whitespace character in a string

25. (text file) - StringBuffer class - more methods for string operations
25. (text file) - use the class above

26. (text file) - StringTokenizer class - tokenize a string
26. (text file) - use the class above

Inheritance (Gaddis Ch 11)

27. (text file) - Superclass
27. (text file) - demo
27. (text file) - subclass of
27. (text file) - demo

The super class constructor always executes before the subclass constructor. The super key word refers to an object's superclass. We can use super to call a superclass constructor.
28. (text file) - Superclass, has a no-arg constructor that prints something
28. (text file) - Subclass, also has a no-arg constructor that prints something
28. (text file) - main(), create eg28_SubClass1 object, shows that super class constructor is executed first

Overriding Superclass Methods - same name, same signature
29. (text file) - Superclass, same (file) as eg27 above
29. (text file) - setScore method overrides the superclass setScore method
29. (text file) - main(), demo

Overloading Superclass Methods - same name, different signature
30. (text file) - Superclass
30. (text file) - Subclass with both overloading and overriding methods
30. (text file) - main(), instantiate the subclass

Protected Members - protected members of a class may be accessed by methods in a subclass, and by methods in the same package as the class
31. (text file) - Superclass, with a protected field
31. (text file) - Subclass, with extra method to use the protected field
31. (text file) - main(), instantiate the subclass

Chain of Inheritance - 3 classes inheritance
32. (text file) - Superclass, same (file) as eg27 above
32. (text file) - Subclass, middle level
32. (text file) - Subclass, lowest leve;
32. (text file) - main(), instantiate the lowest leve subclass
note: super statement that calls a superclass constructor must be in the first statement;
note: if a superclass does have a default constructor (Java automatically insert this) and does not have a no-arg constructor, then a class that inherits from it must call one of the constructors that the superclass does have.

The Object Class - when a class does not use extends key word (to inherit), Java automatically extends it from the Object class.
33. (text file) - toString and equals methods that are inherited from the Object Class.

Polymorphism and Dynamic Binding
34. (text file)
- Polymorphism: A superclass reference variable can reference objects of a subclass.
- Dynamic binding: Java performs dynamic binding or late binding when a variable contains a polymorphic reference.

Abstract Classes and Abstract Methods - an abstract class is not instantiated, but other classes extend it, an abstract methods must be overriden in a subclass
35. (text file) - abstract class
35. (text file) - extends the abstract class, and overrides the abstract method
35. (text file) - main(), uses the eg35_CompSciStudent class

Interfaces - An interface specifies behavior for a class
36. (text file) - inteface (like an abstract class that has all abstract methods)
36. (text file) - Superclass, same (file) as eg27 above
36. eg36_FinalExam3 (text file) - extends eg27_GradedActivity implements eg36_Relatable, overrides all abstract methods in interface
36. eg36_InterfaceDemo (text file) - main(), uses the eg36_FinalExam3 class


Recursion (Gaddis Ch 15)

37. (text file) - basic recursion example and backtracking example

38. eg38_FactorialDemo (text file) - calculate factorial

39. eg39_RangeSum (text file) - calculate sum of elements in an array

40. (text file) - recursive Fibonacci method

41. (text file) -recursive GCD calculation

42. (text file) - Tower of Hanoi (of course it is recursive)


Sorting, Searching, and Algorithm Analysis (Gaddis Ch 16)

43. (text file) - bubble sort, O(n) running time
43. (text file) - main() to demo bubble sort