Saturday, November 9, 2019

Benefits of Spring Framework

Benefits of Spring Framework in Java

Spring Framework's fundamental mission is to simplify Java development. 

Four key strategies which Spring  uses for simplifying java development are :

1. Lightweight nature of Spring and non invasive POJO development.
2. Using Dependency Injection for Loose coupling.
3. Declarative programming through aspects.
4. Boilerplate code is eliminated with aspects and templates 


Spring is Light weight .Spring provides different modules and allow us to use any one based on the requirement, Ideally the spring jar is just 2-3 MB. 

Increasing the power of Non Invasive Pojos.
While using most of the frameworks during java development like Struts 1  and Ejb 2, we have to extend framework related classes or implement interfaces. This makes our Java beans coupled up with the framework decreasing its re-usability. Spring avoids (as much as possible) littering your application code with its API

While using Spring we are not forced to implement a Spring-specific interface or extend a Spring-specific class. Although these Spring POJOs are very simple but can be powerful through the use of Dependenct Injection and AOP. 



For Training Check my Website Online Spring Framework Training From India 











Tuesday, November 5, 2019

Rock Paper Scissors Java Program Code

Developing Rock Paper Scissors Game using Java

In this example we are going to develop a Rock Paper Scissors Game using Java. Developing games while learning java enhances our programming skills.

The Rule of the Game are as follows:

  1. Rock beats Scissors.
  2. Scissors beats Paper.
  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.





Learn Core Java Properly through my Private Core Java Online Training to be able to write games in java easily.

Rock paper Scissors Java Program Video Tutorial


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 user can decide to end the game any time.
  9. We will keep a track of who wins 5 times first. And then display the winner.

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

import java.util.Random;
//This class will represent the computer who is playing with the user.
public class Computer {
// This function helps the computer do the selection among ROCK,
// PAPER,SCISSORS
// we have used Random class from java.util.
public int getInput() {
Random random = new Random();
int input = random.nextInt(3) + 1;
return input;
}
}
view raw Computer.java hosted with ❤ by GitHub
public class Driver {
//These instance variables will act as constants through out the project
public static final int ROCK = 1;
public static final int PAPER = 2;
public static final int SCISSORS = 3;
//This function will help to display who selected what
public static void display(String who, int s) {
switch (s) {
case Driver.ROCK:
System.out.print(who + " selected ROCK ");
break;
case Driver.PAPER:
System.out.print(who + " selected PAPER ");
break;
case Driver.SCISSORS:
System.out.print(who + " selected SCISSORS ");
break;
default:
break;
}
}
//This function contains the actual buisness logic
//and help to decide who won
public static int compareSelections(int userSelection, int computerSelection) {
if(userSelection==computerSelection) return 0;
switch (userSelection) {
case ROCK:
return (computerSelection == SCISSORS ? 1 : -1);
case PAPER:
return (computerSelection == ROCK ? 1 : -1);
case SCISSORS:
return (computerSelection == PAPER ? 1 : -1);
}
return 0;
}
}
view raw Driver.java hosted with ❤ by GitHub
import java.util.Scanner;
public class Player {
private String name;// Represents the Players name
Scanner sc = new Scanner(System.in);
// sc will help to take inputs from keyboard
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void askName() {// Will let the user enter his name
System.out.println("Please enter your name");
name = sc.next();
}
public int getInput() {
// This function lets the user do the selection among
// ROCK PAPER SCISSORS
// and return what has been selected
System.out.println("Select ROCK PAPER SCISSOR");
String input = sc.next();
input = input.toUpperCase();
char c = input.charAt(0);
// checking the 1st character in the input
if (c == 'R')
return Driver.ROCK;
else if (c == 'P')
return Driver.PAPER;
else if (c == 'S')
return Driver.SCISSORS;
else {
getInput();
return 0;
}
}
public boolean playAgain() {
// This function will ask if the user wants to
// play again and return true or false accordingly.
sc=new Scanner(System.in);
System.out.print("Do you want to play again? ");
String userInput = sc.nextLine();
userInput = userInput.toUpperCase();
return userInput.charAt(0) == 'Y';
}
}
view raw Player.java hosted with ❤ by GitHub
public class RPS {
//Instance variables declaration
private Player player;
private Computer computer;
private int playerScore;
private int computerScore;
private int numberOfGames;
//Default Constructor initializing the instance variables
public RPS() {
player = new Player();
computer = new Computer();
playerScore = 0;
computerScore = 0;
numberOfGames = 0;
}
public static void main(String[] args) {
RPS rps = new RPS();//Initialization occured.
rps.getplayerName();//Getting user name
rps.startGame();
}
//This function is called recursively till the player wants to play.
//It is even exited if the user of computer completes the first 5 wins.
public void startGame() {
int playerinput = player.getInput();
Driver.display(player.getName(), playerinput);
// Get moves
int computerinput = computer.getInput();
Driver.display("computer", computerinput);
// Compare moves and determine winner
int compareResult=Driver.compareSelections(playerinput, computerinput);
switch (compareResult) {
case 0: // Tie
System.out.println("Tie!");
break;
case 1: // player wins
System.out.println(player.getName()+ " Beats " + "computer" +" You won!");
playerScore++;
break;
case -1: // Computer wins
System.out.println("Computer" +" Beats "+ player.getName()+" You Lost!");
computerScore++;
break;
}
numberOfGames++;
if(playerScore==5)
{
System.out.println(player.getName()+" HAS WON THE GAME.............");
new RPS();
}
if(computerScore==5)
{
System.out.println("Computer "+" HAS WON THE GAME............");
new RPS();
}
// Ask the player to play again
if (player.playAgain()) {
System.out.println();
startGame();
} else {
printStats();
}
}
//helps to get the users name
public void getplayerName() {
player.askName();
}
//Prints the result.
public void printStats()
{
System.out.println("Number of games played is "+numberOfGames);
System.out.println(player.getName()+"'s score "+playerScore);
System.out.println("Computers score "+computerScore);
}
}
view raw RPS.java hosted with ❤ by GitHub
Please enter your name
Chinmay
Select ROCK PAPER SCISSOR
ROCK
Chinmay selected ROCK computer selected SCISSORS Chinmay Beats computer You won!
Do you want to play again? y
Select ROCK PAPER SCISSOR
Scissor
Chinmay selected SCISSORS computer selected PAPER Chinmay Beats computer You won!
Do you want to play again? y
Select ROCK PAPER SCISSOR
ROCK
Chinmay selected ROCK computer selected PAPER Computer Beats Chinmay You Lost!
Do you want to play again? y
Select ROCK PAPER SCISSOR
ROCK
Chinmay selected ROCK computer selected PAPER Computer Beats Chinmay You Lost!
Do you want to play again? y
Select ROCK PAPER SCISSOR
SCISSORS
Chinmay selected SCISSORS computer selected SCISSORS Tie!
Do you want to play again? y
Select ROCK PAPER SCISSOR
PAPER
Chinmay selected PAPER computer selected ROCK Chinmay Beats computer You won!
Do you want to play again? y
Select ROCK PAPER SCISSOR
ROCK
Chinmay selected ROCK computer selected SCISSORS Chinmay Beats computer You won!
Do you want to play again? y
Select ROCK PAPER SCISSOR
PAPER
Chinmay selected PAPER computer selected SCISSORS Computer Beats Chinmay You Lost!
Do you want to play again? y
Select ROCK PAPER SCISSOR
ROCK
Chinmay selected ROCK computer selected SCISSORS Chinmay Beats computer You won!
Chinmay HAS WON THE GAME.............
Do you want to play again? n Number of games played is 9 Chinmay's score 5 Computers score 3