www.neiljohan.com

Java source code examples
Java Welcome to my Java page. Here you will find some very simple Java source code done by me as part of my Computer Science course.

This is not meant to be a tutorial on Java, the page is here to give you some examples from which you can learn from.

It is very simple code (getting more complicated as you go down the page) with very little error checking but it may help you to figure out how some of the basics of Java. Be aware that some of the files have the same names but they are actually different so make sure you download all the files for that particular program. I have written a Java MIDP game called Atomic which you may be interested in looking at.

Simple programs | Slightly more complicated | Java interfaces | Data structures | MVC | GUI | Code writing interview questions

Simple Programs

Program Name

Description

Further Technical Info

DayFromDate.java Program To Calculate the day from the date using Zeller's congruence. Calls external classes. [requires NeilClass.java]
DaysInMonth.java Type in the number of days in the month and on which day the month starts and it will produce a calender for that month.  Get's input, uses For loops and If statements.
EmailSurname.java Type in an email address such as bill.gates@microsoft.com and it will extract the 'gates' bit of it Uses indexOf and substring to extract the Surname.
Factorial.java Produces factorials by using the switch statement. Demonstrates the use of the switch statement.
LeapYear.java Prints out a list of which years are leap years. Demonstrated 'for' and 'if, else' constructs.
ListAverage.java Inputs a list of numbers and then gives you the average of them. Uses While, For and If constructs.
Midnight.java Type in the number of minutes past midnight and this will tell you what the time is Simple input & modula arithmetic.
NewtonRaphson.java
NewtonRaphsonClass.java
Calculates the square root of a number using the Newton Raphson formula. Calls internal methods & external classes. [requires NeilClass.java]
ReverseAlt.java Reverses the letters of alternate words. Demonstrated string tokeniser
SimilarWords.java Compares two words and tells you how many of the letters are different. Inputs two strings and compares them using charAt.
Stamps.java Type in the weight of your parcel and it will tell you how much you need to spend on stamps. Simple If constructs
TempChart.java Outputs a chart of temperatures in Celcius and Fahrenheit. Uses a for loop to produce the chart.
WordOrder.java Type in two words and the program will tell in which order they come. Uses compareTo and StringTokeniser

Slightly More Complicated Programs

The non linked programs are coming soon.
Most of these need NeilClass.java
 

Program Name

Description

Further Technical Info

BinomialCoefficient.java
needs GetBiClass.java
Calculated The Binomial Coefficient of two numbers. Does some error checking with pre & post conditions.
FileCopy.java Copies and displays the contents of a file. Copies the data into a byte array before outputing it to the destination file. As it copies the data one byte at a time it can be a bit slow. Contains no error checking.
FileCopyEC.java Same as above. Same as above but contains the propper error checking code.
FileDisplay.java Displays the contents of a file. Contains no error checking.
FileEncrypt.java Encrypts a file Uses very simple XOR encryption. Contains no error checking.
GCD.java
needs GCDClass.java
Calculates the GCD using a recursive functions. Does some error checking with pre & post conditions.
NewtonRaphson.java
needs NewtonRaphsonClass.java
Calculates the square root of a number using the Newton Raphson formula.  Like the previous Newton Raphson program but does some error checking with pre & post conditions.
Nought.java A very simple noughts & crosses game. Coded very craply I have to admit but I didn't have much time to do it in. Needs NeilClass.java.
Connect4.java A simple Connect 4 game. Adapted from the Noughts & Crosses Java code but it is coded much better. It is also a far superior game.
CarGoatSim.java Simulates and solves the Car & Goats quiz problem. Creates random numbers.

Java Interfaces

Some programs which are the same as above but which use Interfaces plus some new ones. Download Java Interface Files(4kb)
 

Program Name

Description

Further Technical Info

Bermuda Triangle
PositionMain.java
PositionImpl.java
Position.java
Uses a formula to work out if co-ordinates are in the Bermuda triangle & prints out a grid showing the triangle. Demonstrates Interfaces
Binomial.java
needs GetBiClass.java & BinaryInterface.java
Calculated The Binomial Coefficient of two numbers. Does some error checking with pre & post conditions. 
GCD.java
needs GCDClass.java & GCDInterface.java
Calculates the GCD using a recursive functions. Does some error checking with pre & post conditions.
NewtonRaphson.java
needs NewtonRaphsonClass.java & NewtonInterface.java
Calculates the square root of a number using the Newton Raphson formula.  Like the previous Newton Raphson program but does some error checking with pre & post conditions.
Binary.java
BinaryClass.java
BinaryInterface.java
Binary Simulator. Converts from denary to binary & back. Adds 2 binary numbers and works out the 2's complement of a binary number. Uses Interfaces and arrays.

Data Structures

Program Name

Description

Further Technical Info

SyntaxCheck.java
StackImpl.java
Stack.java
Syntax Checker - You input an expression such as ({hello})[] and it will tell you if the "()", "{}" and "[]" match. Demonstrates how to create and use a stack.
ReadList.java
Position.java
PositionImpl.java
Pos.dat
Flight Planner, reads positions from a file into an Array List and then allows you to move through them, view them and change the values. Shows how to use Java's Array List structure.
FigureProg.java
Figure.java
Circle.java
Rectangle.java
data.dat
Reads objects from a file and puts them in a Hash Set. Uses the Hash Set data structure and demonstrates its iterator.
Flight Planner
TranslateFlight.java
Position.java
PositionImpl.java
Flight.dat
Points.dat
This program reads a load of Positions into a HashMap. It then reads a flight plan from a second file and retrieves the Positions for each of the destinations from the HashSet Uses a Hash Map and shows how to get specific items from it.
Patience
CardTest.java
Pack.java
PackImpl.java
Patience.java
PlayingCard.java
PlayingCardImpl.java
NeilClass.java
Patience Game, Uses a generic Playing Card and Pack object to create a simple card game. The ADT is designed to be as generic as possible so any card game could be created using it. Uses List Array Structure, shows how to create a simple to use generic ADT.
Linked List
DoubleClient.java
DoubleNode.java
DoubleList.java
Custom written Linked List implementation and a program to demonstrate it.

MVC

Program Name

Description

Further Technical Info

PersonArrayQueueProg.java
Queue.java
ArrayQueue.java
OutputQueue.java
Person.java
PersonImpl.java
Date.java
DateImpl.java
Lets you add people to a population and remove them. Demonstrates the MVC viewer system. However this example does not demonstrate it very well as it only uses one viewer.

Graphical User Interfaces

These programs need JDK 1.2

Program Name

Description

Further Technical Info

Calc.java
CalcWindow.java
Add or subtract two numbers from a simple GUI. Sets up a GUI and gets input from it.
Bermuda Triangle GUI PositionMain.java
PersonInputBox.java
PositionForm.java.java
PositionFormJFrame.java
PositionFormProg.java
PositionInputBox.java
Position.java
PositionImpl.java
Inputs the Bermuda Triangle's Co-ordinates from 3 pop up dialogue boxes and then displays a simple representation of the triangle. Sets up a GUI and gets input from it.

Code writing interview questions

If you want to go a bit more advanced here are some links to C++ interview code writing questions. The C++ code linked here is fairly similar to what the Java source code would look like. It wouldn't be hard to convert it into Java code if you needed to.

Getting the Java examples to work

  1. Compile the .java file into a .class file by typing "javac whatever.java".
  2.  Then run it by typing "java whatever"

If the programs need NeilClass.java then this must be in the same directory as the Java program which is being compiled. These programs were written using emacs on UNIX.

Related Links

  • PhotoStamper - simple command line utility written in Java that takes a JPEG photo and then adds a date/time stamp to the photo. Includes Java source.
  • Atomic - Atomic is a J2ME (mobile version of Java) game where you have to take over the board with your atoms. Playable on most mobile phones.
  • Reverse Engineering Project - tool written in Java to convert Java source / class files into UML.

neiljohan.com
Copyright © 1997 - 2014

Privacy policy
Hosted by 1&1