Tuesday, 3 November 2015

Comparator Demo Using TreeSet

import java.util.*;
class Student
{
	int rollno;
	String name;
	String phone;
}

class MyComp implements Comparator {
	public int compare(Object o1, Object o2) {
		Student s1, s2; 
		s1 = (Student) o1; 
		s2 = (Student) o2; 
		int c;
		if(s1.rollno < s2.rollno)
			c = -1;
		else if (s1.rollno > s2.rollno)
			c = 1;
		else
			c = 0;
		return c;
	} 
}

class MyComp1 implements Comparator {
	public int compare(Object o1, Object o2) {
		Student s1, s2; 
		s1 = (Student) o1; 
		s2 = (Student) o2; 
		int c;
		int x = (s1.name).compareTo(s2.name);
		return x;
	} 
}
class CompDemo {
	public static void main(String args[]) {
		// Create a tree set.
		TreeSet ts = new TreeSet(new MyComp1()); 
		// Add elements to the tree set.
		Student s1 = new Student();
		s1.rollno = 1; s1.name = "Rahul"; s1.phone = "2590381";

		Student s2 = new Student();
		s2.rollno = 5; s2.name = "Devesh"; s2.phone = "9829059033";

		Student s3 = new Student();
		s3.rollno = 3; s3.name = "Rakesh"; s3.phone = "2590381";

		Student s4 = new Student();
		s4.rollno = 2; s4.name = "Manohar"; s4.phone = "2590381";

		Student s5 = new Student();
		s5.rollno = 4; s5.name = "Prateek"; s5.phone = "2590381";

		ts.add(s1); 
		ts.add(s2); 
		ts.add(s3); 
		ts.add(s4); 
		ts.add(s5); 
		// Display the elements.
		Iterator<Student> i = ts.iterator();
		while(i.hasNext()) {
			Student s = i.next();
			System.out.println(s.rollno +", "+ s.name + ", " +s.phone);
		}
	}
}

Saturday, 17 October 2015

Java awt Package Choice Example

 import java.awt.*; 
public class ChoiceDemo
{
public static void main(String args[])
{
Frame f = new Frame();
f.setLayout(new FlowLayout());
Choice ch = new Choice();
ch.add("India");
ch.add("Nepal");
ch.add("Shri Lanka");
ch.add("USA");
ch.add("UK");
f.add(ch);
f.setSize(300,300);
f.setVisible(true);
}
}

Java awt Package Checkbox Example

	import java.awt.*;
public class CheckboxDemo
{
public static void main(String args[])
{
Frame f = new Frame();
f.setLayout(new FlowLayout());
Checkbox cb = new Checkbox("Literate");
f.add(cb);
f.setSize(300,300);
f.setVisible(true);
}
}

Java awt Package Canvas Example

	import java.awt.* ;
public class CanvasDemo
{
public static void main(String args[])
{
Frame f = new Frame() ;
f.setLayout(new FlowLayout());
MyCanvas c = new MyCanvas();
c.setBackground(Color.red);
c.setSize(100,100);
f.add(c) ;
f.setSize(300,300);
f.setVisible(true);
}
}

class MyCanvas extends Canvas
{
public void paint(Graphics g)
{
setBackground(Color.green) ;
g.setColor(Color.red) ;
g.drawLine(0,0,100,100);
}
}

Java awt Package Button Demo


	import java.awt.*;
class ButtonDemo
{
public static void main(String args[])
{
Frame f = new Frame();
f.setLayout(new FlowLayout());
Button b = new Button("Apply");
f.add(b);
f.setSize(300,300);
f.setVisible(true);
}
}

Wednesday, 14 October 2015

Apex Trigger Salesforce

Apex Trigger is an action which gets fired on particular event. In salesforce trigger is apex code that executes before or after the below types of operations.

– Insert

– Update

– Delete

– Undelete

Triggers will run before object records are inserted, updated, deleted into the database or after records are inserted, updated, deleted and restored.

Apex Triggers can be classified into two types:

1. Before triggers can be used to update or validate record values before they are saved to the database.

2. After triggers can be used to access field values that are set by the database, and to affect changes in other records.

Events in triggers:

Before Insert, Before Update, Before Delete

After Insert, After Update, After delete, After Undelete

Syntax to create sample trigger:

Use below syntax to create trigger.

	trigger <TriggerName> on ObjectName (<events>) {
		// you can write your code here.
	}

Now i will give simple example to under stand how trigger works.

Here i will give example, when account is inserted, automatically contact also created for that account. this example is to under stand how trigger works.

	trigger insertContact on Account (after insert)
	{
		Contact cont = new Contact();
		cont.LastName = Trigger.new[0].name;
		cont.AccountId = Trigger.new[0].ID;
		insert cont;
	}

Above one is a simple trigger to insert contact when you create account. the text which is mentioned in green color is trigger name. And text which is mentioned in red color is event name.

Trigger.New is a context variable. which returns a list of new records of the sobjects which we are going to insert into the database.

Here in salesforce there many context variable available. Those are Trigger.New, Trigger.old, Trigger.NewMap, Trigger.OldMap, Trigger.isAfter, Trigger.isBefore, Trigger.isInsert, Trigger.isUpdate, Trigger.isDelete and Trigger.isUndelete.

We will discuss about each and every context variable later.

Here one more thing i will tell. If you want to move your trigger to Production, you should cover at least 1% code coverage for trigger.

Sunday, 11 October 2015

Java awt Package Border Layout Example 3

	import java.awt.*;
	public class BorderLayout3
	{
		public static void main(String args[])
		{
			Frame f = new Frame();
			f.setLayout(new BorderLayout());
			Panel toolbar = new Panel();
			toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
			toolbar.setBackground(Color.lightGray);
			toolbar.add(new Button("File"));
			toolbar.add(new Button("Edit"));
			toolbar.add(new Button("Tool"));
			toolbar.add(new Button("Help"));
			f.add(toolbar, BorderLayout.NORTH);
			TextField status = new TextField("Status.");
			status.setFont(new Font("Monospaced", Font.BOLD, 48));
			f.add(status, BorderLayout.SOUTH);
			f.add(status, BorderLayout.SOUTH);
			f.setSize(300,300);
			f.setVisible(true);
		}
	}

Java awt Package Border Layout Example 1


	import java.awt.*;
	public class BorderLayout1
	{
		public static void main(String args[])
		{
			Frame f = new Frame();
			Label labelTop = new Label("This is North");
			labelTop.setFont(new Font("Serif", Font.ITALIC, 36));
			labelTop.setForeground(Color.white);
			labelTop.setBackground(Color.black);
			f.add(labelTop, BorderLayout.NORTH);
			Label labelBottom = new Label("This is South");
			labelBottom.setFont(new Font("Monospaced", Font.BOLD, 18));
			labelBottom.setForeground(Color.white);
			labelBottom.setBackground(Color.black);
			f.add(labelBottom, BorderLayout.SOUTH);
			f.setSize(300,300);
			f.setVisible(true);
		}
	}

Java awt Package Border Layout Example 2

	import java.awt.*;
public class BorderLayout2
{
public static void main(String args[])
{
Frame f = new Frame();
Button north = new Button("N");
f.add(north,BorderLayout.NORTH);
f.add(new Button("S"),BorderLayout.SOUTH);
f.add(new Button("E"),BorderLayout.EAST);
f.add(new Button("W"),BorderLayout.WEST);
Panel p = new Panel();
p.setBackground(Color.green);
//f.add(p, BorderLayout.CENTER);
f.add(p); //default is center
f.setSize(300,300);
f.setVisible(true);
}
}

Tuesday, 29 September 2015

Java Bubble Sort Example

/*
        Java Bubble Sort Example
        This Java bubble sort example shows how to sort an array of int using bubble
        sort algorithm. Bubble sort is the simplest sorting algorithm.
*/

public class BubbleSort {

        public static void main(String[] args) {
             
                //create an int array we want to sort using bubble sort algorithm
                int intArray[] = new int[]{5,90,35,45,150,3};
             
                //print array before sorting using bubble sort algorithm
                System.out.println("Array Before Bubble Sort");
                for(int i=0; i < intArray.length; i++){
                        System.out.print(intArray[i] + " ");
                }
             
                /*sort an array using bubble sort algorithm

                 In bubble sort, we basically traverse the array from first
                  to array_length - 1 position and compare the element with the next one.
                  Element is swapped with the next element if the next element is greater.
               
                  Bubble sort steps are as follows.
               
                  1. Compare array[0] & array[1]
                  2. If array[0] > array [1] swap it.
                  3. Compare array[1] & array[2]
                  4. If array[1] > array[2] swap it.
                  ...
                  5. Compare array[n-1] & array[n]
                  6. if [n-1] > array[n] then swap it.
               
                  After this step we will have largest element at the last index.
               
                  Repeat the same steps for array[1] to array[n-1]
                 
                 */
                int n = intArray.length;
                int temp = 0;
             
                for(int i=0; i < n; i++){
                        for(int j=1; j < (n-i); j++){
                             
                                if(intArray[j-1] > intArray[j]){
                                        //swap the elements!
                                        temp = intArray[j-1];
                                        intArray[j-1] = intArray[j];
                                        intArray[j] = temp;
                                }
                             
                        }
                }
             
                System.out.println("");
             
                //print array after sorting using bubble sort algorithm
                System.out.println("Array After Bubble Sort");
                for(int i=0; i < intArray.length; i++){
                        System.out.print(intArray[i] + " ");
                }

        }
}

/*
Output of the Bubble Sort Example would be

Array Before Bubble Sort
5 90 35 45 150 3
Array After Bubble Sort
3 5 35 45 90 150

*/

Wednesday, 23 September 2015

Sorting ArrayList of Integer arraylist

import java.util.*;
class ArrayListSorting  {

public static void main(String args[]){
  ArrayList<Integer> listofcountries = new ArrayList<Integer>();
  listofcountries.add(89);
  listofcountries.add(5);
  listofcountries.add(3);
  listofcountries.add(9);

  /*Unsorted List*/
  System.out.println("Before Sorting:");
  for(int counter: listofcountries){
System.out.println(counter);
}

  /* Sort statement*/
  Collections.sort(listofcountries);

  /* Sorted List*/
  System.out.println("After Sorting:");
  for(int counter: listofcountries){
System.out.println(counter);
}
}
}

OutPut : - 


Add caption

Tuesday, 15 September 2015

Vactor Class Example 4

import java.util.*;
class VectorDemo3
{ public static void main(String args[])
{ Vector<String> list=new Vector<String>();
for(int i=0;i<args.length;i++)
{ list.addElement(args[i]);
}
list.insertElementAt("Cobol",2);
int size=list.size();
Object a[] = list.toArray();
System.out.println("List of Languages:");
for(int i=0;i<size;i++)
System.out.println((String)a[i]);
}
}

Vactor Class Example 3

import java.util.*;
class VectorDemo
{ public static void main(String args[])
{ // initial size is 3, increment is 2
Vector<Integer> v = new Vector<Integer>(3, 2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " + v.capacity());
v.addElement(1);
v.addElement(2);
v.addElement(3);
v.addElement(4);
System.out.println("Capacity after 4 additions:"+ v.capacity());
v.addElement(5);
System.out.println("Current capacity: " + v.capacity());
v.addElement(6);
v.addElement(7);
System.out.println("Current capacity: " +   v.capacity());
v.addElement(8);
v.addElement(9);
System.out.println("Current capacity: " + v.capacity());
v.addElement(10);
v.addElement(11);
v.addElement(12);

System.out.println("First element: " + v.firstElement());
System.out.println("Last element: " + v.lastElement());

if(v.contains(3))
     System.out.println("Vector contains 3.");

// Enumerate the elements in the vector.
Enumeration e = v.elements();
System.out.println("\nBy Enumeration: Elements in vector:");
while(e.hasMoreElements())
System.out.print(e.nextElement() + " ");
System.out.println();

Iterator<Integer> itr = v.iterator();
System.out.println("\nBy Iterator: Elements in vector:");
while(itr.hasNext())
System.out.print(itr.next() + " ");
System.out.println();

// Use for-each loop to display contents.  
System.out.println("\nBy for-each: Elements in vector:");
for(int i : v)
System.out.print(i + " ");
System.out.println();
System.out.println(v);
}
}

Vactor Class Example 2

import java.util.*;
class VectorDemo1
{ public static void main(String args[])
{ Vector<String> list=new Vector<String>();
for(int i=0; i < args.length; i++)
{ list.addElement(args[i]);
}
list.insertElementAt("Cobol",2);
int size=list.size();
String listArray[]=new String[size];
list.copyInto(listArray);
System.out.println("List of Languages:");
for(int i=0;i<size;i++)
System.out.println(listArray[i]);
}
}

Vector Class Example 1

import java.util.*;
class VectorDemo
{ public static void main(String args[])
{ // initial size is 3, increment is 2
Vector<Integer> v = new Vector<Integer>(3, 2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " + v.capacity());
v.addElement(1);
v.addElement(2);
v.addElement(3);
v.addElement(4);
System.out.println("Capacity after 4 additions:"+ v.capacity());

if(v.contains(3))
     System.out.println("Vector contains 3.");

// Enumerate the elements in the vector.
Enumeration e = v.elements();
System.out.println("\nBy Enumeration: Elements in vector:");
while(e.hasMoreElements())
System.out.print(e.nextElement() + " ");
System.out.println();

Iterator<Integer> itr = v.iterator();
System.out.println("\nBy Iterator: Elements in vector:");
while(itr.hasNext())
System.out.print(itr.next() + " ");
System.out.println();

// Use for-each loop to display contents.  
System.out.println("\nBy for-each: Elements in vector:");
for(int i : v)
System.out.print(i + " ");
System.out.println();
System.out.println(v);
System.out.println();
}
}

Monday, 14 September 2015

Add Integer Number in TreeSet Class Example

import java.util.*;
class TreeSetDemo1
{ public static void main(String args[])
{ // Create a tree set.
TreeSet<Integer> ts = new TreeSet<Integer>();
    // Add elements to the tree set.
ts.add(5);
ts.add(4);
ts.add(3);
ts.add(8);
ts.add(2);
ts.add(1);
System.out.println(ts);
}
}

Add String Number in TreeSet Class Example

import java.util.*;
class TreeSetDemo
{ public static void main(String args[])
{
// Create a tree set.
    TreeSet<String> ts = new TreeSet<String>();
     // Add elements to the tree set.
    ts.add("C");
  ts.add("A");
ts.add("B");
  ts.add("E");
    ts.add("F");
  ts.add("D");
System.out.println(ts);
}
}

TreeMap Class Example 1

import java.util.*;
class TreeMapDemo
{ public static void main(String args[])
{ // Create a hash map.
TreeMap<String, Double> tm = new TreeMap<String, Double>();
tm.put("Devesh",1000.00);
tm.put("Rahul",1500.00);
tm.put("Rakesh",1200.00);
tm.put("Manohar",900.00);
tm.put("Prateek",1300.00);
tm.put("Pawan", null);
//tm.put(null,34.56); will result in run-time error
// Get a set of keys
Set<String> set = tm.keySet();
// Display the set.
for(String key : set)
{
    System.out.print(key + ": ");
System.out.println(tm.get(key));
}
System.out.println();
// Deposit 1000 into Prateek's account.
double balance = tm.get("Prateek");
tm.put("Prateek", balance + 1000);
balance = (Double)tm.get("Prateek");
  System.out.println("Prateek's new balance: " + balance);
}
}

TreeMap Class Example 2

import java.util.*;
class Student
{ int rollno;
String name;
String phone;
}
class Comp implements Comparator<Student>
{ public int compare(Student s1, Student s2)
{ int c;
if(s1.rollno < s2.rollno)
c = -1;
else if (s1.rollno > s2.rollno)
c = 1;
else
c = 0;
return c;
}
}

class TreeMapDemo1
{ public static void main(String args[])
{   TreeMap<Student,Double> tm=new TreeMap<Student,Double>(new Comp());
// Add elements to the tree set.
Student s1 = new Student();
s1.rollno = 1; s1.name = "Rahul"; s1.phone = "2590381";

Student s2 = new Student();
s2.rollno = 5; s2.name = "Devesh"; s2.phone = "9829059033";

Student s3 = new Student();
s3.rollno = 3; s3.name = "Rakesh"; s3.phone = "2590381";

Student s4 = new Student();
s4.rollno = 2; s4.name = "Manohar"; s4.phone = "2590381";

Student s5 = new Student();
s5.rollno = 4; s5.name = "Prateek"; s5.phone = "2590381";

tm.put(s1, 1000.0);
tm.put(s2, 2000.0);
tm.put(s3, 3000.0);
tm.put(s4, 4000.0);
tm.put(s5, 5000.0);

Set<Student> set = tm.keySet();
for(Student s : set)
{
System.out.print(s.rollno + ", " + s.name + ", " + s.phone
+ "; " + "balance:");
System.out.println(tm.get(s));
  }
}
}

Stack Class Example

import java.util.*;
class StackDemo
{
  public static void main(String args[])
{ Stack<Integer> st = new Stack<Integer>();
System.out.println("stack: " + st);
st.push(42);
System.out.println("stack: " + st);
st.push(66);
System.out.println("stack: " + st);
st.push(99);
System.out.println("stack: " + st);
Integer io = st.pop();
System.out.println("stack: " + st);
io = st.pop();
System.out.println("stack: " + st);
io = st.pop();
System.out.println("stack: " + st);
try
{ st.pop();
}
catch (EmptyStackException e)
{ System.out.println("empty stack");
}
}
}

Saturday, 12 September 2015

LinkedHashMap class Example

import java.util.*;
class LinkedHashMapDemo
{ public static void main(String args[])
  { // Create a hash map.
LinkedHashMap hm = new LinkedHashMap();
      // Put elements to the map
hm.put("Devesh",1000.00);
hm.put("Rahul",1500.00);
hm.put("Rakesh",1200.00);
hm.put("Manohar",900.00);
hm.put("Prateek",1300.00);
// Get a set of the entries.
Set set = hm.entrySet();
// Display the set.
Iterator i = set.iterator();
while(i.hasNext())
{ Map.Entry me = (Map.Entry) i.next();
System.out.print(me.getKey() + ": ");
      System.out.println(me.getValue());
}
  System.out.println();
  // Deposit 1000 into John Doe's account.
double balance = (Double) hm.get("Prateek");
hm.put("Prateek", balance + 1000);
balance = (Double)hm.get("Prateek");
System.out.println("Prateek's new balance: " + balance);
}
}

LinkedHashSet Class Example

import java.util.*;
class LinkedHashSetDemo
{ public static void main(String args[])
{ // Create a hash set.
    LinkedHashSet<String> hs = new LinkedHashSet<String>();
// Add elements to the hash set.
  hs.add("B");
    hs.add("A");
    hs.add("D");
    hs.add("E");
    hs.add("C");
    hs.add("F");
System.out.println(hs);
}
}

LinkedList Class Example in java util package

import java.util.*;
class LinkedListDemo
{ public static void main(String args[])
{ // Create a linked list.
LinkedList<String> ll = new LinkedList<String>();
// Add elements to the linked list.
ll.add("F");
ll.add("B");
ll.add("D");
ll.add("E");
ll.add("C");
ll.addFirst("A");
ll.add(1, "A2");
System.out.println("Original contents of ll: " + ll);
// Remove elements from the linked list.
ll.remove("F");
ll.remove(2);
System.out.println("Contents of ll after deletion:"  + ll);
// Remove first and last elements.
ll.removeFirst();
ll.removeLast();
System.out.println("ll after deleting first & last : "+ll);
// Get and set a value.
String val = ll.get(2);
System.out.println("The element at index 2: " + val);
ll.set(2, "E+");
System.out.println("ll after change: " + ll);
}
}

Generic Stack Class Demo Example in java util Package

class Stack<T>
{ int top = -1; int size;
Object a[];
Stack(int size)
{ this.size = size;
a = new Object[size];
}
void push(T obj)
{ if(top == size-1)
{ System.out.println("Stack Full");
return;
}
top = top + 1; a[top] = obj;
}
T pop()
{ if(top ==  -1)
{ return null;
}
T obj = (T)a[top];
top = top - 1;
return obj;
}
void display()
{
for(int i = top; i>=0; i--)
{
T obj1 = (T) a[i];
System.out.println(obj1);
}
}
}

class Student
{
int rollno;
String name;
String phone;
public String toString()
{
return  rollno + ", " + name + ", " + phone;
}
}

class GenericStackDemo
{ public static void main(String args[])
{ Stack st1 = new Stack(10);
st1.push("Devesh");
st1.push(10);
st1.push(new Double(5.4));
System.out.println("Stack of type Object:");
st1.display();
Stack<String> st2 = new Stack<String>(20);
st2.push("Rakesh");
st2.push("Rahul");
st2.push("Devesh");
st2.push("Manohar");
//st2.push(20); //- Compile time error
System.out.println("\nStack of type String:");
st2.display();
Stack<Student> st3 = new Stack<Student>(10);
Student s1 = new Student();
s1.rollno = 1; s1.name = "Rahul"; s1.phone = "2590381";
Student s2 = new Student();
s2.rollno = 5; s2.name = "Devesh"; s2.phone = "9829059033";
Student s3 = new Student();
s3.rollno = 3; s3.name = "Rakesh"; s3.phone = "2590381";
Student s4 = new Student();
s4.rollno = 2; s4.name = "Manohar"; s4.phone = "2590381";
Student s5 = new Student();
s5.rollno = 4; s5.name = "Prateek"; s5.phone = "2590381";
st3.push(s1);
st3.push(s2);
st3.push(s3);
st3.push(s4);
st3.push(s5);
//st3.push("Devesh"); //- will not compile
System.out.println("\nStack of type Student:");
st3.display();
Stack<Integer> s = new Stack<Integer>(10);
st1 = st3; //- will not compile;
}
}

Generic Class Demo Example in java util package

class GenMap<T1,T2>
{
int index = -1;
Object a1[] = new Object[10];
Object a2[] = new Object[10];
void put(T1 obj1, T2 obj2)
{
index++;
a1[index] = obj1;
a2[index] = obj2;
}
T2 get(T1 key)
{
for(int i = 0; i <= index; i++)
{
if(a1[i].equals(key))
return (T2) a2[i];
}
return null;
}
void display()
{
for(int i = 0; i <= index; i++)
{
System.out.println(a1[i] + " : " + a2[i]);
}
}
}


class GenericDemo2
{ public static void main(String args[])
{ GenMap<String, String> gm = new GenMap<String, String>();
gm.put("Devesh", "Ramesh");
gm.put("Pawan", "Prakash");
gm.put("Naveen", "Narendra");
//gm.put("Neeraj", 2433);
System.out.println("Map entries: ");
gm.display();
String fname = gm.get("Pawan");
System.out.println("Pawan's father name: " + fname);

GenMap<String, Double> gm1 = new GenMap<String, Double>();
gm1.put("Devesh", 1000.0);
gm1.put("Pawan", 2000.0);
gm1.put("Naveen", 3000.0);
//gm1.put("Neeraj", "Mohan");
System.out.println();
System.out.println("Map entries: ");
gm1.display();
double balance = gm1.get("Pawan");
System.out.println("Pawan's balance is: " + balance);
}
}

Friday, 11 September 2015

HashTable in java util package

import java.util.*;
class HashtableDemo1
{ public static void main(String args[])
{ // Create a hash map.
Hashtable<String, Double> ht = new Hashtable<String, Double>();
      // Put elements to the map
ht.put("Devesh",1000.00);
ht.put("Rahul",1500.00);
ht.put("Rakesh",1200.00);
ht.put("Manohar",900.00);
ht.put("Prateek",1300.00);
// Get a set of the entries.
Set<String> set = ht.keySet();
// Display the set.
// Get an iterator.
Iterator <String>itr = set.iterator();
String name;
while(itr.hasNext())
{ name = itr.next();
System.out.println(name + ": " + ht.get(name));
}
System.out.println();
  // Deposit 1000 into Prateek's account.
double balance = (Double) ht.get("Prateek");
ht.put("Prateek", balance + 1000);
balance = (Double)ht.get("Prateek");
System.out.println("Prateek's new balance: " + balance);
}
}

HashTable in java util package

import java.util.*;
class HashtableDemo
{ public static void main(String args[])
{ // Create a hash map.
Hashtable<String, Double> ht = new Hashtable<String, Double>();
      // Put elements to the map
ht.put("Devesh",1000.00);
ht.put("Rahul",1500.00);
ht.put("Rakesh",1200.00);
ht.put("Manohar",900.00);
ht.put("Prateek",1300.00);

String name;
Enumeration<String> names = ht.keys();
while(names.hasMoreElements())
{ name = names.nextElement();
System.out.println(name + ": " + ht.get(name));

}
System.out.println();
// Deposit 1000 into Prateek's account.
double balance = (Double) ht.get("Prateek");
ht.put("Prateek", balance + 1000);
balance = (Double)ht.get("Prateek");
System.out.println("Pr ateek's new balance: " + balance);
}
}

Thursday, 20 August 2015

How to find prime no.

 import java.io.*
 public class PrimeNo {
  public static void main(String args[]) {
   Console con = System.console();
   int num = Integer.parseInt(con.readLine());
   for(int i=2;i<=num/2;i++) {
    temp=num%i;
    if(temp==0) {
     isPrime=false;
     break;
    }
   }
   //If isPrime is true then the number is prime else not
   if(isPrime)
    System.out.println(num + " is Prime Number");
   else
    System.out.println(num + " is not Prime Number");
  }
 }

Thursday, 13 August 2015

To check if a number is Armstrong number using java

public class ArmstrongNumber {

    public static void main(String args[]) {
        int n = 0, temp = 0, r = 0, s = 0;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number ");
        if (in.hasNextInt()) {
            n = in.nextInt(); // if there is another number
        } else {
            n = 0;
        }
        temp = n;
        while (n != 0) {
            r = n % 10;
            s = s + (r * r * r);
            n = n / 10;
        }

        if (temp == s) {
            System.out.println(n + " is an Armstrong Number");
        } else {
            System.out.println(n + " is not an Armstrong Number");
        }
    }
}

Wednesday, 5 August 2015

Write a program for print Fibonacci series

public class MyFibonacci {

    public static void main(String a[]){
       
         int febCount = 15;
         int[] feb = new int[febCount];
         feb[0] = 0;
         feb[1] = 1;
         for(int i=2; i < febCount; i++){
             feb[i] = feb[i-1] + feb[i-2];
         }

         for(int i=0; i< febCount; i++){
                 System.out.print(feb[i] + " ");
         }
    }
}

Sunday, 2 August 2015

How to swap two numbers without using temporary variable?

public class MySwapingTwoNumbers {

    public static void main(String a[]){
        int x = 10;
        int y = 20;
        System.out.println("Before swap:");
        System.out.println("x value: "+x);
        System.out.println("y value: "+y);
        x = x+y;
        y=x-y;
        x=x-y;
        System.out.println("After swap:");
        System.out.println("x value: "+x);
        System.out.println("y value: "+y);
    }
}

Tuesday, 28 July 2015

Find the third max from input four number

import java.io.*;
class Max
{
public static void main(String args[])
{
int a,b,c,d,max1=0,max2=0,max3=0,max4=0;
Console con = System.console();
System.out.print("enter value of a");
a=Integer.parseInt(con.readLine());
System.out.print("enter value of b");
b=Integer.parseInt(con.readLine());
System.out.print("enter value of c");
c=Integer.parseInt(con.readLine());
System.out.print("enter value of d");
d=Integer.parseInt(con.readLine());
if(a>b)
{
max1=a;
max2=b;
}
else
{ max1=b;
max2=a;
}
if(c>max1)
{
max3=max2;
max2=max1;
max1=c;
}
else if(c>max2)
{
max3=max2;
max2=c;
}
else
max3=c;
if(d>max1)
{
max4=max3;
max3=max2;
max2=max1;
max1=d;
}
else if(d>max2)
{
max4=max3;
max3=max2;
max2=d;
}
else if(d>max3)
{
max4=max3;
max3=d;
}
else
max4=d;
System.out.println("max1 ="+max1+"\n max2 ="+max2+"\n max3 ="+max3+"\n max4 ="+max4);
}
}

Thursday, 16 July 2015

How to find Palidrom

import java.io.*;
class pallidrom
{
public static void main(String args[])
{
int a,b,n,sum=0;
Console con = System.console();
n = Integer.parseInt(con.readLine());
b=n;
while(n!=0)
{
a=n%10;
sum = sum*10+a;
n=n/10;
}
if(b== sum)
System.out.print(b+"Number is pallidrom");
else
System.out.print(sum+"Number is not pallidrom");
}
}

Saturday, 4 July 2015

Write a Program for Series

 1=1
 12=1+2=3
 36=3+6=9
 123456789=1+2+3+4+5+6+7+8+9=45

import java.io.*;
class Example
{
public static void main(String args[])
{
int a,n,sum=0;
Console con = System.console();
n = Integer.parseInt(con.readLine());
while(n!=0)
{
a=n%10;
sum = sum+a;
n=n/10;
}
System.out.print(n+" series add is"+sum);
}
}

Monday, 29 June 2015

Java program for binary search

import java.util.Scanner;

class BinarySearch
{
  public static void main(String args[])
  {
    int c, first, last, middle, n, search, array[];

    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of elements");
    n = in.nextInt();
    array = new int[n];

    System.out.println("Enter " + n + " integers");


    for (c = 0; c < n; c++)
      array[c] = in.nextInt();

    System.out.println("Enter value to find");
    search = in.nextInt();

    first  = 0;
    last   = n - 1;
    middle = (first + last)/2;

    while( first <= last )
    {
      if ( array[middle] < search )
        first = middle + 1;  
      else if ( array[middle] == search )
      {
        System.out.println(search + " found at location " + (middle + 1) + ".");
        break;
      }
      else
         last = middle - 1;

      middle = (first + last)/2;
   }
   if ( first > last )
      System.out.println(search + " is not present in the list.\n");
  }
}

Saturday, 20 June 2015

Java program for linear search

import java.util.Scanner;
class LinearSearch
{
  public static void main(String args[])
  {
    int c, n, search, array[];

    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of elements");
    n = in.nextInt();
    array = new int[n];

    System.out.println("Enter " + n + " integers");

    for (c = 0; c < n; c++)
      array[c] = in.nextInt();

    System.out.println("Enter value to find");
    search = in.nextInt();

    for (c = 0; c < n; c++)
    {
      if (array[c] == search)     /* Searching element is present */
      {
         System.out.println(search + " is present at location " + (c + 1) + ".");
          break;
      }
   }
   if (c == n)  /* Searching element is absent */
      System.out.println(search + " is not present in array.");
  }
}

Tuesday, 2 June 2015

Java program to find factorial

import java.util.Scanner;
class Factorial
{
   public static void main(String args[])
   {
      int n, c, fact = 1;

      System.out.println("Enter an integer to calculate it's factorial");
      Scanner in = new Scanner(System.in);

      n = in.nextInt();

      if ( n < 0 )
         System.out.println("Number should be non-negative.");
      else
      {
         for ( c = 1 ; c <= n ; c++ )
            fact = fact*c;

         System.out.println("Factorial of "+n+" is = "+fact);
      }
   }
}

Monday, 1 June 2015

Java program to generate random numbers

import java.util.*;
class RandomNumbers {
  public static void main(String[] args) {
    int c;
    Random t = new Random();

    // random integers in [0, 100]

    for (c = 1; c <= 10; c++) {
      System.out.println(t.nextInt(100));
    }
  }
}

Thursday, 14 May 2015

Pattern program 14


	import java.io.Console;
	class pattern14 {
		public static void main(String args[]) {
			int n;
			String s="";
			Console con=System.console();
			System.out.print("Enter your Number : ");
			n=Integer.parseInt(con.readLine());
			for(int i=1;i<=n;i++) {
				for(int j=n;j>i;j--) {
					System.out.print(" ");
				}
				for(int k=i;k>=1;k--) {
					System.out.print("*");
				}
				System.out.println();
			}
		}
	}

Wednesday, 13 May 2015

Pattern program 13


	import java.io.Console;
	class pattern13 {
		public static void main(String args[]) {
			for(int i = 1;i<=5;i++) {
				for(int j = 1;j<=i;j++)
					System.out.print(j);
				for(int j = 1;j<=9-2*i;j++)
					System.out.print(" ");
				for(int j = i;j>=1;j--) {
					if(j==5)
						continue;
					System.out.print(j);
				}
				System.out.println();
			}
		}
	}

Tuesday, 12 May 2015

Pattern program 12



public class pattern12{
    public static void main(String args[]) {
        int ln = 4;    //Number of lines/rows
        for(int i=1; i<=ln; i++){
            //Spacing Logic
            for(int s=i;s<=ln;s++){
                System.out.print("  ");
            }
            for(int j=1,p=i;j<=i;j++,p++){
                System.out.print(p+" ");
                if(j==i){
                    p--;
                    for(int k=1;k<i;k++,--p){
                        System.out.print(p+" ");
                    }
                }
            }
            System.out.println();
        }
    }
}

Monday, 11 May 2015

Pattern program 11


	import java.io.Console;
	class pattern11 {
		public static void main(String args[]) {
			int ln=9;   //Number of lines/rows
			for(int i=1;i<=ln;i++){
				if(i<=ln/2+1){
					for(int j=1;j<=i;j++)
						System.out.print("*");
				} else {
					for(int j=ln-i+1;j>0;j--)
						System.out.print("*");
				}
				System.out.println();
			}
		}
	}

Sunday, 10 May 2015

Pattern program 10



public class pattern10 {
    public static void main ( String arg[] ){
        int ln = 5 , p=1;
        for(int i=0;i<ln;i++){
            for(int s=ln-1;s>i;s--){
                System.out.print(" ");
            }
            for(int j=0;j<=i*2;j++){
                System.out.print(p);
                p = (p==1)?0:1;
            }
            System.out.println();
        }
    }
}

Saturday, 9 May 2015

Pattern program 9



	public class pattern9 {
		public static void main( String arg[]){
			int ck=0,c=2;
			while(c>0){
				if(ck==0){
					for(int i=1,r=5;i<=5;i++,r--){
						for(int j=1;j<=r;j++){
							System.out.print(j);
						}
						System.out.println();
					}
					ck++;
				} else{
				for(int i=2;i<=5;i++){
					for(int j=1;j<=i;j++){
						System.out.print(j);
					}
					System.out.println();
				}
			c--;
			}
		}
	}

Send by Yawer Moin

	import java.io.*;
	class pattern9 {
		public static void main(String args[]) {
			Console con=System.console();
			int n;
			n=Integer.parseInt(con.readLine());
			for(int i=1,r=n;i<=n;i++,r--) {
				for(int j=1;j<=r;j++)
					System.out.print(j);
				System.out.println();
				if(i==n) {
					for(int j=2;j<=n;j++) {
						for(int k=1;k<=j;k++) {
							System.out.print(k);
						}
						System.out.println();
					}
				}
			}
		}
	}

Friday, 8 May 2015

Pattern program 8


	public class pattern8 {
		public static void main( String arg[]){
			int ck=0,c=2;
			while(c>0){
				if(ck==0){
					for(int i=1;i<=5;i++){
						for(int j=1;j<=i;j++){
							System.out.print(j);
						}
						System.out.println();
					}
					ck++;
				} else{
					for(int i=1,r=5-1;i<=5-1;i++,r--){
						for(int j=1;j<=r;j++){
							System.out.print(j);
						}
						System.out.println();
					}
				}
				c--;
			}
		}
	}

Thursday, 7 May 2015

Pattern program 7



	public class pattern7 {
		public static void main( String arg[]){
			for(int i=1,r=5;i<=5;i++,r--)
			{
				for(int j=1;j<=r;j++)
				{
					System.out.print(j);
				}
				System.out.println();
			}
		}
	}

Wednesday, 6 May 2015

Pattern program 6


	public class pattern6 {
		public static void main( String arg[]){
			for(int i=1;i<=5;i++)
			{
				for(int j=5;j>=i;j--)
				{
					System.out.print(j);
				}
				System.out.println();
			}
		}
	}

Tuesday, 5 May 2015

Pattern program 5


	public class pattern5 {
		public static void main( String arg[]){
			for(int i=1;i<=5;i++)
			{
				for(int j=1;j<=i;j++)
				{
					System.out.print(j);
				}
				System.out.println();
			}
		}
	}

Monday, 4 May 2015

Pattern program 4




	import java.io.*;
	class pattern4
	{
		public static void main(String args[])
		{
			Console con = System.console();
			int n = Integer.parseInt(con.readLine());
			for(int i=1;i<=n;i++)
			{
				for(int j=1;j<=i;j++)
					System.out.print(j+" ");
				System.out.println();
			}
		}
	}