Java Source Code to Various Projects

by Jim Krumm

|Home| |Computer Science| |Casper College| |Java Source Code| |C++ Source Code| |Visual C++.NET| |Visual Basic| |Assembly Source Code| |Linux| |Dice Program|

|Wyoming| |Casper Area| |American Album| |Personal Album| |Hawaii| |Denmark| |Greece| |Paris| |London| |Amsterdam| |Links| |Sitemap|

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:

1. Basic Java Commands

2. Blackjack

3. The Graphing Applet

4. The Spiral Graphic Applet

5. Chess Game Applet

6. Battleship Application

7. CRCW Parallel Processing Model Simulation

8. The Mod Converter Converts from Different Bases to Base 10 and back

9. Tic Tac Toe

10. The Titration Program

11. Drawing Multicolored Stars with Lines Applets

12. Happy Face Animated Applet

13. Picture Shower Applet

14. SnowBoard Free Project in Java

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


Basic Java Commands:

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>
<APPLET CODE="lines5.class" CODEBASE="." WIDTH=400 HEIGHT=300></APPLET>
</BODY></HTML>

Then make an applet called lines5.java with this code:

import java.awt.Graphics;
public class lines5 extends java.applet.Applet
{
 public void paint(Graphics screen)
 {
   int i,x=0;
   int y=0;
   for(x=0;x<350;x=x+1)
   {
      y=4*x+(x*x*x);     screen.drawRoundRect(15,15,200,250,70,30);
   }
 }
}

To run the above applet and web page from the command line enter:

c:\>appletviewer lines5.html

(Back) 


The Black Jack Game

Black Jack Program

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.

(Back) 


The Graphing Applet

Graphing Applet

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. 

(Back)


The Spiral Graphic Program

Spiral Applet

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.

(Back)


The Chess Game

Chess Game

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.

(Back)


Battleship Game

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.

(Back)


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.

(Back)


The Mod Converter

Convert from base 10 to any base Convert from any base to base 10
Mod Con Program converting from base 10 to any other base Mod Con Program Converting to Base 10

Enter any positive number in base ten and any base up to base 35 and then press convert.

Press the Clear button to clear all entries

Enter any positive number from any base (other than ten) then enter which base that number is written in. Press convert and the program will display the number in base ten.

Press the Clear button to clear all entries

Programmed by:

Shawn C. Stalder

Programming with Java

Spring 2005

Shawn Stalder

View Source for Conversion from base 10

View Source for Conversion to base 10

(Back)


Tic Tac Toe by Alan Simone

Tic Tac Toe

(Back)


The Titration Program by Eric Mechalke:

Titration Program

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/.
Eric Mechalke

(Back)


Drawing Multicolored Stars with Lines Applets by Steve Tomasini

Four Point Star Six Point Star

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.

(Back)


Animated Happy Face Applet by Marshall Jefcoat

Happy Face

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.

(Back)


The Picture Shower Applet

Picture Shower Program

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 James Krumm and Christian Simon

Below is the SnowBoard Free Project, worked on by James Krumm and Christian Simon. It comes with graphics included. If you want a copy of the program and the graphics in the file click here.

Running Snow Board Free Program

(Back)


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;
class myclass
{
          public static void main(String[] args) throws IOException, InterruptedException
          {
                    Process p = Runtime.getRuntime().exec("cmd /c start /MIN helloworld.exe");
                    p.waitFor();
          }

}

(Back)


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.

Database program

Snapshot of the finished program

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:

Access Program,

Click on the browse Button As shown below:

Browse Button in Access

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).

Saving the Address database

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.

Renaming Columns in Access

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:

Finished Database

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

Name Table

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

Finished Database

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. 

ODBC

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

Create New Data Source

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

ODBC Data Source Name

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. 

Select Address accdb in ODBC

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

Finalize ODBC with OK

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 past 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.

(Back)


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.

  1. Goto mysql.org and under downloads install the mycommunity mysql server

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.

  1. Goto downloads and download and install MySQL Connector/ODBC 3.51 Downloads which is the windows msi file 3.51.25 .
  2. Goto mysql.org and download and install the mysql connector/j

...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 Varialbles of the control panel

To Run MySql first navigate to the MySQL commandline CLient as shown below and click it.

MySQL CommandLine Client

If your password was setup to be student type student under enter the password.

Enter 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.

ODBC Data Source Administrator

Next Select the MySQL ODBC 3.51 Driver as shown below.

Create New Data Source

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

MySQL Connector/ODBC

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

ODBC Data Source Administrator

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.

MySQL Database Java Application

(Back)


©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 July 7, 2009.


To find out more about the Casper College Computer Science Program contact us at:

James Krumm
Department Head
Computer Science,
Wold Physical Sciences Building,
Casper College,
125 College Drive,
Casper, Wyoming 82601
(307) 268-2519 jkrumm@caspercollege.edu

[Return to Home Page]

|Home| |Computer Science| |Casper College| |Java Source Code| |C++ Source Code| |Visual C++.NET| |Visual Basic| |Assembly Source Code| |Linux| |Dice Program|

|Wyoming| |Casper Area| |American Album| |Personal Album| |Hawaii| |Denmark| |Greece| |Paris| |London| |Amsterdam| |Links| |Sitemap|