Java Programming Examples

Algorithms

ArrayTester.java ArrayUtil.java BinaryInsertionSortTest.java
BubbleSortTest.java GapSortTest.java InsertionSortTest.java
IntegerArray.java MergeSortTest.java QuickSort.java
SelectionSortTest.java SelectionSortWithTiming.java SimpleBubbleSortTest.java
SortAnim.java Sorting.java SortingApplet.html
StopWatch.java Timer.java timer.c

Java Basics

ArgsTest.java ArgsTest1.java ArgsTest2.java
ArrayTest.java BitwiseOps.java BooleanTest.java
CallTest.java CharTest.java CharTest1.java
CharTest2.java ConsoleTest.java ConversionTest.java
ConversionTest1D.java ConvertRational.java DanglingElses.java
DialogApp.java ExceptionTest.java FirstSample.java
GUITest.java GenericTest.java HelloUnicode.java
IfTest.java InputTest.java Labeled.java
LoopTest.java MathTest.java MethodTest.java
Parameter.java PassByTest.java PrintNumber.java
PrintfTest.java RosencrantzAndGuildenstern.java ScopeTest.java
SmartTest.java SquareInt.java SquareIntV2.java
SquareIntV3.java StringComp.java StringPooling.java
StringTest.java WrapperTest.java bite_wise_ops.txt

Game of Cards

Card.java Deck.java TestDeck.java

Clean Code

GeneratePrimes.java PrimeGenerator.java

Object Cloning

B.java CloningTest.java MyObject.java

Collections, Sets and Maps

Anagrams.java Freq.java HashCodeTest.java
MapTest.java SetTest.java dictionary.txt

Books and Library examples (part of Collections section)

Book.java BookList.java BookListHasAL.java
BookListIsAL.java BookListIsLL.java BookListIterator.java
BookListWithArray.java BookListWithLL.java Library.java
Library1.java Library2.java TestBookList.java

Collections, HashSet Implementation in TDD style

HashSet.java HashSetTest.java Set.java

Implementation of interfaces using generic algorithms

(“generic” is understood here in the sense of A. Stepanov’s STL: independent of concrete data structures, using only iterator)

ArrayIterator.java BadOperation.java Book.java
Iterator.java Library.java ListIterator.java
ListWithArray.java ListWithLL.java MyList.java
MyUtilities.java Node.java  

Default Methods in Interfaces

A.java B.java C.java

Diamond of Death no more for default methods

A.java B.java C.java
InhTest.java    

Default methods after an (unknown) upgrade of a third-party API

(based on Rafael Winterhalter Java 8 default methods can break your code)

ComplexInput.java MyInput.java SimpleInput.java
ThirdPartyClass.java    

Default methods before an upgrade of a third-party API

(based on Rafael Winterhalter Java 8 default methods can break your code)

MyInput.java SimpleInput.java ThirdPartyClass.java

Step-wise program development

FindRoots.java FindRootsWithLambda.java

Enumerated Types (enums)

Chess.java ChessTest.java Coins.java
EnumValues.java Planets.java VarOpsEnum.java

Bad enumeration “types” which rely on interface constants

Apples.java BadEnums.java Devious.java
Fruits.java Juices.java Oranges.java

Overriding equals() to get equality right

A.java TestingEquals.java

Exceptions and Errors

AnotherListOfNumbers.java AssertionTest.java BadCat.java
Cat.java ExcPerf.java ExceptionTest.java
Java7ResourcesExample.java ListOfNumbers.java ListOfNumbers2.java
MultipleFinally.java OutFile.txt WithFinally.java
WithoutFinally.java infile.txt method1.bc
method2.bc    

A small program which catches exception inside a loop

PointList.java PointTest.java XYPoint.java

Functional Programming

(will be added soon)

—|—|—

Generics (Parameterised Types)

Box.java BoxBT.java BoxDemo1.java
BoxDemo2.java BoxG.java BoxWC.java
Cell.java ClassTest.java GenMethods.java
NewGenerics.java OldGenerics.java OldGenerics1.java

Inheritance

Animal.java Cat.java ExtendShow.java
InheritanceTest.java More.java Point.java
SuperShow.java Test.java That.java

Static methods under inheritance

(hiding, not overriding)

A.java B.java C.java

Input/Output

AnotherScannerExample.java ByteCounter.java ByteCounter2.java
CountSpaces.java DirectoryLister.java FindAndReplace.java
Password.java ToHexConverter.java TranslateByte.java
UppercaseConverter.java artists.txt manifest.mf
request.txt scanner_app.jar usnumbers.txt

New Input/Output API

ReadInFile.java  

A sample program from Oracle’s offcial example set

(works with files stored inside a zip or jar file)

Demo.java README.txt src.zip
zipfs.jar    

Generating Documentation with javadoc

(unzip the clock_doc.zip file and follow the instructions in README)

clock_doc.zip  

UI and Graphics programming with JavaFX

BuilderAtWork.java ComboTransitions.java HelloWorld.java
KeyboardExample.java LectureMayFirstDemo.java MasterSlaveWithBinding.java
MasterSlaveWithoutBinding.java Morphism.java MouseEvents.java
RollingCircles.java SimpleShapesAndTransitions.java SliderDemo.java
WorkingWithTheSceneGraph.java    

JavaFX animation examples (from the official Oracle’s set)

Animation.java AnimationBooleanInterpolator.java AnimationInterpolator.java
TimelineEvents.java    

Styled JavaFX application

Clock.java clock.css

Basic and not so basic examples of JavaFX binding

BindingDemo.java BindingDemo2.java BindingDemo3.java

Counter application with command-line, Swing and JavaFX clients

CommandLineCounter.java Counter.java JavaFXCounter.java
SwingCounter.java    

Event filtering example in JavaFX

(black art, don’t try this at home)

DraggablePanelsExample.java  

Testing with JUnit

Complex.java Point.java TestComplex.java
TestPoint.java    

Old testing (without JUnit framework) technique

Point.java  

Lambda expressions and method references

DefaultMethodTest.java EffectivelyFinal.java FindRootsWithLambda.java
Hello.java Lambdas.java LazyStream.java
OldHello.java OuterScope.java PseudoRandomGenerator.java
RecursiveLambda.java Sorting.java StaticRecursiveLambda.java

Miscellaneous examples

Example.java Foo.java NonPureMethod.java
Parameter.java Planet.java RegexTestHarness.java
Reuse.java ScannerExample.java ScopeTest.java
StaticParameter.java StringBusiness.java StringExample.java

Module System: module declaration, access control etc

Entropy.java README

Object-oriented programming

ArticlesAndPronouns.java Body.java Car.java
CarTest.java ComparePeople.java Employee.java
EmployeeTest.java Manager.java Overdoing.java
OverloadTest.java Person.java PersonTest.java
Shtuka.java StaticEffect.java Student.java
StudentTest.java TestShtuka.java X.java
Y.java    

Covariant Overriding examples

Main.java MyComplexNumber.java MyNumber.java

Package structure and access

Alongsider.java Example.java Foo.java
Makefile Protected.java README
SamePackageDweller.java SubProtected.java  

Polymorphism examples

Circle.java Main.java Point.java
Shape.java Square.java  

Reflections

A.java B.java ClassBrowser.java
ClassSpy.java Foo.java HelloWorld.java
ListParams.java README RunTests.java
SubHelloWorld.java Test.java TestHelloWorld.java

Splotch JavaFX example

(run make compile to compile, make run to run)

Makefile splotch.zip

Sources for Splotch program

(keep them inside splotch subdirectory)

Point.java Smoother.java Splotch.java

Streams examples

CaesarCypher.java Dish.java LazyEagerBenchmark.java
LazyStreams.java ReductionExamples.java StreamBasic.java
StreamFactoring.java StreamVsCollection.java String2Chars.java

Swing GUI programming examples, obsolete

(to run the applet, compile HelloWorldApplet.java and open appletviewer HelloWorld.html; running in a browser is likely disabled nowadays)

Counter.java HelloWorld.html HelloWorldApplet.java
MouseSpy.java TestCounter.java clock1.jar
clock3.jar    

“Classic” Swing application with MVC architecture

AnalogClockPanel.java Clock.java Controller.java
Model.java View.java  

Counter application of a certain (naive) kind

Counter.java CounterApplication.java README.txt

Counter application of another (naive) kind

Counter.java CounterApplication.java IncrementButtonHandler.java
README.txt ResetButtonHandler.java  

Counter application of a yet another (naive) kind

Counter.java CounterApplication.java IncrementButtonHandler.java
README.txt ResetButtonHandler.java  

Counter application with some adornments

Counter.java CounterApplication.java IncrementButtonHandler.java
README.txt ResetButtonHandler.java ResetItemHandler.java

Threads examples

HelloRunnable.java HelloThread.java PingPong.java
SimpleThreads.java SleepMessages.java  

Trees and tries

(will be added later)

—|—|—

Updated:  26 May 2017/ Responsible Officer:  Head of School/ Page Contact:  Alexei Khorev