by Jim Krumm
Click here to go to the Computer Science Department Home Page for Casper College at www.caspercomsci.com.
Below are some projects that we have made in our Java Programming Class COSC2406 and in our computer club. Many of the programs are nice because several are saved in their incremental steps so that you can see how they were logically developed and their source code examples can be used as tutorials. Some of these programs are not really finished, though much of the really messy work is done, and quite a few important issues handled. Hopefully they will give you a place to start and lead you on an adventure.
Source Code Examples of Java Applications and Applets:
7. CRCW Parallel Processing Model Simulation
8. The Mod Converter Converts from Different Bases to Base 10 and back
11. Drawing Multicolored Stars with Lines Applets
12. Happy Face Animated Applet
15. A Java Program Which Makes System Calls to Execute Programs from DOS
16. The Visual Address Book Tutorial which uses an Access 2008 Database
17. Setting up a Visual Java Address Book Program Which Accesses A MySQL Database
18. Basic Trig Functions Calculated Using the McLauren Series
19. Babylonian Method for Computing the Square Root of a Number
20. An Applet that Displays up to 4000 Numbers in the Fibonacci Sequence
If you lose control of your console running a java program you can press Ctrl-C to close your program. If you require more memory than Windows wants to provide for a high memory program you can run a java console program called myprogram (provided you have 1450m of Xmx and 256m Xms memory):
java -Xms256m -Xmx1450m myprogram
If you wish to determine which version of java you are running enter this at the console:
java -version
If you wish to compile a file called myfile.java at the console:
javac myfile.java
If you wish to run a console application called myfile from the command line:
java myfile
To see how to run an applet from the command line first make a no frills web page called lines5.html which has the following code:
<!DOCTYPE HTML><HTML><HEAD></HEAD><BODY> |
Then make an applet called lines5.java with this code:
import java.awt.Graphics; |
To run the above applet and web page from the command line enter:
c:\>appletviewer lines5.html |
Click here if you would like to obtain the source of a black jack program or click here to run the Black Jack program. This project shows each of the 16 steps taken in developing this game of blackjack. The package includes playing cards and is written as applets. The code shows how to use java's AWT, how to display an image, how to use actionlistening to make a button work, and how to build a logical stepped construct for making a game. It is near completion but there are still a few things left to do.
Click here if you would like to obtain the source of the incremental steps developing a graphing program which allows you to input graph linear and quadratic equations or click here to run the Graphing Applet program.
Click here for the source to the above spiral applet or click here to run Spiral Graphic program. It is interesting in that it uses sine and cosine to create this animation which gives the illusion of a spiral cave.
Click here for the source and images to a Chess game or click here to see our Chess Applet. The game has been created in incremental steps, evolving from simple to complex, saved in about 35 evolving programs contained in the zipped file. It now has simple rules for all the pieces, and checking, and pawn promotion, castling. A side from possible bugs missed, it does not detect if a castle is attacked prohibiting castling, and checkmate. It was developed by a project team of Larry Blackburn, Shawn Stalder, and myself. This exciting project shows a nice demonstration of how to use java classes in arrays to handle the duplicative characteristics of each piece.
Click here if you would like to obtain a the source in the development of Battleship program written as a console application in java. Again, this game is broken down into basic steps and developed into a working game.
The CRCW Parallel Processing Model Simulation
Click here if you would like to see an implementation of a console application simulation of the CRCW parallel processing model assuming n^2 processors used to sort n numbers (no duplicate values allowed). Very fast, very inefficient, and sort of interesting, especially to all you algorithm die hards.
Programmed by: Shawn C. Stalder Programming with Java Spring 2005 |
![]() |
View Source for Conversion from base 10
View Source for Conversion to base 10
The Titration Program by Eric Mechalke:
Eric is a Chemistry Professor at Casper College. To view the source for Eric's Titration program click here. To can visit Eric's web page click on http://www.drmechalke.com/. |
![]() |
|---|
Drawing Multicolored Stars with Lines Applets by Steve Tomasini
Steve developed these for fun. If you click on the pictures they will generate a Java applet running each. Click here for the four pointed star's source code and click here for the six pointed stars source code. Steve is a Computer Science Major at Casper College working on his associate's degree. He made these programs after being in our Java Course after just a couple of weeks.
Animated Happy Face Applet by Marshall Jefcoat
Marshall Jefcoat made this program for class to play with graphics and to show how to run a simple java animation. He has something pretty unique here. If you click on the picture it will run the animation. Click here for the source code for his program. Marshall is a student at Casper College working on his Associate's of Science in Computer Science. He is very creative and has had a couple of his books published in his series the "Adventures of Dogboy". To learn more about Marshall and the books he has written click on this link "Adventures of Dogboy" and skip the intro.
This program is a simple applet which shows how to show a display a picture in applet, triggered by a button working with a textfield. Click here for the source code for his program or click here for a zipped up copy of source and image files. You can also click on the program above to run the applet.
SnowBoard Free Project by Bryon Malody based on Jim Krumm's and Christian Simon's earlier Project
Below SnowBoard Free Project, initially developed by James Krumm and Christian Simon, but improved and refined by Bryon Malody. The program uses page buffering and comes with graphics and sound effects. It comes with graphics included. If you want a copy of the program and the graphics in the file click here. If you have some improvements please email me at jkrumm@caspercollege.edu.
A Java Program Which Makes System Calls to Execute Programs from DOS
The following program will call a Console or DOS program written in the Windows XP environment. To get this program to work put a program called helloworld.exe perhaps compiled in C++ in the same directory as myclass.java. Click here for a copy of a program helloword.exe made in C++ to put in the directory with the file myclass.java.
import java.io.IOException; } |
How to Create a Java Visual Application that Links to an Access 2008 Database in Windows XP and Java: The Address Book Project
The following shows how to create an Access 2008 database controlled by a visual Java Application. To get a copy of the source code and the databases created using Access 2008 and Access 2003 format click here. To view the source code click here: copy of the source AddressBookDataBase.java.

A. Make the Database
Open Access (Access 2008 in this tutorial, but it is similar to Access 2003) and create a database called Address in your root directory on your C: drive. Save this database to your root directory. I do not advise normally doing this on a routine basis. To do this you will first have to go the top left corner of Access and click on the colored circle with the 4 squares on it to trigger the new option in the menu as shown below:
,
Click on the browse Button As shown below:

Navigate to your root directory as shown below and save the file as Address.accdb (if you have Access 2008) or Address.mdb (if you have Access 2003).

In the first column, right click at the very top of the column (where it says add a new field) and select rename column and rename this column Last (for last name). Leave the ID column alone. If you are using Access 2003 you won't see an ID column and when you finish, the last thing you will be asked is if you wish a numerical key to be created, say yes.

Do not rename the ID column. Rename the next column First and the next Phone. If you make any mistakes you probably will want to start over. It is often easier to recreate the database and make it perfect than it is to change the code. Enter some initial data in the table which could look as follows:

Next go to file > save > and name your table AddressTable.

At this point your database file is saved. At this point your database should look as follows:

Exit your database.
B. Make the Connection
Close Microsoft Access (a connection cannot be established with Access running). Locate the odbcad32.exe using windows explorer found in your c:\windows\system32 directory and double click on it. The ODBC Data Source Administrator program will open. Click the System tab. Click the Add Button.

A “Create New Data Source” window will open, select Microsoft Access Driver (*.mdb, accdb) from the list and click finish:

In the ODBC Microsoft Access Setup window that appears next, type myAddressBook in the field for Data Source Name and click the Select Button.

In the Select Database window that opens navigate to and select Address.accdb (if you are in Access 2008) or Address.mdb if you are using Access 2003 and click OK.

Then click OK in the ODBC Microsoft Access Setup window. Finally click OK in the Microsoft ODBC Data Source Administrator Window.

This completes making the connection. Close Microsoft ODBC.
C. Make the program
Start Eclipse or TextPad (or some other java text editor). Start a new java application project/program called AddressBookDataBase which looks as follows. The following program actually works, you can simply copy and paste it as source. You can also click here for a copy of the source AddressBookDataBase.java. A couple words of caution: Before coding close Access. Also, this code is not easily adaptable to an applet. Applications have more permissions to write and save to file than do applets. It is however possible to convert this program if you know what you are doing by adjusting your computer's file permissions using the Java policy editor.
Setting up a Visual Java Address Book Program Which Accesses A MySQL Database
Click here for a for the source code to MyAddressSQL.java or here to view the source in a web page.
And download mySQL database server 6.0. After install goto to mysql in your programs and configure the server program. Install a path to mysql in windows.
...may be necessary to add mysql-connector-java-3.1.14-bin.jar to the MySQL directory in Program files and to add a reference to this directory and this file in CLASSPATH in System>Advanced>Environment Variables of the control panel
To Run MySql first navigate to the MySQL commandline Client as shown below and click it.

If your password was setup to be student type student under enter the password.
Create the myaddress4 database as follows (use the password student if you have it set to this).
Enter password: ******* Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 to server version: 4.1.22-community-nt Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> show databases; +----------+ | Database | +----------+ | mysql | | test | +----------+ 2 rows in set (0.08 sec) mysql> drop DATABASE IF EXISTS myaddressbook4; mysql> create database myaddressbook4; Query OK, 1 row affected (0.01 sec) mysql> show databases; +----------------+ | Database | +----------------+ | myaddressbook4 | | mysql | | test | +----------------+ 3 rows in set (0.00 sec) mysql> use myaddressbook4; Database changed mysql> create table phonelistTable(id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), Last VARCHAR(255), First VARCHAR(255), Phone VARCHAR(255)); Query OK, 0 rows affected (0.17 sec) mysql> insert into phonelistTable(Last,First,Phone) values ('Krumm','Jim','123-4567'); Query OK, 1 row affected (0.05 sec) mysql> insert into phonelistTable(Last,First,Phone) values ('Powers','Austin','444-4567'); Query OK, 1 row affected (0.01 sec) mysql> insert into phonelistTable(Last,First,Phone) values ('Slick','Tom','555-4567'); Query OK, 1 row affected (0.02 sec) mysql> insert into phonelistTable(Last,First,Phone) values ('Timberlake','Justin ','555-4567'); Query OK, 1 row affected (0.03 sec) mysql> show tables; +--------------------------+ | Tables_in_myaddressbook4 | +--------------------------+ | phonelisttable | +--------------------------+ 1 row in set (0.00 sec) mysql> select * from phonelistTable; +------------+--------+----------+ mysql> select * from phonelistTable; +----+------------+---------+----------+ | id | Last | First | Phone | +----+------------+---------+----------+ | 1 | Krumm | Jim | 123-4567 | | 2 | Powers | Austin | 444-4567 | | 3 | Slick | Tom | 555-4567 | | 4 | Timberlake | Justin | 555-4567 | +----+------------+---------+----------+ 4 rows in set (0.00 sec)4 rows in set (0.03 sec) mysql>exit
|
Next make a connection for phonelistTable by first running odbcad32.exe. (This program is in your windows directory, when you find it send the shortcut to your desktop.) Select the System Tab as shown and click add.

Next Select the MySQL ODBC 3.51 Driver as shown below.

Set up the following MySQL window as follows (set user to root and password to student and test the connection) and click OK.

Now that the database is configured, as shown in the following window, click OK.

3: Make a new Java project in eclipse or your favorite Java IDE and click here to view the code to AddressBookMySql in a web page or click here to download the source file.
4. Below is a screen shot of the program running.

Basic Trig Functions Calculated Using the McLauren Series
The following application calculates approximations trig functions of sine, cosine, and tangent inputted as radians without using internal math functions in Java much as a scientific calculator might. I cannot claim they are optimal in efficiency, but they work. To download click here: trigFun.java.
import javax.swing.*;
public class trigFun
{
public static void main(String args[])
{
String temp;
temp=JOptionPane.showInputDialog(null, "Enter an
integer to take factorial of");
int myFactorial=Integer.parseInt(temp);
System.out.println("Factorial
of("+temp+")="+factorial(myFactorial));
temp=JOptionPane.showInputDialog(null, "Enter a
radian value");
double myRadian=Double.parseDouble(temp);
System.out.println("sin("+myRadian+")="+sin(myRadian));
System.out.println("cos("+myRadian+")="+cos(myRadian));
System.out.println("tan("+myRadian+")="+tan(myRadian));
}
public static double factorial(int n)
{
double sum=1;
for(double i=n; i>0; i--)
{
sum=sum*i;
}
return sum;
}
public static double sin(double radian)//calculated
using mclauren series
{
int sign;
if (radian<-3.141592653589793)
sign=0;
else
sign=1;
if (sign==0)
{
while(radian<-3.141592653589793)
{
radian=radian+6.283185307179586;
}
}
if (sign==1)
{
while(radian>3.141592653589793)
{
radian=radian-6.283185307179586;
}
}
double result=0;
int j=1;
for (double i=1; i<=21;i=i+2)
{ //21 is a large
arbitrary value
if(j%2==0)
{
result=result-1*(pow(radian, i))/((double)(factorial((int)(i))));
}
else
{
result=result+1*(pow(radian, i))/((double)(factorial((int)(i))));
}
j=j+1;
}
return result;
}
public static double cos(double radian)
{
int sign;
if (radian<0)
sign=0;
else
sign=1;
if (sign==0)
{
while(radian<-3.141592653589793)
{
radian=radian+6.283185307179586;
}
}
if (sign==1)
{
while(radian>3.141592653589793)
{
radian=radian-6.283185307179586;
}
}
double result=0;
int j=1;
for (double i=0; i<=20;i=i+2)
{
if(j%2==0)
{
result=result-1*(pow(radian, i))/((double)(factorial((int)(i))));
}
else
{
result=result+1*(pow(radian, i))/((double)(factorial((int)(i))));
}
j=j+1;
}
return result;
}
public static double tan(double radian)
{
int sign;
if (radian<0)
sign=0;
else
sign=1;
if (sign==0)
{
while(radian<-3.141592653589793)
{
radian=radian+6.283185307179586;
}
}
if (sign==1)
{
while(radian>3.141592653589793)
{
radian=radian-6.283185307179586;
}
}
return sin(radian)/cos(radian);
}
public static double pow(double b, double exp)
{ // an
incomplete pow function that only calculates
correctly for integer exp's
double result=1;
if(exp==0
&& b==0)
return 1./0;
if(exp==0
&& b !=0)
return 1;
for(int i=1; i<=exp; i++)
{
result=result*b;
}
return result;
}
}
|
The Babylonian Method for Determining a Square Root of a Number
The following is a method which is used to calculate the square root of a postive number. To download click here: sqrt.java.
import javax.swing.*;
public class sqrt
{
public static void main(String args[])
{
String tempString=JOptionPane.showInputDialog("Enter a positive number");
double number=Double.parseDouble(tempString);
System.out.println("Square root of " + number+"="+sqrt(number));
}
public static double sqrt(double number)
{ //square root Babylonian method
double estimate=number;
double divisor=2;
//below 100 is arbitrary, for
very small decimals i values must be large
for(int i=0; i<100; i++)
{
estimate=number/divisor;
estimate=(estimate+divisor)/2; //find average estimate & divisor
divisor=estimate;
}
return estimate;
}
}
|
An Applet that Displays up to 4000 Numbers in the Fibonacci Sequence
The following program displays a scrollable list of up to 4000 numbers in the Fibonacci Sequence. The 4000th number in the sequence displays its 836 digits in all their glory. This program allows the user to select how many numbers they wish to view in the sequence starting with 1. It is quick in that it does not use recursive function calls to calculate the results, but rather just adds the last two numbers in the sequence to generate the next number. If you select 4000 it will take a while (perhaps 5 or more minutes) to run. This program requires that your computer resolution be set to at least 1024x768. The code for the program shows how Java's BigInteger class can be used to display a huge integer. To download click here: BigFibApplet.java. To view the source code click here. To see the program run click on the picture of the applet below:
©All rights reserved by James Krumm. Originally made available at www.caspercomsci.com. Materials here can be used, and redistributed, provided proper reference is made to the origin and author(s) of these materials. Please send any corrections or suggestions to jkrumm@caspercollege.edu. Last modified May 12, 2011.
To find out more about the Casper College Computer Science Program contact us at:
James Krumm |