Sample Java Code

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

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

A sequence of java codes improving on each other: calculate average of exams and decides on grade introduces methods to do the same thing and introduce classes and objects using the same problem (plus adds new methods for standard deviation, max and min) shows how to use command line and enter the grades shows how to use simple graphical user interface to get the grades

    Static variables 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 show sequential search method. Its main method can be found in This version searches a list of integers. The flowchart is at seqSearch.pdf shows the non-recursive version of the binary search method. Its main method can be found in This version searches a list of words (String objects). The flowchart can be found at bSearch.pdf 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 is the implementation of the insertion sort algorithm. See the flowchart of this algorithm.

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

    A second main method is is which uses gui for communicating data

    The recursive quicksort algorithm is given in 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 shows how to first initialize superclass's constructors 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,,,, and I also have made a main method in 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 about how to compile and run these files carefully.