Rock Paper Scissors Game using Java Programming

http://www.javatutoronline.com/wp-content/uploads/2017/10/Rock_Paper_Scissors_Java_Code

 The Rule of the Game are as follows:

  1. Rock beats Scissors.
  2. Scissors beats Rock.
  3. Paper beats Rock.
  4. Who ever wins 5 times first is the Winner.
  5. In this example the game will be between a user and the computer.

 

Steps of the Rock Paper Scissors Java Program

  1. We will create a class Player.java that represents the user who is going to play the game with the computer.
  2. The class Computer.java represents the Computer that is playing with the user.
  3. Driver.java is a helper class containing the Rock Paper Scissor constant variables and the business logic code.
  4. RPS.java is the class containing the main function where the program execution will begin.
  5. We will ask the user to enter his name. Then select one among ROCK PAPER SCISSORS.
  6. Then we will let the computer randomly select ROCK PAPER SCISSORS.
  7. We will use the business logic function to find out who wins. And every time we will display the selections made and who won.
  8. The can decide to end the game any time.
  9. We will keep a track of who wind 5 times first. And then display the winner.

Player.java will represent the User playing with the computer.

Computer.java will represent the computer that is playing with the user.

Driver.java is a helper class containing constants and business logic.

RPS.java contains the main method. This is the starting point of program execution.

Output of the Rock Paper Scissors Java Game

 

Download Java Source Code for Rock Paper Scissor Game

Ejb1 vs Ejb2 vs Ejb3

EJB1 vs. EJB2 vs. EJB3

Ejb1 had limited functionality just as a lady bug that doesn’t do much work still is appreciated by people due to its beauty.

Ejb2 was very heavy like the Elephant with functionalities. Still to take proper use of the elephant power it had to be tamed, or Ejb2 was later simplified to Ejb3

Ejb3 is very simple having power and is serving the people very well like a cow giving milk to the universe.

 

Overview of EJB

Ejb is a platform for building ,reusable, portable and scalable business applications using java.

Ejb helps developers to concentrate on building business logic code without having to build infrastructure code comprising of functionalities including transactions, security, automated persistence that are provided by the Ejb container.

Ejb as Component

Ejbs are server side components that you can use to build parts of an application like business logic or persistence code. These components in Ejb3 are simple POJO with special power. Component means that the user need not know the inner working of the Component, he only needs to know what he has to give and what the Component will give you i.e. application behavior has to be completely encapsulated from the outside world.

There are 3 types of Ejbs. Session beans taking care of business logic. Entity Bean used for persistence. Message driven beans.

One of the most important use of Ejbs is its reusability. E.g. If there is a site for online books that is using an Ejb code for checking credit card. Then if we create another Website for CDs then we can reuse the same credit card Ejb. The first Ejb can be put as an Ejb component or a web service that is used by both applications.

Application1
uses Credit card Ejb

Application2
Ejb as Framework

The Ejb Container houses the Ejb components we develop. This Ejb Container together with the Ejb Component act as a Framework with predefined functionalities. This relieves the programmer from coding certain areas like transaction, security and persistence that are provide by the Ejb Container itself. Like if a programmer is creating a bean related to processing a credit card he need not create coding for the Transaction and Security that is provided by the Ejb Container Automatically.

We can use metadata annotations in our beans to specify what all services and functionalities we require. These are added dynamically when required during deployment. Metadata annotations is a way to specify the properties in a POJO, and these functionalities get automatically added. So we can say a POJO + Metadata annotation=Ejb3 bean. So we don’t need any outside xml configuration files.

Ejb is a specification which is followed by a large group of user vendors creating application server. So all have to follow the same specification making our applications portable.

Domain Driven Design

DDD is the preferred way as compared to the traditional four tired architecture followed by Ejb 2.

Following DDD the domain objects should contain buisness logic and shouldn’t be just a replica of database record. Incase of Ejb 2 beans were not POJOS and didn’t support OO features like inheritance, polymorphism.

Ejb3 helps follow DDD. Beans created using Ejb3 JPA supports polymorphism, inheritance etc.

Why choose EJB 3 among other technologies

Ease of use, Integrated solution stack— Ejb3 offers a complete stack of server solutions, including persistence, dependency injection, interceptors i.e. we won’t need to look for third party solutions to integrate in our application.

Ant Java Build Tool Tutorial

Ant is a Java build tool. It is an open source program designed to help develop, and build big programs by automating the work tasks of compiling code, running tests, and Packaging the results for redistribution.

It is written completely in Java there by it is platform independent, easy to use, extensible, and scalable. It can be used in a small personal project, or it can be used in a large, project. It automates the entire build process.

In Ant build files are used to describe how to build a project. In the build file

developers list the high-level goals called the targets and actions to take to achieve each goal called the tasks. Each project contains multiple targets. Targets can depend on other targets. Targets contain tasks and Tasks do the work

Working with ANT

First ANT has to be downloaded and installed in the developers machine. Then the Path of the computer should be set to point to the bin directory of ANT. An environment variable called ANT_HOME should also be made in the System variables pointing to the installation directory of ANT.

A build.xml file should be created that will contain several targets that will further contain different tasks.

1) Target containing Tasks for creating build/classes directory to contain compiled files and creating a dist folder to contain the jar or war file of the project.

2) A Target to compile the java files in the source folder and put it into the build/classes folder.

3) A Target that will package the project to a jar or war file.

4) A Target to create the java docs of the project.

5) A Target to run all the tests on the project.

6) A Target to run the class file containing the main method.

After following the steps the user can simply type ant on the command line and ANT will read the “build.xml” and execute all the targets.

 

Some of the very important benefits of ANT are as follows:

Ant is free and Open Source. Ant makes it easy to bring developers into a project. It is widely supported. It integrates testing into the build processes and enables continuous integration. It runs inside Integrated Development Environments

Java Exception Handling

Exception Handling in Java

Exceptions are used in Java Programming to handle errors and exceptional conditions.
This tutorial describes about exception and how to use them.

A program passes through compile time and run time.
Exceptions are run time errors.

Exception is an inbuilt class in the java.lang package. There are many sub classes of Exception class like ArithmeticException, NullpointerException etc.

Depending upon the nature of the problem occurring during run time, java creates the appropriate Exception object and throws it.

For example: – int i = 1/0; [Java will throw ArithmeticException type of object]

Why do we need to do Exception handling?

Problems with the default exception handling mechanism of Java

Java’s Default exception handlers  don’t do the work properly they stop the program execution abruptly and give the weird output to the user. This is the reason why we should do the Exception handling by using our own exception handlers. To handle we have to use try and catch blocks. The lines of code that may throw some exception should be put in the try block and whatever we want our exception handlers to do should be put in to the catch block.

Here, by doing 1/0, our program will stop abruptly. And it will give a weird error message. Like,

Exception in thread “main” java.lang.ArithmeticException: / by zero
at Exception_Demo.main(Exception_Demo.java:6)

But on using try and catch the problem is overcome.

Try Catch Block

It will not stop program abruptly and give customize message like

Multiple catch blocks

There are many sub classes of the main exception class. Depending upon the user’s input if different type of exception is occurring we need various type of catch block to handle them individually.

 

In this example, if length is 0 than it will be caught by catch block handling ArithmeticException. If we go out of the range of the array an ArryIndexOutOfBoundExceptionis thrown. Programs may have various type of exceptions which can be handled through multiple catch blocks. In this example third catch block is for general Exception type. The general Exception catch block should be put at the end.

How to Create Custom Exception Class?

There are some situations that is not exception for java but it is exception for our project requirement. For example if you want to run your program only when user gives commandline input (this is your program need which is not an exception for java itself.) so, this is the time when we manually create our own exception class. When we make this kind of exception it always extends the Exception class.

Throw keword

Throw keyword is used for manually throwing exception objects.

When a custom exception object is thrown it has to be handled through try and catch blocks because it cannot be handled by the fefault exception handler of java.

Throws KeyWord

When we define function and we don’t want to put try and catch block than we can use throws keyword. The caller of the function has to put the try and catch blocks. Throws keyword is for delegating the exception handling mechanism to the caller of the function.

eg. static void fun() throws No_Commandline_Exception,ArithmeticException

Checked and unchecked exception

Unchecked exceptions are those exceptions that can be handling by java’s default exception handlers. If we try to throw unchecked exception inside a function we don’t need to put try and catch block nor the throws.
Checked exceptions are those exceptions that can’t handle by java’s default exception handlers.
When a checked exception is thrown from inside a function we are bound to put the try and catch block or the throws declaration.

The difference between throw and throws

Throw is used to manually throw exception. If the definer don’t want to handle the exception using try and catch block than definer use throws keyword to indicate the caller that you must need to put try and catch block when you call the function. In this case definer delegates the work to the caller to put try and catch. So, if definer used more than 1 exceptions after throws keyword than caller is supposed to use corresponding catch block when function is being called.
All the custom exceptions are checked exception but there are also some inbuilt exception classes that are also checked.

List of checked exception

ClassNotFoundException Class not found.
CloneNotSupportedException Attempt to clone an object that does not implement the Cloneable interface
IllegalAccessException Access to a class is denied.
InstantiationException Attempt to create an object of an abstract class or interface.
InterruptedException One thread has been interrupted by another thread.
NoSuchFieldException A requested field does not exist.
NoSuchMethodException A requested method does not exist.

List of unchecked exception

ArithmeticException Arithmetic error, such as divide-by-zero.
ArrayIndexOutOfBoundsException Array index is out-of-bounds.
ArrayStoreException Assignment to an array element of an incompatible type.
ClassCastException Invalid cast.
IllegalArgumentException Illegal argument used to invoke a method.
IllegalMonitorStateException Illegal monitor operation, such as waiting on an unlocked thread.
IllegalStateException Environment or application is in incorrect state.
IllegalThreadStateException Requested operation not compatible with current thread state.
IndexOutOfBoundsException Some type of index is out-of-bounds.
NegativeArraySizeException Array created with a negative size.
NullPointerException Invalid use of a null reference.
NumberFormatException Invalid conversion of a string to a numeric format.
SecurityException Attempt to violate security.
StringIndexOutOfBounds Attempt to index outside the bounds of a string.
UnsupportedOperationException An unsupported operation was encountered.