February 9th, 2008 03:57am
wsaryada
The Rock, Paper and Scissors is a traditional classic game that maybe can be found in almost every part of the world including in my homeland, Bali. This classic game played by two people, they can pick either the rock, the paper or the scissors as their weapon. The rule is simple: rock wins to scissors, paper wins to rock, scissors wins to paper.
In this blog post we will create this simple game. We’ll create the Rock, Paper and Scissors object and will make them comparable to each other and they also have their own name. To make this feature we create a GameObject interface that extends the java.lang.Comparable interface, this is where it will get the ability to do the comparison. Let’s see what we got so far in our code.
The GameObject interface.
package org.kodejava.example.app;
public interface GameObject extends Comparable {
public String getName();
}
The Rock class.
package org.kodejava.example.app;
public class Rock implements GameObject {
private String name;
public Rock() {
this.name = "Rock";
}
@Override
public String getName() {
return name;
}
@Override
public int compareTo(Object o) {
if (o instanceof Paper) {
return -1;
} else if (o instanceof Scissor) {
return 1;
} else if (o instanceof Rock) {
return 0;
}
throw new UnsupportedOperationException();
}
}
The Paper class.
package org.kodejava.example.app;
public class Paper implements GameObject {
private String name;
public Paper() {
this.name = "Paper";
}
@Override
public String getName() {
return name;
}
@Override
public int compareTo(Object o) {
if (o instanceof Scissor) {
return -1;
} else if (o instanceof Rock) {
return 1;
} else if (o instanceof Paper) {
return 0;
}
throw new UnsupportedOperationException();
}
}
The Scissors class.
package org.kodejava.example.app;
public class Scissors implements GameObject {
private String name;
public Scissors() {
this.name = "Scissors";
}
@Override
public String getName() {
return name;
}
@Override
public int compareTo(Object o) {
if (o instanceof Rock) {
return -1;
} else if (o instanceof Paper) {
return 1;
} else if (o instanceof Scissor) {
return 0;
}
throw new UnsupportedOperationException();
}
}
As you can see from the code above, all classes; Rock, Paper and Scissors; are implementing the GameObject interface. This will make each class to implements the getName() and compareTo() methods.
The compareTo() method basically compare the object with the passed in object. We first check it using the instanceof operator to know if the passed object is type of Rock, Paper or Scissors. Based on the win, lose or draw rules the method return the corresponding 1, -1 or 0 value.
Now let see the main class that run our game. This class is a standard Java application class with the main(String[] args) method. The class also have a factory method; createObject(int type) method; for creating the correct GameObject from user input or a random pick by the computer player.
package org.kodejava.example.app;
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissorsGame {
public static final int ROCK = 0;
public static final int PAPER = 1;
public static final int SCISSORS = 2;
public static void main(String[] args) {
RockPaperScissorsGame game = new RockPaperScissorsGame();
game.runGame();
}
protected void runGame() {
System.out.println("ROCK = 0");
System.out.println("PAPER = 1");
System.out.println("SCISSORS = 2");
Scanner scanner = new Scanner(System.in);
int input;
do {
System.out.print("Pick your selection (0, 1, 2): ");
input = scanner.nextInt();
} while (input < 0 || input > 2);
GameObject objectOne = createObject(input);
//
// Computer pick his game object.
//
Random random = new Random();
int type = random.nextInt(3);
GameObject objectTwo = createObject(type);
System.out.println(”Your pick : ” + objectOne.getName());
System.out.println(”Computer pick: ” + objectTwo.getName());
getWinner(objectOne, objectTwo);
}
private void getWinner(GameObject objectOne, GameObject objectTwo) {
if (objectOne.compareTo(objectTwo) > 0) {
System.out.println(objectOne.getName() + ” vs ”
+ objectTwo.getName() + “: YOU WIN!!!”);
} else if (objectOne.compareTo(objectTwo) < 0) {
System.out.println(objectOne.getName() + " vs "
+ objectTwo.getName() + ": YOU LOSE!!!");
} else if (objectOne.compareTo(objectTwo) == 0) {
System.out.println(objectOne.getName() + " vs "
+ objectTwo.getName() + ": DRAW!!!");
}
}
private GameObject createObject(int type) {
if (type == ROCK) {
return new Rock();
} if (type == PAPER) {
return new Paper();
} if (type == SCISSORS) {
return new Scissors();
}
return null;
}
}
We have all the code, compile it with your JDK or IDE. To play it, run the application and pick your target weapon to fight the computer player by entering the number displayed in the console, 0 for rock, 1 for paper or 2 for scissors.
November 27th, 2007 08:41am
wsaryada
I was just reinstall my PC operating system so I need to reinstall everything once again and this include the Subversion version control system. I download Subversion 1.4.5 extract it to my Program Files folder, create a new repository and then start the svnserve so I can start to use it to keep my projects.
But there is one more thing that I need to do, how do I make the svnserve run as a Windows service instead of just run it from the command prompt from time to time I work with Subversion. Previously I know that there is a library that can make it run as a Windows service.
I start to Google it and found some other new way to configure the svnserve as service. I then tried this post and combined it with other people suggestion and it just works.
I am using Windows XP with SP2, to register a service we can use sc command. SC is a command line program used for communicating with the NT Service Controller and Services. So below is the command line:
sc create svn.local binpath= "%ProgramFiles%\Subversion\bin\svnserve.exe --service --root D:\Repository" displayname= "Subversion Repository" depend= Tcpip start= auto
Note: I found that copy pasting breaks the the command above, so I suggest you to just retype what was shown above.
And please note that a space between the equal sign above is required (key= value), so just type it as it is. When it success you can see the success message like “[SC] CreateService SUCCESS” following your command.
Now you should have the svnserve installed as Windows service and you can configure it to run automatically every time you startup your Windows box.
November 1st, 2007 08:12am
wsaryada
By default the keywords expansion in Subversion is disabled. To enable this feature you’ll have to update the Subversion configuration file. In Windows operating system this file is located under the C:\Documents and Settings\[username]\Application Data\Subversion\config folder, where [username] is your login username.
Ok, now to enable it, find a property called enable-auto-prop in the [miscellany] section and remove the remark, or if it doesn’t exist you can create one as the example below:
[miscellany]
enabe-auto-prop = yes
In the [auto-props] section you can declare the file to which the keywords expansion will be enabled. To create it see the following example for .java and .html files.
[auto-props]
*.java = svn:keywords=Id Author Date Revision HeadURL
*.html = svn:keywords=Id Author Date Revision HeadURL
Here is an example of a Java class with the SVN keywords in it.
/*
* $Id:$
* --------------------------------------------------------
* Copyright (c) 2007 Kode Java Org.
*/
package org.kodejava.example.intro;
import java.util.Date;
/**
* @author I Wayan Saryada
* @version $Revision:$
*/
public class HelloWorld {
public static void main(String[] args) {
}
}
From this point, everytime you add a Java or HTML document into your Subversion repository it will automatically have the keywords specified above expanded.
October 31st, 2007 08:53pm
wsaryada
In this post I will show you how we can use Apache Commons-IO FileUtils class to help you doing file manipulation and getting some information about files. To start you need to have the latest commons-io library which you can download from http://jakarta.apache.org/commons project website.
You are now have a handfull classes of Apache Commons-IO. Let’s begin, first I’ll show you how we can read a file content straight to an instance of string using FileUtils class methods, either FileUtils.readFileToString(File file) or FileUtils.readFileToString(File file, String encoding).
I’m using the first method to create an example to show you how to read the content of a file. Let say that we are going to read the content of sample.txt file. So here is the code.
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class FileUtilsSample
{
public static void main(String[] args)
{
File file = new File("sample.txt");
try
{
String content = FileUtils.readFileToString(file);
System.out.println(content);
} catch (IOException e)
{
e.printStackTrace();
}
}
}
As the readFileToString(File file) is a static method you need to called it by prefixing the method call with its class name. It’s a very basic rule in Java, you should already know about it. We create an instance of File which will be passed into the method. This method also thrown an IOException exception when the operation failed, so we need to add a try-catch block for the method.
The next method is also quite usefull, say that you want to read the file line by line just like the BufferedReader does for you, but one again no while-loop routine. You can do this by using FileUtils.readLines(File file) method. This method read the file content line by line and returns it as a List of string.
Let me give you a simple example to use this method, the example is exactly the same with the previous one but we are using the readLine(File file) method in turn.
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;\r\n\r\npublic class FileUtilsSample
{
public static void main(String[] args)
{
File file = new File("sample.txt");
try
{
List contents = FileUtils.readLines(file);
for (String line : contents)
{
System.out.println(line);
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
I’ve shown you the file reading utilities above, now what about writing to a file, can I use this class to? Yes, our course you can. Maybe you can guess the name of a method that you can used to write to a file. I think I heard you, did you say writeStringToFile? Yes, that’s it
This method has the following signature, writeStringToFile(File file, String data). Let’s try the following example.
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class WriteToFileExample
{
public static void main(String[] args)
{
try
{
File file = new File("sample.txt");
String data = "Learning Java Programming";
FileUtils.writeStringToFile(file, data);
} catch (IOException e)
{
e.printStackTrace();
}
}
}
The example will create sample.txt file an insert some text in it. When this file is not exist it will automatically created. This example can tell you how easy to read from and write to a file using this library.
Beside of read and write utility you can also use this class to calculate directory size including its sub directory, find files in directory, checking file timestamp, copying file, checking if to file have the same content, etc.
October 30th, 2007 12:04am
wsaryada
When you decided to start to learn Java Programming you can start by downloading the Java Development Kit (JDK) from Java Offcial website. They are three different type of JDK, the JSE (Java Standard Edition), JEE (Java Enterprise Edition), JME (Java Mobile Edition).
From the website you can also download the Java API documentations which will sure be your first companion when learning the language. It is better also to download the Java Tutorial Series that was written by the Java expert.
From the tutorial you can learn from the basic of Java programming, the introduction of the fundamental object oriented programming (OOP) which is Java all about. Next you can also find trails in each subject of the API (application programming interface) that is provided by Java, such as the core package, how to communicate with database, Java GUI programming, Image manipulation, RMI, Java Beans Framework, etc.
When you want to write a code you might wonder what editor or IDE that you”ll need to use to start learning. A good text editor that support a coloring will be a good candidate, colorful screen is better that just black and white isn”t it?
There are a lot of good text editor available today such as the VIM, NotePad++, TextPad, Editplus, UltraEdit. If you already have your prefered editor you can use it of course.
If you”ll ready for the big stuff, a bigger homework of project you might considering to use an IDE (Integrated Development Environment) as you”ll be working with lots of Java classes and other configuration files and build script for examples. There are many great IDE on the Java world from the free to the commercial product.
What IDE to use is really a developer decision, use whatever tools that can help you to improve your learning and coding activity. You can find IDE such as NetBeans, Eclipse, JCreator, IDEA, JDeveloper, etc.
Beside learning from the Java tutorials there are also many forum on the internet where you can discuss your doubts or your problems. Forum like JavaRanch, Java Forum is a great forum with Java gurus that can help you to clarify your doubts and help you to solve your problem. But remember one thing when you ask for help, be polite, elaborate your problem clearly.
A good Java books on your desktop is also a good resource to study Java, from a good books you can learn the bolts and nuts of the Java programming languages. When you have all your arsenal you can get the best out of you in learning Java. Have fun!
October 29th, 2007 09:49pm
wsaryada
public class HelloWorld {
/**
* The main entry point for the application.
*/
public static void main(String[] args) {
System.out.println("Hello World, Welcome to My Java Examples Blog.");
}
}
Recent Comments