Sample Java Code

Last changed: Nov 16, 2002, 12:54 AM

    First set of java examples: Stats (Topics 1& 2 of the syllabus) Stats.java , noInputStats.java, onLineStats.java and guiStats.java are slightly modified. Check them again

A sequence of java codes improving on each other:

    firstStats.java calculate average of exams and decides on grade

    secondStats.java introduces methods to do the same thing

    noInput.java and Stats.java introduce classes and objects using the same problem (plus adds new methods for standard deviation, max and min)

    onlineStats.java shows how to use command line and enter the grades

    guiStats.java shows how to use simple graphical user interface to get the grades

    Static variables



    Employee.java is an example for demonstrating how a static variable is used to create unique ID value for each instance without the creator of the instances having to worry about it.

    The sequential and binary search algorithms and their flowcharts

    SeqSearchable.java show sequential search method. Its main method can be found in seqMain.java. This version searches a list of integers. The flowchart is at seqSearch.pdf

    bSearchable.java shows the non-recursive version of the binary search method. Its main method can be found in bMain.java. This version searches a list of words (String objects). The flowchart can be found at bSearch.pdf

    bSearchRecursive.java implements a recursive version of the binary search algorithm. The class contains only static methods and fields and has its own main method. It searches a list of integers.

    The insertion and quick sort method and their flowcharts

    InsertionSort.java is the implementation of the insertion sort algorithm. See the flowchart of this algorithm.

    The testSort.java is where the main method is to use insertionSort class.

    A second main method is is guiSort.java which uses gui for communicating data

    The recursive quicksort algorithm is given in quickSort.java. The flowchar for the pivoting part is given in pivot.pdf

    A couple of simple examples showing effects of inheritiance or fields, constructors and methods

    testConstructor.java shows how to first initialize superclass's constructors

    testOverload.java shows how inherited fields and methods work, and contrasts overloading and overriding methods.

    An extensive study of an object oriented sorting example

    The object oriented version of sort and comparable classes are provided as follows:

    First the UML information is in objectiveSorte.zargo file, download it and open it using argoUML version 0.9 or higher

    The java program generated by argoUML is modified by adding functionality in the methods. They are available in sorter.java, quickSorter.java, insertionSorter.java, comparable.java, Stringcomparable.java and integerComparable.java. I also have made a main method in Main.java which lets you test the code. Note you need to open a folder called objectiveSort (the name of the package these files belong to) and put all the .java in this folder. Read the comments in Main.java about how to compile and run these files carefully.