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. eg01array.java (text file) - array decleration, user entry from keyboard, basic for loop; copy reference memery address (only)

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

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

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

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

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

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

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

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

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

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

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

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


Classes and Objects
(Gaddis Ch 9)

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

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

16. eg16_Rectangle.java (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. eg16_PassObject.java (text file) - demonastrate pass "reference to objects" as arguments to methods, main() is here.

17. Aggregation
eg17_Instructor.java (text file)
eg17_TextBook.java (text file)
eg17_Course.java (text file), fileds contain instance of eg17_Instructor class and eg17_TextBook class.
eg17_CourseDemo.java (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
eg18_EnumDemo.java (text file)


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

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

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

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

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

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

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

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

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


Inheritance (Gaddis Ch 11)

27. eg27_GradedActivity.java (text file) - Superclass
27. eg27_GradeDemo.java (text file) - demo eg27_GradedActivity.java
27. eg27_FinalExam.java (text file) - subclass of eg27_GradedActivity.java
27. eg27_FinalExamDemo.java (text file) - demo eg27_FinalExam.java

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. eg28_SuperClass1.java (text file) - Superclass, has a no-arg constructor that prints something
28. eg28_SubClass1.java (text file) - Subclass, also has a no-arg constructor that prints something
28. eg28_ConstructorDemo1.java (text file) - main(), create eg28_SubClass1 object, shows that super class constructor is executed first

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

Overloading Superclass Methods - same name, different signature
30. eg30_SuperClass3.java (text file) - Superclass
30. eg30_SubClass3.java (text file) - Subclass with both overloading and overriding methods
30. eg30_ShowValueDemo.java (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. eg31_GradedActivity2.java (text file) - Superclass, with a protected field
31. eg31_FinalExam2.java (text file) - Subclass, with extra method to use the protected field
31. eg31_ProtectedDemo.java (text file) - main(), instantiate the subclass

Chain of Inheritance - 3 classes inheritance
32. eg27_GradedActivity.java (text file) - Superclass, same (file) as eg27 above
32. eg32_PassFailActivity.java (text file) - Subclass, middle level
32. eg32_PassFailExam.java (text file) - Subclass, lowest leve;
32. eg32_PassFailExamDemo.java (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. eg33_ObjectMethods.java (text file) - toString and equals methods that are inherited from the Object Class.

Polymorphism and Dynamic Binding
34. eg34_Polymorphic.java (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. eg35_Student.java (text file) - abstract class
35. eg35_CompSciStudent.java (text file) - extends the abstract class, and overrides the abstract method
35. eg35_CompSciStudentDemo.java (text file) - main(), uses the eg35_CompSciStudent class

Interfaces - An interface specifies behavior for a class
36. eg36_Relatable.java (text file) - inteface (like an abstract class that has all abstract methods)
36. eg27_GradedActivity.java (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. eg37_Recursive.java (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. eg40_FibNumbers.java (text file) - recursive Fibonacci method

41. eg41_GCDdemo.java (text file) -recursive GCD calculation

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

 

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

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