About Assignment Five


This program consists of four .java files and one simple MySQL database (nim_db) that contains two tables (human and computer). I created nim_db by using the following MySQL statements at the MySQL monitor prompt:

CREATE DATABASE nim_db
use nim_db;
CREATE TABLE computer (gameNum int NOT NULL AUTO_INCREMENT, number_of_moves int, won_or_lost varchar(4) , PRIMARY KEY (gameNum));
CREATE TABLE human (gameNum int NOT NULL AUTO_INCREMENT, number_of_moves int , won_or_lost varchar(4), PRIMARY KEY (gameNum));

It's essentially the same thing that you could put into an sql script file, I did create a script file after the fact, in case I wanted to wipe the database and start fresh without having to type all the commands at the monitor.

    One of the .java files is the one that I wrote for the GUI version of NIM for assignment three (I renamed it AssignmentFive.java). I made some slight modifications to it to enable it to save data that will be written to nim_db after the game is finished.

1) In class Game (line 34, AssignmentFive.java) I added two integer variables (playerMoves & computerMoves-both initialized to zero) for storing the number of moves that both the player and the computer makes during the game. I also added two String variables (playerWonOrLost & compWonOrLost). playerWonOrLost and compWonOrLost are both initialized to "" and will eventually be assigned the value of "WON" or "LOST" depending upon who wins the game; This is done in methods playerTurn() and compTurn() in class Game of AssignmentFive.java. playerMoves & computerMoves are incremented each time a successful move is made. This is also done in methods playerTurn() and compTurn() in class Game of AssignmentFive.java.

2) In class Game, I added two get methods, public static int getNumber(String mover) & public static String getGameResult(String mover), which are called from methods insertHuman() and insertComputer() in class NimDB (NimDB.java). Depending upon who wins the game, these methods return the appropriate integer and String values (mentioned above), which are written to nim_db.

3) In methods playerTurn() and compTurn() a call to method writeToDB() in class NimDB (NimDB.java) is made when a winner is determined.

4) I added two buttons to the GUI (and registered ActionListeners for each). The first one (infoButton) displays a message dialog that provides information about the tables in database nim_db; the second button (showDatabaseButton) up the searchable GUI for nim_db (NimHistory.java).

NimDB.java contains methods thatand close the connection to the nim_db database; it also contains methods (insertHuman(), insertComputer()) that create sql strings for inserting values into tables human and computer, respectively.

The final method in NimDB.java is writeToDB(), which is called from methods playerTurn() and compTurn() in class Game. This method calls the connect(), insertHuman(), insertComputer() and disconnect() methods in NimDB.java.

Where NimDB.java is responsible for writing to the nim_db database, DisplayHistory.java is responsible for reading from it. The database driver is loaded, a connection is made, a statement created and the query is executed. A String variable (results) is used for holding the query results that are displayed to the user in the TextArea (textA) in NimHistory.java. The results String is built with a while loop (while (resultSet.next()) in the DisplayHistory constructor.

NimHistory.java is the GUI part. When the user enters their query into the text field and clicks the submit button, the submit button's action listener then sets the TextArea (textA) to "", creates a new DisplayHistory, grabs the results String from DisplayHistory and sets it into TextA.

I used the following as models for writing this program:
  •     
  • DisplayQuotes.java     
  • DisplayShirts.java     
  • DisplayAuthors.java (Deitel, pages 1196-1198)     
  • ResultSetTableModel.java (Deitel, pages 1202-1205)     
  • DisplayQueryResults.java (Deitel, pages 1208-1211)

Files for AssignmentFive (note: these are plain text files) Assignment Six Next >>
More examples...Assignment Seven, JNI, Soap, WebServices >>


FMI, email: cosmo_AT_megalink_DOT_net


Cosmo Cat Astrology-Horoscopes, zodiac trivia, cosmic information Articles catalogue
2001 2002 2004 2007