Interview Questions

Miscellaneous

Card Game Using Java

Here is a simple game played with a pack of cards, usually by 2 or more players. Initially 'n' number of cards are distributed to all players. In each round, a player is given a chance to select a single card from his own set of cards. The player with maximum card number wins the round and gets a point. At last, player with most number of points wins the game. Friends, its time to play around in Java with cards. Happy Gaming!

Game.java

package in.techdive.game;

public interface Game
{
        void playGame(int numberOfPlayers);

        void displayWinners();

Java - equals() and hashcode() Implementation

The below code sample provides a basic implementation of equals() and hashcode() methods of Object class in Java. Consider a class named Device, with two attributes deviceId and deviceName. We assume that for two device objects to be equal, both their deviceId and deviceName should be the same. We have used the same attribute to calculate hashcode also. It takes each character's ASCII value in the deviceName and multiplies it with its position. Finally the resultant value is added to deviceId. In this way hashcode is calculated with both the attributes.

Java - Execute System Commands

This article describes how to execute System Commands using Java program.

The Runtime class allows the Java application to interface with the system environment. Using this Runtime class, the system commands can be executed using the api Runtime.exec().

Consider the below example.

ExecuteSystemCommands Class

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR

Java - Pagination Utility

Consider a scenario, where you want to display a set of records from database in the User interface page. If the number of records is more, we would be using pagination to display the records. In this case we need to reform the SQL query to retrieve results for a particular page number with the limited records per page. For example, consider the following SQL query.

SELECT * FROM EMPLOYEES ORDER BY DEPT_ID,DOJ

Java Base64 Encoding

Base64Encoding.java

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

Using Enum with Hibernate

This article deals with using Enum types in Hibernate mapping.

Enum is a new data type added as part of JDK 1.5 mainly used to deal with constants in Java applications.

In the Hibernate mapping file, we usually map data types in Java bean to the columns in database tables.

For data types like String, Integer it is just a direct mapping to columns in database tables.

Consider the Gender.java Enum data type class below.

Gender.java

package in.techdive.hib.model.Gender;

public enum Gender
{
        MALE("M"), FEMALE("F");

Java - Checksum manipulation

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

Sending Email using Java

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

Java - Jar Operations

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

Snake and Ladder Game

"All work and No Play makes Jack a dull boy". Yes friends its time to stop programming and have fun, lets play a game with java. Hope you would have heard about the Snake and Ladder game.

Its just a turn-based board game with numbers from 1 to 100. Some numbers may have ladders pointing up to a greater number and some numbers may have snake pointing to lower number.