Richard's Java Page

Useful Links

My simple steps to setup Java

This page contains stuff from the 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, no "class" at the back of file name


0. Setting up Programming Environment

::: video 00_001: Show File Extensions in Windows 8 / 8.1 File Explorer - introduction, set up programming environment 1.

::: video 00_002: Install Notepad++ - introduction, set up programming environment 2.

::: video 00_003: Create Screen Captures, Save As PDF Files - introduction, set up programming environment 3.

::: video 05_001: Setup Java SE JDK in Windows 8.1 --- 1.

::: video 05_002: Setup Java SE JDK in Windows 8.1 --- 2.

::: video 05_003: Setup Java SE JDK in Windows 8.1 --- 3.

Java Fundamentals (Gaddis Ch 2) - Java is case sensitive

1. (text file) - print and println (Ch 2.2), note: class and file must have same name, case sensitive.

2. (text file) - variables and literals (Ch 2.3 & Ch 2.4)

3. (text file) - arithmetic operations (Ch 2.5)

4. (text file) - conversion between primitive data types (Ch 2.7 & Ch 2.8)

5. (text file) - String class (Ch 2.9)

6. (text file) - Reading keyboard input (Ch 2.13)

7. (text file) - Use dialog box (class) for input (Ch 2.14)


Decision Structure (Gaddis Ch 3)

8. (text file) - If-Else (Ch 3.2)

9. (text file) - If-Else-If (Ch 3.4)

10. (text file) - DecimalFormat class, round to some number of decimal places (Ch 3.10) - and something interesting about working with money in Java (external site)

11. (text file) - And, Or logical operators (Ch 3.5)

12. (text file) - comparing String objects (Ch 3.6)

13. (text file) - Switch statement (Ch 3.9)

Skipping Ch 3.11 printf


Loops and Files (Gaddis Ch 4)

14. (text file) - While, and Do While statements (Ch 4.2, 4.4)

15. (text file) - While loop for input validation (Ch 4.3)

16. (text file) - For loop (Ch 4.5)

17. (text file) - Read data from a file (Ch 4.10)

18. (text file) - Write data to a file, check if file exists (Ch 4.11)

19. (text file) - Generate random numbers (Ch 4.12)

20. (text file) - Postfix increment and prefix increment (Ch 4.1)


Methods (Gaddis Ch 5)
4 types of class methods
- final methods : invariant over inheritance hierarchy
- abstract methods : base class, no implementation
- static methods : no controlling object
- other methods : base class = default implementation; derived class = override or accept unchanged

21. (text file) - Static void method

22. (text file) - Hierarchical method calls

23. (text file) - Pass arguments to a method, by value

24. (text file) - Pass arguments to a method, by reference for String (String objects is immutable, cannot be changed)

25. (text file) - method returns a value

26. (text file) - opens a file containing sales amounts for some number of days, calculates and displays the total sales


A First Look at Classes (Gaddis Ch 6)

27. (text file) - private fields, public methods, accessors, mutators
27. file) - demo Ex27_Rectangle class, main() is here
27. (text file) - demo Ex27_Rectangle class, main() is here, create 3 instances (objects) of the Ex27_Rectangle class

28. (text file) - constructors, overloading methods (and overloading constructors)
28. (text file) - demo Ex28_BankAccount class, main() is here


A First Look at GUI Applications (Gaddis Ch 7)

29. (text file) - how to use JFrame

30. (text file) - extends (inheritates) JFrame, JPanel, JLabel, JTextField, JButton
30. (text file) - demo Ex30_KiloConverterWindow, main() is here

31. (text file) - more ActionListener, changing color, JLabel
31. (text file) - demo Ex31_ColorWindow, main() is here

32. (text file) - more ActionListener, getActionCommand() method
32. (text file) - demo Ex32_EventObjectWindow, main() is here

33. (text file) - FlowLayout

34. (text file) - BorderLayout


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.

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


Exceptions (Gaddis Ch 12)

45. (text file) - throws exception, try{} catch() {}, good error example

46. (text file) - handle FileNotFoundException, try{} catch() {}, cannot be used as error example


Applets (Gaddis Ch 14)

44. (text file) - a very simple applet
44. eg44_SimpleApplet.htm - how to embed a java applet (class file) on web page
Note: check your browser's Java plugin version. You may need to javac your java file using 32-bit older version JDK.


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^2) running time
43. (text file) - main() to demo bubble sort


Links to Other Old Pages

Old Java Page (21 Days Book)

Something else Adv. Data Structure Related

More Examples Ele. Data Structure Related