Tuesday 21 January 2014

Finding duplicates from List

Finding the duplicates from List: The below program is for finding the duplicates from the list and return the duplicate values.finding the duplicates from the list this will be useful for any interviews as well as best programming practice to the beginners in developement. please make sure that it is one way i have shown to you. but as a programmer we can write multiple logics for the same.if you have such type of logics please comment it below then everyone can come to know the different ways to find out the same solution.

//Finding the duplicates from list

import java.util.*;

public class FindOutRepeatedNum {

public static void main(String[] args) {
List<Integer> listContainingDuplicates = new ArrayList<Integer>();

for (int i = 0; i < 20; i++) {
listContainingDuplicates.add(i);
}
//addding duplicates
listContainingDuplicates.add(1);
listContainingDuplicates.add(3);
listContainingDuplicates.add(5);

FindOutRepeatedNum forn = new FindOutRepeatedNum();
Set<Integer> set = forn.findDuplicates(listContainingDuplicates);

for (Integer intval : set) {
System.out.println(intval);
}
}

public Set<Integer> findDuplicates(List<Integer> listContainingDuplicates)

 final Set<Integer> setToReturn = new HashSet<Integer>(); 
 final Set<Integer> set1 = new HashSet<Integer>();

 for (Integer yourInt : listContainingDuplicates)
 {
 //if a set is not going add a repeated value that time we are going to add the repeated value to another set (because set never allowed duplicates)
  if (!set1.add(yourInt))
  {
   setToReturn.add(yourInt);//adding duplicate value to set
  }
 }
 return setToReturn;
}

}



OUTPUT:
1
3
5

Snake dice program in java

The below code is for Snake dice
Snake dice program means i think all of you know snake chart and  dice game,
based on the snake dice game i have implemented the below program for the same.
In many interviews they are asking the below program.

Requirement: Please write a program which performs snake dice game.


       /*  An object of class PairOfDice represents a pair of dice,
         where each die shows a number between 1 and 6.  The dice
         can be rolled, which randomizes the numbers showing on the
         dice.
      */

      /* Roll the dice until they come up snake eyes(if the dice total is 2). */



package com.naga.test;

import java.util.Scanner;

public class PairOfDice {

 private int die1;
 private int die2;

 private static int singleDie; // 6,5,4,3,2,1

 public void rollDice() {
  die1 = (int) (Math.random() * 6) + 1;
  die2 = (int) (Math.random() * 6) + 1;
 }

 public void rollSingleDice() {
  singleDie = (int) (Math.random() * 6) + 1;
 }

 public int getTotal() {
  return die1 + die2;
 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  PairOfDice dice; // A variable that will refer to the dice.
  int rollCount; // Number of times the dice have been rolled.

  dice = new PairOfDice(); // Create the PairOfDice object.
  rollCount = 0;

  /*
   * Roll the dice until they come up snake eyes.(dies total is equal to
   * 2)
   */
  int enterdValue;
  String enterdString = null;
  do {
   System.out
     .print("Please select how many dices are you going to use to play this game:  ");
   Scanner sc = new Scanner(System.in);
   enterdValue = sc.nextInt();

   if (enterdValue == 2 || enterdValue == 1) {
    if (enterdValue == 2) {
     do {
      dice.rollDice();
      System.out.println("The dies come up with dies1= "
        + dice.die1 + " and dies2= " + dice.die2);
      rollCount++;

     } while (dice.getTotal() != 2);

     /* Report the number of rolls. */
     System.out.println("\nIt took " + rollCount
       + " rolls to get a 2(means reached snake eyes).");
     System.out
       .print("Do you want to continue the playing this game(yes/no): ");
     enterdString = sc.next();
    } else {
     do {
      dice.rollSingleDice();
      System.out.println("The dies come up with dice= "
        + singleDie);
      rollCount++;

     } while (singleDie != 2);

     /* Report the number of rolls. */
     System.out.println("\nIt took " + rollCount
       + " rolls to get a 2(means reached snake eyes).");
     System.out
       .print("Do you want to continue the playing this game(yes/no): ");
     enterdString = sc.next();
    }

   } else {
    System.out.println("Please enter only 1 or 2.... ");
   }
  } while (enterdString.equalsIgnoreCase("yes") || (enterdValue != 2));

 }

}
OUTPUT:
Please select how many dices are you going to use to play this game: 2 The dies come up with dies1= 6 and dies2= 2 The dies come up with dies1= 3 and dies2= 2 The dies come up with dies1= 4 and dies2= 2 The dies come up with dies1= 4 and dies2= 4 The dies come up with dies1= 5 and dies2= 5 The dies come up with dies1= 1 and dies2= 5 The dies come up with dies1= 3 and dies2= 6 The dies come up with dies1= 3 and dies2= 2 The dies come up with dies1= 6 and dies2= 1 The dies come up with dies1= 5 and dies2= 1 The dies come up with dies1= 3 and dies2= 4 The dies come up with dies1= 6 and dies2= 5 The dies come up with dies1= 4 and dies2= 6 The dies come up with dies1= 5 and dies2= 4 The dies come up with dies1= 3 and dies2= 3 The dies come up with dies1= 2 and dies2= 3 The dies come up with dies1= 1 and dies2= 5 The dies come up with dies1= 1 and dies2= 1 It took 18 rolls to get a 2(means reached snake eyes). Do you want to continue the playing this game(yes/no): yes Please select how many dices are you going to use to play this game: 1 The dies come up with dice= 1 The dies come up with dice= 2 It took 20 rolls to get a 2(means reached snake eyes). Do you want to continue the playing this game(yes/no): 3 Please select how many dices are you going to use to play this game: