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. Ex01MyFirst.java (text file) - print and println (Ch 2.2), note: class and file must have same name, case sensitive.

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

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

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

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

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

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

 

Decision Structure (Gaddis Ch 3)

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

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

10. Ex10DecimalFormat.java (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. Ex11AndOr.java (text file) - And, Or logical operators (Ch 3.5)

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

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

Skipping Ch 3.11 printf

 

Loops and Files (Gaddis Ch 4)

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

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

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

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

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

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

20. Ex20PostPre.java (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. Ex21CreditCard.java (text file) - Static void method

22. Ex22Deep.java (text file) - Hierarchical method calls

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

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

25. Ex25ValueReturn.java (text file) - method returns a value

26. Ex26SalesReport.java (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. Ex27_Rectangle.java (text file) - private fields, public methods, accessors, mutators
27. Ex27_RectangleDemo.java(text file) - demo Ex27_Rectangle class, main() is here
27. Ex27_RoomAreas.java (text file) - demo Ex27_Rectangle class, main() is here, create 3 instances (objects) of the Ex27_Rectangle class

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

 

A First Look at GUI Applications (Gaddis Ch 7)

29. Ex29_ShowWindow.java (text file) - how to use JFrame

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

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

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

33. Ex33_FlowWindow.java (text file) - FlowLayout

34. Ex34_BorderWindow.java (text file) - BorderLayout

 

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

 

Exceptions (Gaddis Ch 12)

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

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

 

Applets (Gaddis Ch 14)

44. eg44_SimpleApplet.java (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. 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^2) running time
43. eg43_BubbleSortTest.java (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