The Tic Tac Toe Game in Java Code

This is a small program that shows how to create a TicTacToe game in Java.
The purpose of the game is to get three in a row before your opponent does, either vertically, horizontally or diagonally.

The TicTacToe program is made up of two Java classes. One is called TicTacToe and that is the actual game class that contains much of the logic for the game.
The other class called Main is the class that starts the game, displays the rules and loops for as many times as there are moves by the users.

The Main class looks like this:

Continue reading

Pretty Print SOAP Message and Display the SOAP message in HTML

In some situations it could be useful to pretty print a SOAP message to be able to show it in a html page.
Often applications that uses web services needs to log the SOAP messages that is sent to and from the services and then display them in some sort of administration application.
It would also perhaps be nice not to use the pre-tag but to embed the code along the rest of the html code.
In such a case the pretty print comes in handy. This example shows how to pretty print a SOAP message reading the unformatted envelope from a file, formats (pretty print) the message and writes it to a html file.

Continue reading

Weight Converter, Pound to Kilogram, Ounce to Kilogram

This example shows how to take input from the user and convert pound to Kilogram and also how to convert Ounce to Kilogram.
The user should first enter a number in pound. The program makes the conversion to Kilogram and prints out the result.
Next the user is asked to enter a number in ounce. The program makes the conversion to Kilogram and prints out the result.
It uses a BufferedReader and InputStreamReader to collect the input from the user.

Continue reading

Java Source Code for The HiLo Game

The purpose of the HiLo (or Hi-Lo) game is to guess the right number in a certain number of attempts. If the guess is too high the word HI is printed out and if the guess is too low the word LO is printed out.
In this example you have six attempts to guess the right number. The code consists of two classes, one main class and one class that contains the actual game logic.
The HiLo class generates its secret number, between 1-100, with the java.util.Random class.

Continue reading